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()
運行結果