昨天学习了一元的线性回归,今天看了下多元的,数据是从这里拿的http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv
from sklearn.linear_model import LinearRegression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv('C:/pyproject/Advertising.csv')
y=data.loc[:, 'sales'].as_matrix(columns=None)
y=np.array([y]).T
#print(y)
x=data.drop('sales', 1)
x=x.loc[0:].as_matrix(columns=None)
#print(x)
l=LinearRegression()
l.fit(x,y)
print(l.coef_)
print(l.predict([[60,60,60]]))
print(l.score(x,y))
print(np.mean((l.predict(x)-y)**2))
多元就相当于是k1x1+k2x2+.....knxn=b,最后我们打印出的是系数,某个数据的预测值,相关系数R,均方误差
结果如下: