下面是一个简单的sklearn线性回归代码样例:

from sklearn.linear_model import LinearRegression
import numpy as np

# 构造数据集
X = np.array([[1],[2],[3],[4],[5]])
y = np.array([2,4,5,4,5])

# 定义模型
model = LinearRegression()

# 拟合数据
model.fit(X, y)

# 输出模型参数
print('Coefficients: ', model.coef_)
print('Intercept: ', model.intercept_)

输出结果为:

Coefficients:  [0.6]
Intercept:  2.0

这说明模型的拟合方程为 y = 0.6x + 2.0。


原文地址: https://www.cveoy.top/t/topic/br5M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录