赞
踩
UCI糖尿病数据利用逻辑回归算法进行训练和预测
jupyter
{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "diabetes_data = pd.read_csv('diabetes.csv')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>Pregnancies</th>\n", " <th>Glucose</th>\n", " <th>BloodPressure</th>\n", " <th>SkinThickness</th>\n", " <th>Insulin</th>\n", " <th>BMI</th>\n", " <th>DiabetesPedigreeFunction</th>\n", " <th>Age</th>\n", " <th>Outcome</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>6</td>\n", " <td>148</td>\n", " <td>72</td>\n", " <td>35</td>\n", " <td>0</td>\n", " <td>33.6</td>\n", " <td>0.627</td>\n", " <td>50</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>1</td>\n", " <td>85</td>\n", " <td>66</td>\n", " <td>29</td>\n", " <td>0</td>\n", " <td>26.6</td>\n", " <td>0.351</td>\n", " <td>31</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>8</td>\n", " <td>183</td>\n", " <td>64</td>\n", " <td>0</td>\n", " <td>0</td>\n", " <td>23.3</td>\n", " <td>0.672</td>\n", " <td>32</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>1</td>\n", " <td>89</td>\n", " <td>66</td>\n", " <td>23</td>\n", " <td>94</td>\n", " <td>28.1</td>\n", " <td>0.167</td>\n", " <td>21</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>0</td>\n", " <td>137</td>\n", " <td>40</td>\n", " <td>35</td>\n", " <td>168</td>\n", " <td>43.1</td>\n", " <td>2.288</td>\n", " <td>33</td>\n", " <td>1</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \\\n", "0 6 148 72 35 0 33.6 \n", "1 1 85 66 29 0 26.6 \n", "2 8 183 64 0 0 23.3 \n", "3 1 89 66 23 94 28.1 \n", "4 0 137 40 35 168 43.1 \n", "\n", " DiabetesPedigreeFunction Age Outcome \n", "0 0.627 50 1 \n", "1 0.351 31 0 \n", "2 0.672 32 1 \n", "3 0.167 21 0 \n", "4 2.288 33 1 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diabetes_data.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def sigmoid(z):\n", " return 1 / (1 + np.exp(-z))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sigmoid(0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[<matplotlib.lines.Line2D at 0x1dd0d80a908>]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHelJREFUeJzt3Xt8VPWd//HXh4SLCoiCpCoWoaAuVrc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PjY/q+B59z9/3fQ5kNtnwGY2SjC7/ubBcqXzs9rMfCF+FkzHwfecffNhciXoMP/bUd5/HIi6k90O1oIRagJ2A1sAZYk7LuKcCbDemB8wuN1wFHx9aGEot8I3AH0zHPeW4Gvt3vsKKAuIc/q+LKWMBxRqGM5H3gGWEP4hTqyfb749gTCWRcvFjhfI2Hs9en48rP2+aI4fsmOBzCH8EcIoFf8vdUYf68NLeAxO4swhLEm4bhNAL7e9j4EZsSP1WrCB9VnFDBf0p9Xu3wGzI0f32dIOCuuQBkPJhTrQxMeK4rjl4tFV6iKiJShUhyWERGRFFTcRUTKkIq7iEgZUnEXESlDKu4iImVIxV1EpAypuIuIlCEVdxGRMvS/Tq7yTBjr6swAAAAASUVORK5CYII=\n", "text/plain": [ "<Figure size 432x288 with 1 Axes>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "xx = np.arange(-10,10,step=1)\n", "yy = sigmoid(xx)\n", "plt.grid()\n", "plt.plot(xx, yy, 'r')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def logistic_model(X, theta):\n", " temp = np.dot(X,theta.T)\n", " return sigmoid(temp)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "diabetes_data.insert(0,'Ones',1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>Ones</th>\n", " <th>Pregnancies</th>\n", " <th>Glucose</th>\n", " <th>BloodPressure</th>\n", " <th>SkinThickness</th>\n", " <th>Insulin</th>\n", " <th>BMI</th>\n", " <th>DiabetesPedigreeFunction</th>\n", " <th>Age</th>\n", " <th>Outcome</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>1</td>\n", " <td>6</td>\n", " <td>148</td>\n", " <td>72</td>\n", " <td>35</td>\n", " <td>0</td>\n", " <td>33.6</td>\n", " <td>0.627</td>\n", " <td>50</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>1</td>\n", " <td>1</td>\n", " <td>85</td>\n", " <td>66</td>\n", " <td>29</td>\n", " <td>0</td>\n", " <td>26.6</td>\n", " <td>0.351</td>\n", " <td>31</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>1</td>\n", " <td>8</td>\n", " <td>183</td>\n", " <td>64</td>\n", " <td>0</td>\n", " <td>0</td>\n", " <td>23.3</td>\n", " <td>0.672</td>\n", " <td>32</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>1</td>\n", " <td>1</td>\n", " <td>89</td>\n", " <td>66</td>\n", " <td>23</td>\n", " <td>94</td>\n", " <td>28.1</td>\n", " <td>0.167</td>\n", " <td>21</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>1</td>\n", " <td>0</td>\n", " <td>137</td>\n", " <td>40</td>\n", " <td>35</td>\n", " <td>168</td>\n", " <td>43.1</td>\n", " <td>2.288</td>\n", " <td>33</td>\n", " <td>1</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " Ones Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \\\n", "0 1 6 148 72 35 0 33.6 \n", "1 1 1 85 66 29 0 26.6 \n", "2 1 8 183 64 0 0 23.3 \n", "3 1 1 89 66 23 94 28.1 \n", "4 1 0 137 40 35 168 43.1 \n", "\n", " DiabetesPedigreeFunction Age Outcome \n", "0 0.627 50 1 \n", "1 0.351 31 0 \n", "2 0.672 32 1 \n", "3 0.167 21 0 \n", "4 2.288 33 1 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diabetes_data.head()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "orig_data = diabetes_data.values\n", "cols = orig_data.shape[1]\n", "X = orig_data[:,0:cols-1] #X大写,表示是一个矩阵\n", "y = orig_data[:,cols-1:cols] #y小写,表示矢量\n", "theta = np.zeros([1,cols-1]) #theta设为行向量" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "theta" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def cost(X, y, theta):\n", " item1 = np.multiply(y, np.log(logistic_model(X,theta))) \n", " item2 = np.multiply(1-y, np.log(1 - logistic_model(X,theta))) \n", " return np.sum(item1 - item2) / (len(X))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.20938821079415015" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cost(X, y, theta)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def gradient(X, y, theta):\n", " grad = np.zeros(theta.shape) #一个梯度是对theta求导的\n", " error = (logistic_model(X,theta) - y).ravel()\n", " for j in range(len(theta.ravel())):\n", " temp = np.multiply(error, X[:,j])\n", " grad[0,j] = np.sum(temp) / len(X)\n", " return grad" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "#拟合 fit,求模型的参数,也称为训练的过程,\n", "def fit(X, y, theta, iter_num = 5000, alpha=0.00001):\n", " #梯度下降求解\n", " i = 0 # 迭代次数\n", " grad = np.zeros(theta.shape) # 计算的梯度\n", " costs = [cost(X, y, theta)] # 损失值\n", " while True:\n", " grad = gradient(X, y, theta)\n", " theta = theta - alpha*grad # 参数更新\n", " costs.append(cost(X, y, theta)) # 计算新的损失\n", " i += 1 \n", " if i % (iter_num / 10) == 0: print(costs[i])\n", " if i > iter_num: break\n", " \n", " return theta" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "D:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: RuntimeWarning: divide by zero encountered in log\n", " This is separate from the ipykernel package so we can avoid doing imports until\n", "D:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in multiply\n", " This is separate from the ipykernel package so we can avoid doing imports until\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "-5.511409235893268\n", "-5.31859458354884\n", "-5.063153190867181\n", "-4.696751678117883\n", "-4.074011252701483\n", "-3.902875016698084\n", "-3.744762960211906\n", "-3.601077279719243\n", "-3.4711575061505564\n", "-3.352062637279824\n" ] } ], "source": [ "# 调参,只能根据经验来,炼丹\n", "theta = fit(X, y, theta,iter_num = 500000, alpha=0.0015)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "D:\\Anaconda\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2026: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.\n", " FutureWarning)\n" ] } ], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, train_size = .8)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(614, 9)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train.shape" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "theta = np.zeros([1,cols-1])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "D:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: RuntimeWarning: divide by zero encountered in log\n", " This is separate from the ipykernel package so we can avoid doing imports until\n", "D:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: RuntimeWarning: invalid value encountered in multiply\n", " This is separate from the ipykernel package so we can avoid doing imports until\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "-3.4953304853973584\n", "-4.075793679628199\n", "-3.7134641993740907\n", "-3.402073232763518\n", "-3.155158733286961\n", "-2.9550791865014996\n", "-2.785268118118423\n", "-2.6323045981679933\n", "-2.4989764065989086\n", "-2.3919272008733246\n" ] } ], "source": [ "theta = fit(X_train, y_train, theta,iter_num = 1000000, alpha=0.0015)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# 测试过程\n", "def predict(X,theta, threshold = 0.5):\n", " p = logistic_model(X, theta)\n", " #分类,如果这个概率大于0.5,分类为1,否则为0\n", " y = np.where(p > threshold, 1, 0)\n", " return y" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# shift + enter\n", "myPredict_y = predict(X_test, theta, threshold = 0.5)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>0</th>\n", " <th>MyPredict</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>5</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>7</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>9</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>10</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>11</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>13</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>14</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>15</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>16</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>17</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>18</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>19</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>20</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>21</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>22</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>23</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>24</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>25</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>26</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>27</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>28</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>29</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>...</th>\n", " <td>...</td>\n", " <td>...</td>\n", " </tr>\n", " <tr>\n", " <th>124</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>125</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>126</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>127</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>128</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>129</th>\n", " <td>1.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>130</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>131</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>132</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>133</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>134</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>135</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>136</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>137</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>138</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>139</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>140</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>141</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>142</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>143</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>144</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>145</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>146</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>147</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>148</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>149</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>150</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>151</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>152</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>153</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "<p>154 rows × 2 columns</p>\n", "</div>" ], "text/plain": [ " 0 MyPredict\n", "0 0.0 0\n", "1 1.0 1\n", "2 1.0 1\n", "3 0.0 0\n", "4 0.0 0\n", "5 0.0 1\n", "6 1.0 1\n", "7 1.0 1\n", "8 0.0 0\n", "9 1.0 1\n", "10 0.0 0\n", "11 0.0 1\n", "12 0.0 0\n", "13 1.0 1\n", "14 0.0 1\n", "15 0.0 1\n", "16 0.0 0\n", "17 0.0 0\n", "18 0.0 1\n", "19 1.0 1\n", "20 0.0 1\n", "21 1.0 1\n", "22 0.0 0\n", "23 0.0 1\n", "24 0.0 1\n", "25 0.0 0\n", "26 0.0 0\n", "27 1.0 1\n", "28 1.0 1\n", "29 0.0 0\n", ".. ... ...\n", "124 1.0 1\n", "125 1.0 1\n", "126 1.0 1\n", "127 1.0 1\n", "128 0.0 1\n", "129 1.0 0\n", "130 0.0 1\n", "131 0.0 0\n", "132 1.0 1\n", "133 0.0 1\n", "134 0.0 1\n", "135 1.0 1\n", "136 1.0 1\n", "137 1.0 1\n", "138 0.0 0\n", "139 0.0 1\n", "140 0.0 0\n", "141 0.0 0\n", "142 0.0 1\n", "143 0.0 1\n", "144 0.0 0\n", "145 0.0 0\n", "146 0.0 0\n", "147 0.0 0\n", "148 0.0 0\n", "149 0.0 0\n", "150 0.0 1\n", "151 1.0 1\n", "152 0.0 1\n", "153 0.0 1\n", "\n", "[154 rows x 2 columns]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 要看预测的值和真实值y_test之间的差别\n", "yy = pd.DataFrame(y_test)\n", "yy[\"MyPredict\"] = myPredict_y\n", "yy\n", "#20个数据对了16个, 80%准确率" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "D:\\Anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py:578: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", " y = column_or_1d(y, warn=True)\n" ] }, { "data": { "text/plain": [ "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n", " intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n", " penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n", " verbose=0, warm_start=False)" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 看sklearn的函数的表现\n", "from sklearn.linear_model import LogisticRegression\n", "\n", "lr = LogisticRegression()\n", "\n", "lr.fit(X_train, y_train)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "lr_predict = lr.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 1., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 1., 0., 0., 0.,\n", " 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1.,\n", " 0., 0., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 1., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0., 1., 0., 0., 0.,\n", " 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0.,\n", " 0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 0., 0., 0., 1., 1., 0., 1.,\n", " 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", " 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.,\n", " 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0.,\n", " 1.])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lr_predict" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>0</th>\n", " <th>MyPredict</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>4</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>5</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>6</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>7</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>8</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>9</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>10</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>11</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>12</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>13</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>14</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>15</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>16</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>17</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>18</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>19</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>20</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>21</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>22</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>23</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>24</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>25</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>26</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>27</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>28</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>29</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>...</th>\n", " <td>...</td>\n", " <td>...</td>\n", " </tr>\n", " <tr>\n", " <th>124</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>125</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>126</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>127</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>128</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>129</th>\n", " <td>1.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>130</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>131</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>132</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>133</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>134</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>135</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>136</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>137</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>138</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>139</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>140</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>141</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>142</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>143</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>144</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>145</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>146</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>147</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>148</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>149</th>\n", " <td>0.0</td>\n", " <td>0</td>\n", " </tr>\n", " <tr>\n", " <th>150</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>151</th>\n", " <td>1.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>152</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " <tr>\n", " <th>153</th>\n", " <td>0.0</td>\n", " <td>1</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "<p>154 rows × 2 columns</p>\n", "</div>" ], "text/plain": [ " 0 MyPredict\n", "0 0.0 0\n", "1 1.0 1\n", "2 1.0 1\n", "3 0.0 0\n", "4 0.0 0\n", "5 0.0 1\n", "6 1.0 1\n", "7 1.0 1\n", "8 0.0 0\n", "9 1.0 1\n", "10 0.0 0\n", "11 0.0 1\n", "12 0.0 0\n", "13 1.0 1\n", "14 0.0 1\n", "15 0.0 1\n", "16 0.0 0\n", "17 0.0 0\n", "18 0.0 1\n", "19 1.0 1\n", "20 0.0 1\n", "21 1.0 1\n", "22 0.0 0\n", "23 0.0 1\n", "24 0.0 1\n", "25 0.0 0\n", "26 0.0 0\n", "27 1.0 1\n", "28 1.0 1\n", "29 0.0 0\n", ".. ... ...\n", "124 1.0 1\n", "125 1.0 1\n", "126 1.0 1\n", "127 1.0 1\n", "128 0.0 1\n", "129 1.0 0\n", "130 0.0 1\n", "131 0.0 0\n", "132 1.0 1\n", "133 0.0 1\n", "134 0.0 1\n", "135 1.0 1\n", "136 1.0 1\n", "137 1.0 1\n", "138 0.0 0\n", "139 0.0 1\n", "140 0.0 0\n", "141 0.0 0\n", "142 0.0 1\n", "143 0.0 1\n", "144 0.0 0\n", "145 0.0 0\n", "146 0.0 0\n", "147 0.0 0\n", "148 0.0 0\n", "149 0.0 0\n", "150 0.0 1\n", "151 1.0 1\n", "152 0.0 1\n", "153 0.0 1\n", "\n", "[154 rows x 2 columns]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yy" ] } ], "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", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。