from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
加载自定义数据集
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
创建线性回归模型对象并拟合训练数据
model = LinearRegression()
model.fit(X_train, y_train)
使用训练好的模型对测试集进行预测
y_pred = model.predict(X_test)
计算预测误差
mse = mean_squared_error(y_test, y_pred)
print("均方误差:", mse)
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red')
plt.show()