import matplotlib.pyplot as plt
import numpy as np
#读取的是sklearn自带的数据集
from sklearn import datasets
class LinearRegression():
def __init__(self):
self.w = None
def fit(self, X, y):
#在第0列填充1
X = np.insert(X, 0, 1, axis=1)
print(X.shape)
#X.T.dot(X) 求逆运算 没有考虑矩阵的逆不存在的情况
X_ = np.linalg.inv(X.T.dot(X))
self.w = X_.dot(X.T).dot(y)
def predict(self, X):
# Insert constant ones for bias weights
X = np.insert(X, 0, 1, axis=1)
y_pred = X.dot(self.w)
return y_pred
def mean_squared_error(y_true, y_pred):
#np.power数组元素求n次方
mse = np.mean(np.power(y_true - y_pred, 2))
return mse
def main():
# Load the diabetes dataset
diabetes = datasets.load_diabetes()
#print(diabetes)
#diabetes没有shape的属性
#print(diabetes.shape) AttributeError: shape
# Use only one feature
#X = diabetes.data[:, 2]直接取到的是一个一维的数据,要把它变成n*1二维数组的形式,需在列上增加维度
X = diabetes.data[:, np.newaxis, 2]
print (X.shape)
# Split the data into training/testing sets
#X[:-20]从头开始到倒数第20行
x_train, x_test = X[:-20], X[-20:]
# Split the targets into training/testing sets
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 the mean squared error
print ("Mean Squared Error:", mean_squared_error(y_test, y_pred))
# Plot the results
plt.scatter(x_test[:,0], y_test, color='black')
plt.plot(x_test[:,0], y_pred, color='y', linewidth=3)
plt.show()
执行main函数:main()
运行结果