算法編寫
class LinearRegression:
'''
alpha:擬合參數
fit :訓練函數,得到擬合參數
predict:預測並輸出結果
'''
def __init__(self):
pass
def fit(self,x,y):
x = np.insert(x,0,1,axis=1)
xt = np.linalg.inv(x.T.dot(x))
self.alpha=xt.dot(x.T).dot(y)
self.alpha=self.alpha.reshape(-1,1)
def predict(self,x):
try:
x=np.insert(x,0,1,axis=1)
return np.sum(self.alpha.T*x,axis=1)
except:
x=np.insert(x,0,1)
return np.sum(self.alpha.T*x)
實例演示
from sklearn import datasets
import numpy as np
data = datasets.load_diabetes()
x = data['data']
y = data['target']
line = LinearRegression()
line.fit(x,y)
line.predict(np.random.randn(10))