In [10]:
'''http://wenku.baidu.com/link?
url=Rkiuci3Z4Ox0grmTiq6cYIBaEUsH10XjwS0vY0VANS9uZx5wo5T7
g_AASZ2fyA2_mx7YZ-mQEjAeyTX8R9c2_NbZNoDH-vciZ5XnwTR5W17
'''
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
In [11]:
class LinearRegression():
def __init__(self):
self.w = None
def fit(self,X,y):
print X.shape
X = np.insert(X,0,1,axis=1)
print X.shape
X_ = np.linalg.inv(X.T.dot(X))
self.w = X_.dot(X.T).dot(y)
def predict(self,X):
X = np.insert(X,0,1,axis=1)
y_pred = X.dot(self.w)
return y_pred
In [12]:
def mean_squared_error(y_true,y_pred):
mse = np.mean(np.power(y_true - y_pred,2))
return mse
#print help(np.power)
In [19]:
def main():
diabetes = datasets.load_diabetes() #加载数据集
X = diabetes.data[:,np.newaxis,2] #np.newaxis行向量转化为列向量
print X.shape
x_train,x_test = X[:-20],X[-20:]
y_train,y_test = diabetes.target[:-20],diabetes.target[-20:]
clf = LinearRegression()
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
print 'Mean Squared Error:',mean_squared_error(y_test,y_pred)
plt.scatter(x_test[:,0],y_test,color='black')
plt.plot(x_test[:,0],y_pred,color='blue',linewidth=3)
plt.show()
In [20]:
main()
In [52]:
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
In [53]:
class LinearRegression_1:
def __init__(self):
self.w = None
def fit (self,X,y):
print X.shape
X = np.insert(X,0,1,axis=1)
print X.shape
X_ = np.linalg.inv(X.T.dot(X))
self.w = X_.dot(X.T).dot(y)
def predict(self,X):
X = np.insert(X,0,1,axis=1)
y_pred = X.dot(self.w)
return y_pred
In [54]:
def mean_square_error(y_true,y_pred):
mse = np.mean(np.power(y_true - y_pred,2))
print 'Mean Square Error:%.2f' % mse
#return mse
In [55]:
def main_1():
diabetes = datasets.load_diabetes();
diabetes_X = diabetes.data[:,np.newaxis,2]
X_train,X_test = diabetes_X[:-20],diabetes_X[-20:]
y_train,y_test = diabetes.target[:-20],diabetes.target[-20:]
lr = LinearRegression_1()
lr.fit(X_train,y_train)
y_pred = lr.predict(X_test)
mean_square_error(y_test,y_pred)
#print 'Mean Square Error:%.2f' % mean_square_error(y_true,y_pred)
plt.scatter(X_test,y_test,color='black')
plt.plot(X_test,y_pred,color='red',linewidth = 5)
plt.grid()
plt.show()
In [56]:
main_1()
In [ ]: