编写具体的基于岭回归实现空气质量预测的python代码
时间: 2024-01-26 08:03:00 浏览: 67
ridge_regression:用于岭回归的python代码(已实现以预测下个月的CO2浓度)
5星 · 资源好评率100%
好的,以下是基于岭回归实现空气质量预测的Python代码示例:
```python
# 导入必要的库
import pandas as pd
import numpy as np
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 读取数据集
data = pd.read_csv('air_quality.csv')
# 特征选择
features = ['PM10', 'PM2.5', 'NO2', 'SO2', 'O3']
# 划分特征和标签
X = data[features]
y = data['AQI']
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 岭回归模型训练
ridge = Ridge(alpha=0.1) # alpha为正则化参数
ridge.fit(X_train, y_train)
# 预测测试集结果
y_pred = ridge.predict(X_test)
# 模型评估
mse = mean_squared_error(y_test, y_pred) # 均方误差
rmse = np.sqrt(mse) # 均方根误差
r2 = ridge.score(X_test, y_test) # 决定系数
# 输出模型评估结果
print('MSE:', mse)
print('RMSE:', rmse)
print('R^2:', r2)
```
其中,数据集文件为'air_quality.csv',特征选择为['PM10', 'PM2.5', 'NO2', 'SO2', 'O3'],岭回归模型的正则化参数alpha设为0.1。
阅读全文