给出训练模型的示例代码
以下是一个简单的线性回归模型训练示例代码:
import numpy as np
from sklearn.linear_model import LinearRegression
# 生成随机数据
np.random.seed(42)
X = np.random.rand(100, 1)
y = 2 + 3 * X + np.random.randn(100, 1)
# 创建模型
model = LinearRegression()
# 训练模型
model.fit(X, y)
# 打印模型参数
print("Intercept:", model.intercept_)
print("Coefficient:", model.coef_)
该代码生成了一个随机的数据集,并使用sklearn中的LinearRegression来创建和训练一个线性回归模型。最后,它打印出训练后的模型的截距和系数。
原文地址: https://www.cveoy.top/t/topic/fsJT 著作权归作者所有。请勿转载和采集!