集成学习笔记02 使用sklearn构建基本的机器学习模型
学习参考datawhale
1.线性回归模型
sklearn内有关于线性回归的函数LinearRegression(),简单的线性回归代码如下:
import pandas as pd
from sklearn import datasets
boston = datasets.load_boston() # 返回一个类似于字典的类
X = boston.data
y = boston.target
features = boston.feature_names
boston_data = pd.DataFrame(X,columns=features)
boston_data["Price"] = y
boston_data.head()
from sklearn import linear_model # 引入线性回归方法
lin_reg = linear_model.LinearRegression() # 创建线性回归的类
lin_reg.fit(X,y) # 输入特征X和因变量y进行训练
print("模型系数:",lin_reg.coef_) # 输出模型的系数
print("模型得分:",lin_reg.score(X,y)) # 输出模型的决定系数R^2
输出:
模型系数: [-1.08011358e-01 4.64204584e-02 2.05586264e-02 2.68673382e+00
-1.77666112e+01 3.80986521e+00 6.92224640e-04 -1.47556685e+00
3.06049479e-01 -1.23345939e-02 -9.52747232e-01 9.31168327e-03
-5.24758378e-01]
模型得分: 0.7406426641094095
1.1多项式回归
诸如式(1)形态的多项式函数:
y i = w 0 + w 1 ∗ x i + w 2 ∗ x i 2 + . . . . + w d x i d + e y_i= w_0+w_1*x_i+w_2*x_i^2+....+w_dx_i^d+e yi=w0+w1∗xi+w2∗xi2