学习覃禀丰老师机器学习的第二天
一元线性回归可以用一条直线来描述拟合程度
二元线性回归用一个平面来描述拟合程度
多元则无法在二维平面画出图像来描述,用一个超平面来描述。
大多数多元都是通过函数整合几个自变量变成一个自变量或两个,转成容易分析一元和二元。
写成的网格矩阵
原来的x0,x1
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.] [4. 3. 4. 2. 2. 2. 3. 4. 3. 2.]
现在的x0,x1
[[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]
[100. 50. 100. 100. 50. 80. 75. 65. 90. 90.]]
[[4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]]
用梯度下降法实现的二元线性回归
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = genfromtxt(r"Delivery.csv",delimiter=',')
x_data = data[:,:-1] #获取data所有行的除最后一列之外所有的列
y_data = data[:,-1] #获取data所有行最后一列
lr = 0.0001
t0 = 0
t1 = 0
t2 = 0
epochs = 1000
def compute_error(t0,t1,t2,x_data,y_data):
totalError = 0
for i in range(0,len(x_data)):
totalError += (y_data[i] - (t1 * x_data[i,0] + t2 * x_data[i,1] + t0))
return totalError / float(len(x_data))
def gendient_descent_runner(x_data, y_data, t0, t1, t2,lr,epochs):
m = float(len(x_data))
for i in range(epochs):
t0_grad = 0
t1_grad = 0
t2_grad = 0
for j in range(0,len(x_data)):
t0_grad += (1 / m) * ((t1 * x_data[j,0] + t2 * x_data[j,1] + t0) - y_data[j])
t1_grad += (1 / m) * x_data[j,0] * ((t1 * x_data[j, 0] + t2 * x_data[j, 1] + t0) - y_data[j])
t2_grad += (1 / m) * x_data[j,1] * ((t1 * x_data[j, 0] + t2 * x_data[j, 1] + t0) - y_data[j])
t0 = t0 - (lr * t0_grad)
t1 = t1 - (lr * t1_grad)
t2 = t2 - (lr * t2_grad)
return t0, t1, t2
print("Starting theta0 = {0}, theta1 = {1}, theta2 = {2}, error = {3}".
format(t0, t1, t2, compute_error(t0, t1, t2, x_data, y_data)))
print("Running...")
t0, t1, t2 = gendient_descent_runner(x_data, y_data, t0, t1, t2, lr, epochs)
print("After {0} iterations theta0 = {1}, theta1 = {2}, theta2 = {3}, error = {4}".
format(epochs, t0, t1, t2, compute_error(t0, t1, t2, x_data, y_data)))
ax = plt.figure().add_subplot(111, projection = '3d')
ax.scatter(x_data[:,0], x_data[:,1], y_data, c = 'r', marker = 'o', s = 100) #点为红色三角形
x0 = x_data[:,0]
x1 = x_data[:,1]
x0,x1 = np.meshgrid(x0,x1)
z = t0 + x0*t1 + x1*t2
ax.plot_surface(x0,x1,z)
ax.set_xlabel('Miles')
ax.set_xlabel('Num of Deliveries')
ax.set_xlabel('Time')
plt.show()
sklearn实现二元线性回归
import numpy as np
from numpy import genfromtxt
from sklearn import linear_model
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = genfromtxt(r"Delivery.csv",delimiter=',')
x_data = data[:,:-1] #切分成训练数据
y_data = data[:,-1] #
model = linear_model.LinearRegression()
model.fit(x_data, y_data)
ax = plt.figure().add_subplot(111,projection='3d')
ax.scatter(x_data[:,0],x_data[:,1], y_data,c = 'r',marker='o',s=100)
x0 = x_data[:,0]
x1 = x_data[:,1]
print(x0,x1)
x0,x1 = np.meshgrid(x0,x1) #把x0,x1变成len(x)阶矩阵,相当于x0的每一个变每一列,x1的每一个变每一行
print(x0,x1)
#系数 model.coef_
# 截距 model.intercept_
z = model.intercept_ + x0 * model.coef_[0] + x1 * model.coef_[1]
ax.plot_surface(x0,x1,z)
ax.set_xlabel('Miles')
ax.set_ylabel('Num of Deliver')
ax.set_zlabel('Time')
plt.show()