(python线性回归) 8种用Python实现线性回归的方法对比详解
在Python中,有多种方法可以实现线性回归。下面是8种用Python实现线性回归的方法的详细解释和实现方法,每种方法都有相应的代码示例和注释信息。
- NumPy和最小二乘法(Ordinary Least Squares,OLS):
import numpy as np
# 生成示例数据
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4]])
y = np.array([2, 3, 4, 5])
# 使用最小二乘法进行线性回归
coefficients = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
# 打印回归系数
print(coefficients)
- NumPy和梯度下降法:
import numpy as np
# 生成示例数据
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4]])
y = np.array([2, 3, 4, 5])
# 设置学习率和迭代次数
learning_rate = 0.01
num_iterations = 1000
# 初始化回归系数
coefficients = np.zeros(X.shape[1])
# 梯度下降算法
for _ in range(num_iterations):
predictions = np.dot(X, coefficients)
errors = predictions - y
gradients = 2 * np.dot(X.T, errors) / len(X)
coefficients -= learning_rate * gradients
# 打印回归系数
print(coefficients)
- Scikit-learn库:
from sklearn.linear_model import LinearRegression
# 生成示例数据
X = [[1, 1], [1, 2], [1, 3], [1, 4]]
y = [2, 3, 4, 5]
# 使用Scikit-learn的线性回归模型
model = LinearRegression()
model.fit(X, y)
# 打印回归系数
print(model.coef_)
- Statsmodels库:
import statsmodels.api as sm
# 生成示例数据
X = [[1, 1], [1, 2], [1, 3], [1, 4]]
y = [2, 3, 4, 5]
# 添加常数项
X = sm.add_constant(X)
# 使用Statsmodels进行线性回归
model = sm.OLS(y, X)
results = model.fit()
# 打印回归系数
print(results.params)
- TensorFlow库:
import tensorflow as tf
# 生成示例数据
X = [[1, 1], [1, 2], [1, 3], [1, 4]]
y = [2, 3, 4, 5]
# 添加常数项
X = tf.constant(X, dtype=tf.float32)
y = tf.constant(y, dtype=tf.float32)
# 创建模型变量
coefficients = tf.Variable(tf.zeros(X.shape[1]))
# 定义损失函数和优化器
loss = tf.reduce_mean(tf.square(tf.matmul(X, coefficients) - y))
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
# 进行梯度下降优化
for _ in range(1000):
optimizer.minimize(loss, var_list=[coefficients])
# 打印回归系数
print(coefficients.numpy())
- PyTorch库:
import torch
# 生成示例数据
X = torch.tensor([[1, 1], [1, 2], [1, 3], [1, 4]], dtype=torch.float32)
y = torch.tensor([2, 3, 4, 5], dtype=torch.float32)
# 创建模型参数
coefficients = torch.zeros(X.shape[1], requires_grad=True)
# 定义损失函数和优化器
loss = torch.mean((torch.matmul(X, coefficients) - y) ** 2)
optimizer = torch.optim.SGD([coefficients], lr=0.01)
# 进行梯度下降优化
for _ in range(1000):
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 打印回归系数
print(coefficients.detach().numpy())
- Gradient Descent with Momentum(带动量的梯度下降):
import numpy as np
# 生成示例数据
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4]])
y = np.array([2, 3, 4, 5])
# 设置学习率、迭代次数和动量参数
learning_rate = 0.01
num_iterations = 1000
momentum = 0.9
# 初始化回归系数和动量
coefficients = np.zeros(X.shape[1])
velocity = np.zeros(X.shape[1])
# 梯度下降算法
for _ in range(num_iterations):
predictions = np.dot(X, coefficients)
errors = predictions - y
gradients = 2 * np.dot(X.T, errors) / len(X)
velocity = momentum * velocity - learning_rate * gradients
coefficients += velocity
# 打印回归系数
print(coefficients)
- Stochastic Gradient Descent(随机梯度下降):
import numpy as np
# 生成示例数据
X = np.array([[1, 1], [1, 2], [1, 3], [1, 4]])
y = np.array([2, 3, 4, 5])
# 设置学习率和迭代次数
learning_rate = 0.01
num_iterations = 1000
# 初始化回归系数
coefficients = np.zeros(X.shape[1])
# 随机梯度下降算法
for _ in range(num_iterations):
for i in range(len(X)):
random_index = np.random.randint(len(X))
xi = X[random_index]
yi = y[random_index]
prediction = np.dot(xi, coefficients)
error = prediction - yi
gradients = 2 * xi * error
coefficients -= learning_rate * gradients
# 打印回归系数
print(coefficients)
以上是8种用Python实现线性回归的方法的详细解释和实现方法。每种方法都有不同的优缺点和适用场景,具体选择哪种方法取决于你的需求和数据特征。
(argmax函数) 详解Numpy argmax()(返回数组元素的最大值的索引)函数的作用与使用方法 Numpy argmax() 函数概述 全网首发(图文详解1)
(多项式拟合) 详解Numpy polyfit()(多项式拟合)函数的作用与使用方法 如何使用 NumPy 的 polyfit 函数 全网首发(图文详解1)