【342】Linear Regression by Python

本文详细介绍了如何利用Python中的scikit-learn库和pandas库来实现线性回归模型。首先,通过pandas加载数据并将其划分为特征X和目标变量y,然后使用scikit-learn的LinearRegression类拟合模型,最后获取截距和系数。通过两个实例演示了这一过程,为初学者提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Reference: 用scikit-learn和pandas学习线性回归

  • 首先获取数据存储在 pandas.DataFrame 中,获取途径(CSV 文件、Numpy 创建)
  • 将数据分成 X 和 y,X 可以含有多列,也就是多个参数
  • 通过 Linear Regression 计算
  • 获取 intercept 和 coefficient

实现步骤如下:

>>> import numpy as np
>>> import pandas as pd
>>> from sklearn import datasets, linear_model
>>> from sklearn.linear_model import LinearRegression
>>> data = np.array([
	['X', 'y'],
	[2.0, 2.2],
	[3.0, 3.8],
	[4.0, 5.5],
	[5.0, 6.5],
	[6.0, 7.0]])
>>> lr = pd.DataFrame(data=data[1:,:],
		      columns=data[0,:])
>>> lr
     X    y
0  2.0  2.2
1  3.0  3.8
2  4.0  5.5
3  5.0  6.5
4  6.0  7.0
>>> X = lr[['X']]
>>> X
     X
0  2.0
1  3.0
2  4.0
3  5.0
4  6.0
>>> y = lr[['y']]
>>> y
     y
0  2.2
1  3.8
2  5.5
3  6.5
4  7.0
>>> linreg = LinearRegression()
>>> linreg.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
>>> linreg.intercept_
array([0.08])
>>> linreg.coef_
array([[1.23]])


代码如下:

>>> data = np.array([[3,2.5],[4,3],[5,4],[6,4.5]])
>>> lr = pd.DataFrame(data)
>>> X = lr[[0]]
>>> y = lr[[1]]
>>> linReg = LinearRegression()
>>> linReg.fit(X, y)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
         normalize=False)
>>> linReg.intercept_
array([0.35])
>>> linReg.coef_
array([[0.7]])

 

Here is an example implementation of a linear regression model using PyTorch and Autograd for optimization: ```python import torch import numpy as np # Generate some random data np.random.seed(42) x = np.random.rand(100, 1) y = 2 + 3 * x + 0.1 * np.random.randn(100, 1) # Convert data to PyTorch tensors x_tensor = torch.from_numpy(x).float() y_tensor = torch.from_numpy(y).float() # Define the model class LinearRegression(torch.nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear = torch.nn.Linear(1, 1) def forward(self, x): return self.linear(x) model = LinearRegression() # Define the loss function criterion = torch.nn.MSELoss() # Define the optimizer optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # Train the model num_epochs = 1000 for epoch in range(num_epochs): # Forward pass y_pred = model(x_tensor) loss = criterion(y_pred, y_tensor) # Backward pass and optimization optimizer.zero_grad() loss.backward() optimizer.step() # Print progress if (epoch+1) % 100 == 0: print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item())) # Print the learned parameters w, b = model.parameters() print('w =', w.item()) print('b =', b.item()) ``` In this example, we define a linear regression model as a subclass of `torch.nn.Module`, with a single linear layer. We use the mean squared error loss function and stochastic gradient descent optimizer to train the model on the randomly generated data. The model parameters are learned through backpropagation using the `backward()` method, and are optimized using the `step()` method of the optimizer. After training, we print the learned values of the slope and intercept parameters.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值