手写和sklearn线性回归示例

linear_regression_example
In [1]:
# %load ../../standard_import.txt
# 引入模块
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 引入sklearn线性回归
from sklearn.linear_model import LinearRegression
from mpl_toolkits.mplot3d import axes3d

# 最多显示150列
pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', 150)
pd.set_option('display.max_seq_items', None)
 
#%config InlineBackend.figure_formats = {'pdf',}
%matplotlib inline  

import seaborn as sns
sns.set_context('notebook')
sns.set_style('white')
In [2]:
# help(pd.set_option)

熟悉numpy

In [3]:
def warmUpExercise():
    # 返回五维方阵
    return(np.identity(5))
In [4]:
warmUpExercise()
Out[4]:
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

单变量线性回归

In [5]:
data = np.loadtxt('linear_regression_data1.txt', delimiter=',')
In [6]:
np.ones(data.shape[0])
Out[6]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [7]:
data[:,0]
Out[7]:
array([ 6.1101,  5.5277,  8.5186,  7.0032,  5.8598,  8.3829,  7.4764,
        8.5781,  6.4862,  5.0546,  5.7107, 14.164 ,  5.734 ,  8.4084,
        5.6407,  5.3794,  6.3654,  5.1301,  6.4296,  7.0708,  6.1891,
       20.27  ,  5.4901,  6.3261,  5.5649, 18.945 , 12.828 , 10.957 ,
       13.176 , 22.203 ,  5.2524,  6.5894,  9.2482,  5.8918,  8.2111,
        7.9334,  8.0959,  5.6063, 12.836 ,  6.3534,  5.4069,  6.8825,
       11.708 ,  5.7737,  7.8247,  7.0931,  5.0702,  5.8014, 11.7   ,
        5.5416,  7.5402,  5.3077,  7.4239,  7.6031,  6.3328,  6.3589,
        6.2742,  5.6397,  9.3102,  9.4536,  8.8254,  5.1793, 21.279 ,
       14.908 , 18.959 ,  7.2182,  8.2951, 10.236 ,  5.4994, 20.341 ,
       10.136 ,  7.3345,  6.0062,  7.2259,  5.0269,  6.5479,  7.5386,
        5.0365, 10.274 ,  5.1077,  5.7292,  5.1884,  6.3557,  9.7687,
        6.5159,  8.5172,  9.1802,  6.002 ,  5.5204,  5.0594,  5.7077,
        7.6366,  5.8707,  5.3054,  8.2934, 13.394 ,  5.4369])
In [8]:
X = np.c_[np.ones(data.shape[0]),data[:,0]]
In [9]:
data[:,1]
Out[9]:
array([17.592  ,  9.1302 , 13.662  , 11.854  ,  6.8233 , 11.886  ,
        4.3483 , 12.     ,  6.5987 ,  3.8166 ,  3.2522 , 15.505  ,
        3.1551 ,  7.2258 ,  0.71618,  3.5129 ,  5.3048 ,  0.56077,
        3.6518 ,  5.3893 ,  3.1386 , 21.767  ,  4.263  ,  5.1875 ,
        3.0825 , 22.638  , 13.501  ,  7.0467 , 14.692  , 24.147  ,
       -1.22   ,  5.9966 , 12.134  ,  1.8495 ,  6.5426 ,  4.5623 ,
        4.1164 ,  3.3928 , 10.117  ,  5.4974 ,  0.55657,  3.9115 ,
        5.3854 ,  2.4406 ,  6.7318 ,  1.0463 ,  5.1337 ,  1.844  ,
        8.0043 ,  1.0179 ,  6.7504 ,  1.8396 ,  4.2885 ,  4.9981 ,
        1.4233 , -1.4211 ,  2.4756 ,  4.6042 ,  3.9624 ,  5.4141 ,
        5.1694 , -0.74279, 17.929  , 12.054  , 17.054  ,  4.8852 ,
        5.7442 ,  7.7754 ,  1.0173 , 20.992  ,  6.6799 ,  4.0259 ,
        1.2784 ,  3.3411 , -2.6807 ,  0.29678,  3.8845 ,  5.7014 ,
        6.7526 ,  2.0576 ,  0.47953,  0.20421,  0.67861,  7.5435 ,
        5.3436 ,  4.2415 ,  6.7981 ,  0.92695,  0.152  ,  2.8214 ,
        1.8451 ,  4.2959 ,  7.2029 ,  1.9869 ,  0.14454,  9.0551 ,
        0.61705])
