线性回归问题
案例:根据工资、年龄来预估贷款额
假设额度服从工资和年龄的线性分布
最终加上误差,得到最后的模型公式:
现在把误差项解出,然后对它进行展开、处理如下:
然后求解极大似然估计,整理得到J
最大似然函数的求解等价于最小化J,解出
在以后的模型使用中,直接使用结果即可
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
class LinearRegession():
def __init__(self):
self.w = None
def fit(self,X,y):
print(X.shape)
# 插入一个偏置项X的第0项为1(insert constant ones for bias weights)
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
def mean_squared_error(y_true,y_pred):
mse = np.mean(np.power(y_true - y_pred),2))
return mse
def main():
diabetes = datasets.load.diabetes()
X = diabetes.data[:,np.newaxis,2]
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 Souared 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()
if __name__ = "__main__":
main()