慕粉8371238
2019-07-28 19:30
from numpy import * from numpy.linalg import inv import operator import pandas as pd dataset = pd.read_csv('data.csv') temp=dataset.iloc[:,0:3] Y=dataset.iloc[:,3] X=temp X['b']=1 #print (Y) ##最小二乘 #theta=inv(X.T*X)*X.T*Y theta=dot(dot(inv(dot(X.T,X)),X.T),Y) print(theta) #梯度下降 alpha=0.01 theta1=array([1., 1., 1., 1.]).reshape(4, 1) X1=X.iloc[:, 0] X2=X.iloc[:, 1] X3=X.iloc[:, 2] B=X.iloc[:, 3] t=theta1 print(X2) for i in range(10000): t[0] = theta1[0] - alpha * sum((dot(X, theta1)-Y)*X1)/200. t[1] = theta1[1] - alpha * sum((dot(X, theta1) - Y) * X2) / 200. t[2] = theta1[2] - alpha * sum((dot(X, theta1) - Y) * X3) / 200. t[3] = theta1[3] - alpha * sum((dot(X, theta1) - Y) * B) / 200. theta1 = t
dot(X, theta1)返回的是一个 200 * 1 矩阵,而 Y不是一个200 * 1的矩阵,所以需要对Y进行重新规划, Y = Y.values.reshape(200, 1)就可以了
Python实现线性回归
26153 学习 · 36 问题
相似问题