In [10]:
y = np.c_[data[:,1]]
y
Out[10]:
array([[17.592  ],
       [ 9.1302 ],
       [13.662  ],
       [11.854  ],
       [ 6.8233 ],
       [11.886  ],
       [ 4.3483 ],
       [12.     ],
       [ 6.5987 ],
       [ 3.8166 ],
       [ 3.2522 ],
       [15.505  ],
       [ 3.1551 ],
       [ 7.2258 ],
       [ 0.71618],
       [ 3.5129 ],
       [ 5.3048 ],
       [ 0.56077],
       [ 3.6518 ],
       [ 5.3893 ],
       [ 3.1386 ],
       [21.767  ],
       [ 4.263  ],
       [ 5.1875 ],
       [ 3.0825 ],
       [22.638  ],
       [13.501  ],
       [ 7.0467 ],
       [14.692  ],
       [24.147  ],
       [-1.22   ],
       [ 5.9966 ],
       [12.134  ],
       [ 1.8495 ],
       [ 6.5426 ],
       [ 4.5623 ],
       [ 4.1164 ],
       [ 3.3928 ],
       [10.117  ],
       [ 5.4974 ],
       [ 0.55657],
       [ 3.9115 ],
       [ 5.3854 ],
       [ 2.4406 ],
       [ 6.7318 ],
       [ 1.0463 ],
       [ 5.1337 ],
       [ 1.844  ],
       [ 8.0043 ],
       [ 1.0179 ],
       [ 6.7504 ],
       [ 1.8396 ],
       [ 4.2885 ],
       [ 4.9981 ],
       [ 1.4233 ],
       [-1.4211 ],
       [ 2.4756 ],
       [ 4.6042 ],
       [ 3.9624 ],
       [ 5.4141 ],
       [ 5.1694 ],
       [-0.74279],
       [17.929  ],
       [12.054  ],
       [17.054  ],
       [ 4.8852 ],
       [ 5.7442 ],
       [ 7.7754 ],
       [ 1.0173 ],
       [20.992  ],
       [ 6.6799 ],
       [ 4.0259 ],
       [ 1.2784 ],
       [ 3.3411 ],
       [-2.6807 ],
       [ 0.29678],
       [ 3.8845 ],
       [ 5.7014 ],
       [ 6.7526 ],
       [ 2.0576 ],
       [ 0.47953],
       [ 0.20421],
       [ 0.67861],
       [ 7.5435 ],
       [ 5.3436 ],
       [ 4.2415 ],
       [ 6.7981 ],
       [ 0.92695],
       [ 0.152  ],
       [ 2.8214 ],
       [ 1.8451 ],
       [ 4.2959 ],
       [ 7.2029 ],
       [ 1.9869 ],
       [ 0.14454],
       [ 9.0551 ],
       [ 0.61705]])
In [11]:
# 看下维度
data.shape
Out[11]:
(97, 2)
In [12]:
# 关于scatter散点图
# plt.scatter(X[:,1], y, s=30, c='r', marker='x', linewidths=1)
plt.scatter(X[:,1], y, s=30, c='g', marker='*', linewidths=1)
plt.xlim(4,24)
#  10000人的城市人口
plt.xlabel('Population of City in 10,000s')
#  人均收入1万美元
plt.ylabel('Profit in $10,000s');

梯度下降

In [13]:
y.size
Out[13]:
97

损失函数定义 avatar

In [14]:
# 计算损失函数(定义代价函数)
def computeCost(X, y, theta=[[0],[0]]):
    m = y.size
    J = 0
    h = X.dot(theta)
    J = 1.0/(2*m)*(np.sum(np.square(h-y)))
    return J
In [15]:
computeCost(X,y)
Out[15]:
32.072733877455676
In [16]:
np.arange(1500)
Out[16]:
array([   0,    1,    2, ..., 1497, 1498, 1499])
In [17]:
np.zeros(1500)
Out[17]:
array([0., 0., 0., ..., 0., 0., 0.])
In [18]:
# 定义梯度下降函数
def gradientDescent(X, y, theta=[[0],[0]], alpha=0.01, num_iters=1500):
    m = y.size
    J_history = np.zeros(num_iters)
    
    for iter in np.arange(num_iters):
        h = X.dot(theta)
        theta = theta - alpha*(1.0/m)*(X.T.dot(h-y))
        J_history[iter] = computeCost(X, y, theta)
    return(theta, J_history)
In [19]:
# 画出每一次迭代和损失函数变化
theta , Cost_J = gradientDescent(X, y)

print(theta.ravel())
print('theta: ',theta.ravel())


plt.plot(Cost_J)
plt.ylabel('Cost J')
plt.xlabel('Iterations');
[-3.63029144  1.16636235]
theta:  [-3.63029144  1.16636235]
In [20]:
xx = np.arange(5,23)
In [21]:
xx
Out[21]:
array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
       22])
In [22]:
theta[0]
Out[22]:
array([-3.63029144])
In [23]:
theta[1]
Out[23]:
array([1.16636235])
In [24]:
theta[1]*xx
Out[24]:
array([ 5.83181175,  6.9981741 ,  8.16453645,  9.3308988 , 10.49726115,
       11.6636235 , 12.82998585, 13.9963482 , 15.16271055, 16.3290729 ,
       17.49543526, 18.66179761, 19.82815996, 20.99452231, 22.16088466,
       23.32724701, 24.49360936, 25.65997171])
In [25]:
# -3.63029144 + 1.16636235 * 5 = 2.20152031
In [26]:
yy = theta[0]+theta[1]*xx
yy
Out[26]:
array([ 2.20152031,  3.36788266,  4.53424501,  5.70060736,  6.86696971,
        8.03333206,  9.19969441, 10.36605676, 11.53241911, 12.69878147,
       13.86514382, 15.03150617, 16.19786852, 17.36423087, 18.53059322,
       19.69695557, 20.86331792, 22.02968027])
In [27]:
xx = np.arange(5,23)
yy = theta[0]+theta[1]*xx

# 画出我们自己写的线性回归梯度下降收敛的情况
plt.scatter(X[:,1], y, s=30, c='r', marker='x', linewidths=1)
plt.plot(xx,yy, label='Linear regression (Gradient descent)')

# 和Scikit-learn中的线性回归对比一下 
regr = LinearRegression()
regr.fit(X[:,1].reshape(-1,1), y.ravel())
plt.plot(xx, regr.intercept_+regr.coef_*xx, label='Linear regression (Scikit-learn GLM)')

plt.xlim(4,24)
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.legend(loc=4);
In [28]:
# 预测一下人口为35000和70000的城市的结果
print(theta.T.dot([1, 3.5])*10000)
print(theta.T.dot([1, 7])*10000)
print(theta.T.dot([1, 15])*10000)
[4519.7678677]
[45342.45012945]
[138651.43815629]

本文于 2020-03-13 11:03 由作者进行过修改

本文链接:https://itarvin.com/detail-35.aspx

登录或者注册以便发表评论

登录

注册