test.ipynb 5.1 KB
Notebook
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天的日期是:  2023-05-11\n"
     ]
    }
   ],
   "source": [
17 18
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
19
    "\n",
20 21 22 23 24 25
    "\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "\n",
    "def costFunction(theta, X, y):\n",
    "    m = len(y)\n",
6
UPDATE  
622ee496dfef6c4fdb84cccd 已提交
26
    "    \n",
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    "    J = 0\n",
    "    grad = np.zeros(theta.shape)\n",
    "    \n",
    "    h = sigmoid(np.dot(X, theta))\n",
    "    J = (-1/m) * np.sum(y*np.log(h) + (1-y)*np.log(1-h))\n",
    "    grad = (1/m) * np.dot(X.T, (h-y))\n",
    "    \n",
    "    return J, grad\n",
    "\n",
    "def gradientDescent(X, y, theta, alpha, num_iters):\n",
    "    m = len(y)\n",
    "    J_history = np.zeros(num_iters)\n",
    "    \n",
    "    for i in range(num_iters):\n",
    "        J_history[i], grad = costFunction(theta, X, y)\n",
    "        theta = theta - alpha*grad\n",
    "        \n",
    "    return theta, J_history\n",
    "\n",
    "# 生成样本数据\n",
    "np.random.seed(0)\n",
    "X = np.random.randn(100, 2)\n",
    "ones = np.ones((100, 1))\n",
    "X = np.hstack((ones, X))\n",
    "y = np.random.randint(0, 2, size=(100,1))\n",
    "\n",
    "# 初始化theta\n",
    "initial_theta = np.zeros((X.shape[1], 1))\n",
    "\n",
    "# 梯度下降\n",
    "alpha = 0.1\n",
    "num_iters = 1000\n",
    "theta, J_history = gradientDescent(X, y, initial_theta, alpha, num_iters)\n",
    "\n",
    "# 绘制决策边界\n",
    "x1 = np.arange(-3, 3, 0.1)\n",
    "x2 = -(theta[0]+theta[1]*x1)/theta[2]\n",
    "plt.plot(x1, x2, label='Decision Boundary')\n",
    "plt.scatter(X[:, 1], X[:, 2], c=y.flatten())\n",
    "plt.legend()\n",
    "plt.show()\n"
68 69 70 71
   ]
  },
  {
   "cell_type": "code",
6
UPDATE  
622ee496dfef6c4fdb84cccd 已提交
72
   "execution_count": 1,
73 74 75 76 77 78 79 80 81
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'numpy'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
6
UPDATE  
622ee496dfef6c4fdb84cccd 已提交
82
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnumpy\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnp\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmatplotlib\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpyplot\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mplt\u001b[39;00m\n\u001b[1;32m      5\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39msigmoid\u001b[39m(z):\n",
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'numpy'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "\n",
    "def costFunction(theta, X, y):\n",
    "    m = len(y)\n",
    "    J = 0\n",
    "    grad = np.zeros(theta.shape)\n",
    "    \n",
    "    h = sigmoid(np.dot(X, theta))\n",
    "    J = (-1/m) * np.sum(y*np.log(h) + (1-y)*np.log(1-h))\n",
    "    grad = (1/m) * np.dot(X.T, (h-y))\n",
    "    \n",
    "    return J, grad\n",
    "\n",
    "def gradientDescent(X, y, theta, alpha, num_iters):\n",
    "    m = len(y)\n",
    "    J_history = np.zeros(num_iters)\n",
    "    \n",
    "    for i in range(num_iters):\n",
    "        J_history[i], grad = costFunction(theta, X, y)\n",
    "        theta = theta - alpha*grad\n",
    "        \n",
    "    return theta, J_history\n",
    "\n",
    "# 生成样本数据\n",
    "np.random.seed(0)\n",
    "X = np.random.randn(100, 2)\n",
    "ones = np.ones((100, 1))\n",
    "X = np.hstack((ones, X))\n",
    "y = np.random.randint(0, 2, size=(100,1))\n",
    "\n",
    "# 初始化theta\n",
    "initial_theta = np.zeros((X.shape[1], 1))\n",
    "\n",
    "# 梯度下降\n",
    "alpha = 0.1\n",
    "num_iters = 1000\n",
    "theta, J_history = gradientDescent(X, y, initial_theta, alpha, num_iters)\n",
    "\n",
    "# 绘制决策边界\n",
    "x1 = np.arange(-3, 3, 0.1)\n",
    "x2 = -(theta[0]+theta[1]*x1)/theta[2]\n",
    "plt.plot(x1, x2, label='Decision Boundary')\n",
    "plt.scatter(X[:, 1], X[:, 2], c=y.flatten())\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
164
   "version": "3.9.5 (default, Nov 23 2021, 15:27:38) \n[GCC 9.3.0]"
165 166 167 168 169 170 171 172 173 174 175
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "f9f85f796d01129d0dd105a088854619f454435301f6ffec2fea96ecbd9be4ac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}