多元线性回归模型python
时间: 2023-07-02 10:10:54 浏览: 100
在Python中,可以使用多个库来构建多元线性回归模型。其中最常用的是Scikit-learn和Statsmodels库。
Scikit-learn:
```python
from sklearn.linear_model import LinearRegression
# 假设有3个自变量x1,x2,x3和1个因变量y
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = [10, 20, 30]
# 创建线性回归模型
model = LinearRegression()
# 拟合模型
model.fit(X, y)
# 预测新数据
new_X = [[2, 4, 6], [3, 5, 7]]
predictions = model.predict(new_X)
```
Statsmodels:
```python
import statsmodels.api as sm
# 假设有3个自变量x1,x2,x3和1个因变量y
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = [10, 20, 30]
# 添加常数项
X = sm.add_constant(X)
# 创建线性回归模型
model = sm.OLS(y, X)
# 拟合模型
results = model.fit()
# 打印模型摘要
print(results.summary())
# 预测新数据
new_X = [[2, 4, 6], [3, 5, 7]]
new_X = sm.add_constant(new_X)
predictions = results.predict(new_X)
```
以上是两种常用的多元线性回归模型的Python实现。需要注意的是,多元线性回归模型在使用之前需要对数据进行预处理,例如特征缩放、数据清洗等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)