算法编写
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))