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