线性回归,简单讲就是拟合一条直线,意味着可以将输入项分别乘以一些回归系数,再将结果加起来作为输出
训练线性回归算法,就是找到回归系数
#线性回归例子
from sklearn import datasets
import numpy as np
diabetes = datasets.load_diabetes()
diabetes_X_train = diabetes.data[:-20]
diabetes_X_test = diabetes.data[-20:]
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)
print(regr.coef_) #输出线性回归的参数
# The mean square error
err = np.mean((regr.predict(diabetes_X_test)-diabetes_y_test)**2)
print(err)
# Explained variance score: 1 is perfect prediction
# and 0 means that there is no linear relationship
# between X and y.
print(regr.score(diabetes_X_test, diabetes_y_test))