机器学习入门:线性回归算法
线性回归是机器学习中最基础的算法之一,也是理解更复杂算法的基石。
简单线性回归
简单线性回归使用单个特征来预测目标值:
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
# 创建示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# 创建并训练模型
model = LinearRegression()
model.fit(X, y)
# 预测
prediction = model.predict([[6]])
print(f'预测结果: {prediction[0]:.2f}')
多元线性回归
当有多个特征时,使用多元线性回归:
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 多特征数据
X_multi = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
y_multi = np.array([3, 5, 7, 9, 11])
# 分割数据
X_train, X_test, y_train, y_test = train_test_split(
X_multi, y_multi, test_size=0.2, random_state=42
)
# 训练模型
model_multi = LinearRegression()
model_multi.fit(X_train, y_train)
# 评估模型
y_pred = model_multi.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print(f'均方误差: {mse:.2f}')
模型评估指标
常用的回归模型评估指标:
- R² (决定系数):衡量模型解释数据变异的程度
- 均方误差(MSE):预测值与实际值差的平方的平均值
- 平均绝对误差(MAE):预测值与实际值差的绝对值的平均值
线性回归是理解和应用机器学习的重要起点。