first commit
This commit is contained in:
commit
6ae57ad9f2
467
.ipynb_checkpoints/air_quality_prediction-checkpoint.ipynb
Normal file
467
.ipynb_checkpoints/air_quality_prediction-checkpoint.ipynb
Normal file
@ -0,0 +1,467 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"# 预测建模\n",
|
||||||
|
"北京市空气质量指数预测(推荐难度系数10)\n",
|
||||||
|
"\n",
|
||||||
|
"这个数据集是北京市2022年11月1日至2023年10月31日期间空气质量相关数据。\n",
|
||||||
|
"根据这个数据集,回答以下问题"
|
||||||
|
],
|
||||||
|
"id": "b610f839dca4877"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"id": "initial_id",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true,
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:04.926730Z",
|
||||||
|
"start_time": "2025-03-22T07:55:03.071940Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"import pandas as pd\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import matplotlib.pyplot as plt\n",
|
||||||
|
"from calculate import *\n",
|
||||||
|
"from heatmap import *"
|
||||||
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:05.632142Z",
|
||||||
|
"start_time": "2025-03-22T07:55:04.941177Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"#读取数据\n",
|
||||||
|
"data=pd.read_excel('北京市空气质量指数与气象数据.xlsx')\n",
|
||||||
|
"data.head()"
|
||||||
|
],
|
||||||
|
"id": "92ea7ba1218799cd",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
" date hour AQI CO NO2 O3 PM10 \\\n",
|
||||||
|
"0 2022-11-01 2 18.371429 0.211429 23.771429 29.057143 13.257143 \n",
|
||||||
|
"1 2022-11-01 5 21.914286 0.180000 26.571429 20.142857 18.914286 \n",
|
||||||
|
"2 2022-11-01 8 28.628571 0.311429 30.028571 14.285714 27.942857 \n",
|
||||||
|
"3 2022-11-01 11 19.000000 0.237143 17.971429 40.529412 17.852941 \n",
|
||||||
|
"4 2022-11-01 14 21.742857 0.252941 15.588235 53.617647 20.941176 \n",
|
||||||
|
"\n",
|
||||||
|
" PM2.5 SO2 T ... P Pa U Ff Tn Tx VV Td \\\n",
|
||||||
|
"0 3.057143 2.628571 6.7 ... 770.5 0.1 36.0 1.0 5.3 17.3 30.0 -7.3 \n",
|
||||||
|
"1 3.771429 2.542857 2.0 ... 770.8 0.3 62.0 0.0 1.9 17.3 7.0 -4.5 \n",
|
||||||
|
"2 6.857143 2.400000 6.6 ... 771.7 0.9 56.0 0.0 0.9 17.3 10.0 -7.1 \n",
|
||||||
|
"3 5.914286 2.176471 13.5 ... 771.3 -0.4 19.0 2.0 0.9 17.3 30.0 -9.7 \n",
|
||||||
|
"4 6.742857 2.000000 15.7 ... 768.6 -2.7 19.0 2.0 0.9 17.3 30.0 -7.9 \n",
|
||||||
|
"\n",
|
||||||
|
" RRR tR \n",
|
||||||
|
"0 0.0 12 \n",
|
||||||
|
"1 0.0 12 \n",
|
||||||
|
"2 0.0 12 \n",
|
||||||
|
"3 0.0 12 \n",
|
||||||
|
"4 0.0 12 \n",
|
||||||
|
"\n",
|
||||||
|
"[5 rows x 21 columns]"
|
||||||
|
],
|
||||||
|
"text/html": [
|
||||||
|
"<div>\n",
|
||||||
|
"<style scoped>\n",
|
||||||
|
" .dataframe tbody tr th:only-of-type {\n",
|
||||||
|
" vertical-align: middle;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe tbody tr th {\n",
|
||||||
|
" vertical-align: top;\n",
|
||||||
|
" }\n",
|
||||||
|
"\n",
|
||||||
|
" .dataframe thead th {\n",
|
||||||
|
" text-align: right;\n",
|
||||||
|
" }\n",
|
||||||
|
"</style>\n",
|
||||||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||||||
|
" <thead>\n",
|
||||||
|
" <tr style=\"text-align: right;\">\n",
|
||||||
|
" <th></th>\n",
|
||||||
|
" <th>date</th>\n",
|
||||||
|
" <th>hour</th>\n",
|
||||||
|
" <th>AQI</th>\n",
|
||||||
|
" <th>CO</th>\n",
|
||||||
|
" <th>NO2</th>\n",
|
||||||
|
" <th>O3</th>\n",
|
||||||
|
" <th>PM10</th>\n",
|
||||||
|
" <th>PM2.5</th>\n",
|
||||||
|
" <th>SO2</th>\n",
|
||||||
|
" <th>T</th>\n",
|
||||||
|
" <th>...</th>\n",
|
||||||
|
" <th>P</th>\n",
|
||||||
|
" <th>Pa</th>\n",
|
||||||
|
" <th>U</th>\n",
|
||||||
|
" <th>Ff</th>\n",
|
||||||
|
" <th>Tn</th>\n",
|
||||||
|
" <th>Tx</th>\n",
|
||||||
|
" <th>VV</th>\n",
|
||||||
|
" <th>Td</th>\n",
|
||||||
|
" <th>RRR</th>\n",
|
||||||
|
" <th>tR</th>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </thead>\n",
|
||||||
|
" <tbody>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>0</th>\n",
|
||||||
|
" <td>2022-11-01</td>\n",
|
||||||
|
" <td>2</td>\n",
|
||||||
|
" <td>18.371429</td>\n",
|
||||||
|
" <td>0.211429</td>\n",
|
||||||
|
" <td>23.771429</td>\n",
|
||||||
|
" <td>29.057143</td>\n",
|
||||||
|
" <td>13.257143</td>\n",
|
||||||
|
" <td>3.057143</td>\n",
|
||||||
|
" <td>2.628571</td>\n",
|
||||||
|
" <td>6.7</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>770.5</td>\n",
|
||||||
|
" <td>0.1</td>\n",
|
||||||
|
" <td>36.0</td>\n",
|
||||||
|
" <td>1.0</td>\n",
|
||||||
|
" <td>5.3</td>\n",
|
||||||
|
" <td>17.3</td>\n",
|
||||||
|
" <td>30.0</td>\n",
|
||||||
|
" <td>-7.3</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>12</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>1</th>\n",
|
||||||
|
" <td>2022-11-01</td>\n",
|
||||||
|
" <td>5</td>\n",
|
||||||
|
" <td>21.914286</td>\n",
|
||||||
|
" <td>0.180000</td>\n",
|
||||||
|
" <td>26.571429</td>\n",
|
||||||
|
" <td>20.142857</td>\n",
|
||||||
|
" <td>18.914286</td>\n",
|
||||||
|
" <td>3.771429</td>\n",
|
||||||
|
" <td>2.542857</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>770.8</td>\n",
|
||||||
|
" <td>0.3</td>\n",
|
||||||
|
" <td>62.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>1.9</td>\n",
|
||||||
|
" <td>17.3</td>\n",
|
||||||
|
" <td>7.0</td>\n",
|
||||||
|
" <td>-4.5</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>12</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>2</th>\n",
|
||||||
|
" <td>2022-11-01</td>\n",
|
||||||
|
" <td>8</td>\n",
|
||||||
|
" <td>28.628571</td>\n",
|
||||||
|
" <td>0.311429</td>\n",
|
||||||
|
" <td>30.028571</td>\n",
|
||||||
|
" <td>14.285714</td>\n",
|
||||||
|
" <td>27.942857</td>\n",
|
||||||
|
" <td>6.857143</td>\n",
|
||||||
|
" <td>2.400000</td>\n",
|
||||||
|
" <td>6.6</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>771.7</td>\n",
|
||||||
|
" <td>0.9</td>\n",
|
||||||
|
" <td>56.0</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>0.9</td>\n",
|
||||||
|
" <td>17.3</td>\n",
|
||||||
|
" <td>10.0</td>\n",
|
||||||
|
" <td>-7.1</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>12</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>3</th>\n",
|
||||||
|
" <td>2022-11-01</td>\n",
|
||||||
|
" <td>11</td>\n",
|
||||||
|
" <td>19.000000</td>\n",
|
||||||
|
" <td>0.237143</td>\n",
|
||||||
|
" <td>17.971429</td>\n",
|
||||||
|
" <td>40.529412</td>\n",
|
||||||
|
" <td>17.852941</td>\n",
|
||||||
|
" <td>5.914286</td>\n",
|
||||||
|
" <td>2.176471</td>\n",
|
||||||
|
" <td>13.5</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>771.3</td>\n",
|
||||||
|
" <td>-0.4</td>\n",
|
||||||
|
" <td>19.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>0.9</td>\n",
|
||||||
|
" <td>17.3</td>\n",
|
||||||
|
" <td>30.0</td>\n",
|
||||||
|
" <td>-9.7</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>12</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" <tr>\n",
|
||||||
|
" <th>4</th>\n",
|
||||||
|
" <td>2022-11-01</td>\n",
|
||||||
|
" <td>14</td>\n",
|
||||||
|
" <td>21.742857</td>\n",
|
||||||
|
" <td>0.252941</td>\n",
|
||||||
|
" <td>15.588235</td>\n",
|
||||||
|
" <td>53.617647</td>\n",
|
||||||
|
" <td>20.941176</td>\n",
|
||||||
|
" <td>6.742857</td>\n",
|
||||||
|
" <td>2.000000</td>\n",
|
||||||
|
" <td>15.7</td>\n",
|
||||||
|
" <td>...</td>\n",
|
||||||
|
" <td>768.6</td>\n",
|
||||||
|
" <td>-2.7</td>\n",
|
||||||
|
" <td>19.0</td>\n",
|
||||||
|
" <td>2.0</td>\n",
|
||||||
|
" <td>0.9</td>\n",
|
||||||
|
" <td>17.3</td>\n",
|
||||||
|
" <td>30.0</td>\n",
|
||||||
|
" <td>-7.9</td>\n",
|
||||||
|
" <td>0.0</td>\n",
|
||||||
|
" <td>12</td>\n",
|
||||||
|
" </tr>\n",
|
||||||
|
" </tbody>\n",
|
||||||
|
"</table>\n",
|
||||||
|
"<p>5 rows × 21 columns</p>\n",
|
||||||
|
"</div>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"## 题目1\n",
|
||||||
|
"研究单日内空气质量指数与各项指标的变化趋势,这种趋势是否具有周期性?"
|
||||||
|
],
|
||||||
|
"id": "bca65e544d8bef55"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:05.749697Z",
|
||||||
|
"start_time": "2025-03-22T07:55:05.746320Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"#数据预处理:将数据按小时分组,计算每个小时各指标的平均值\n",
|
||||||
|
"\n",
|
||||||
|
"#可视化:绘制各指标小时均值的折线图,观察是否存在规律性波动\n"
|
||||||
|
],
|
||||||
|
"id": "5f8e89a8d1561e4f",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:05.777089Z",
|
||||||
|
"start_time": "2025-03-22T07:55:05.774038Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "#ACF检验周期性\n",
|
||||||
|
"id": "4521bfa63d480997",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"## 题目2\n",
|
||||||
|
"简述各项指标间的相互关系。"
|
||||||
|
],
|
||||||
|
"id": "59e20f3463e819a6"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:05.992326Z",
|
||||||
|
"start_time": "2025-03-22T07:55:05.988969Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"#计算相关系数矩阵\n",
|
||||||
|
"\n",
|
||||||
|
"#绘制热力图\n"
|
||||||
|
],
|
||||||
|
"id": "c917d14115569bcd",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:06.153442Z",
|
||||||
|
"start_time": "2025-03-22T07:55:06.150747Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "#因子分析(PCA)\n",
|
||||||
|
"id": "509d783a82bbdcb2",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 6
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:06.261340Z",
|
||||||
|
"start_time": "2025-03-22T07:55:06.258833Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "#多元线性回归(我试试玩的)\n",
|
||||||
|
"id": "bb2d87337f46df",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 7
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"## 题目3\n",
|
||||||
|
"令2022年11月1日至2023年9月30日的空气质量数据为训练集,剩余数据为测试集。基于训练集,尝试使用两种不同的方法构建空气质量指数预测模型,并在测试集上测试。比较所选模型的预测效果。"
|
||||||
|
],
|
||||||
|
"id": "3f89fa62a897a3e3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:06.414915Z",
|
||||||
|
"start_time": "2025-03-22T07:55:06.410784Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": "#数据划分:训练集:2022-11-01至2023-09-30,测试集:2023-10-1至2023-10-31。\n",
|
||||||
|
"id": "d1bdac1e4e1562f2",
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": 8
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": "### (1)SARIMA模型",
|
||||||
|
"id": "75bc1cfcc85f60a7"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:06.452015Z",
|
||||||
|
"start_time": "2025-03-22T07:55:06.446830Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
"该模型在假设不知道测试集其他指标的情况下,仅使用AQI历史数据预测未来AQI\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"#训练模型\n",
|
||||||
|
"\n",
|
||||||
|
"#输出预测与实际AQI的对比图\n",
|
||||||
|
"\n",
|
||||||
|
"#计算拟合度\n"
|
||||||
|
],
|
||||||
|
"id": "24996a0c06820cdc",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"'\\n该模型在假设不知道测试集其他指标的情况下,仅使用AQI历史数据预测未来AQI\\n'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 9
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": "### (2)XGBOOST模型",
|
||||||
|
"id": "ebe88094b6c13e0c"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-03-22T07:55:06.482520Z",
|
||||||
|
"start_time": "2025-03-22T07:55:06.477496Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"cell_type": "code",
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
"该模型在同样未知测试集其他指标的情况下,考虑到训练集的多种参数预测未来AQI\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"#训练模型\n",
|
||||||
|
"\n",
|
||||||
|
"#输出预测与实际AQI的对比图\n",
|
||||||
|
"\n",
|
||||||
|
"#计算拟合度\n"
|
||||||
|
],
|
||||||
|
"id": "66f104e110aba36",
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"'\\n该模型在同样未知测试集其他指标的情况下,考虑到训练集的多种参数预测未来AQI\\n'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"execution_count": 10
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
BIN
25春统计分析软件大作业-v1.0.docx
Normal file
BIN
25春统计分析软件大作业-v1.0.docx
Normal file
Binary file not shown.
BIN
XG_pred_by_recursion.png
Normal file
BIN
XG_pred_by_recursion.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 85 KiB |
BIN
XG_pred_by_step.png
Normal file
BIN
XG_pred_by_step.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 287 KiB |
BIN
XG_top_importance.png
Normal file
BIN
XG_top_importance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
BIN
__pycache__/calculate.cpython-312.pyc
Normal file
BIN
__pycache__/calculate.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/heatmap.cpython-312.pyc
Normal file
BIN
__pycache__/heatmap.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/sort_matrix.cpython-312.pyc
Normal file
BIN
__pycache__/sort_matrix.cpython-312.pyc
Normal file
Binary file not shown.
1278
air_quality_prediction.ipynb
Normal file
1278
air_quality_prediction.ipynb
Normal file
File diff suppressed because one or more lines are too long
26
calculate.py
Normal file
26
calculate.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def cal_metrics(estimated, true):
|
||||||
|
"""
|
||||||
|
这是一个用来计算拟合度指标的函数
|
||||||
|
"""
|
||||||
|
|
||||||
|
estimated = np.array(estimated)
|
||||||
|
true = np.array(true)
|
||||||
|
|
||||||
|
#RMSE
|
||||||
|
RMSE = np.sqrt(np.mean((estimated - true) ** 2))
|
||||||
|
|
||||||
|
#R-squared
|
||||||
|
SS_res = np.sum((true - estimated) ** 2)
|
||||||
|
SS_tot = np.sum((true - np.mean(true)) ** 2)
|
||||||
|
R_squared = 1 - (SS_res / SS_tot) if SS_tot != 0 else 0
|
||||||
|
|
||||||
|
#MAE
|
||||||
|
MAE = np.mean(np.abs(estimated - true))
|
||||||
|
|
||||||
|
return {
|
||||||
|
'RMSE': RMSE,
|
||||||
|
'R-squared': R_squared,
|
||||||
|
'MAE': MAE
|
||||||
|
}
|
BIN
components_heatmap.png
Normal file
BIN
components_heatmap.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 80 KiB |
BIN
correlation_heatmap.png
Normal file
BIN
correlation_heatmap.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 253 KiB |
29
heatmap.py
Normal file
29
heatmap.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import seaborn as sns
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
def plot_heatmap(matrix, len, wid, title="Heatmap", save_path="heatmap.png"):
|
||||||
|
# 设置支持中文的字体
|
||||||
|
plt.rcParams['font.family'] = 'SimHei'
|
||||||
|
|
||||||
|
# 解决负号显示问题
|
||||||
|
plt.rcParams['axes.unicode_minus'] = False
|
||||||
|
|
||||||
|
# 读取数据
|
||||||
|
df = matrix
|
||||||
|
|
||||||
|
# 绘制热力图
|
||||||
|
plt.figure(figsize=(len, wid))
|
||||||
|
sns.heatmap(
|
||||||
|
df,
|
||||||
|
annot=True, # 显示数值
|
||||||
|
fmt=".2f", # 数值格式
|
||||||
|
cmap="coolwarm", # 红-蓝渐变色
|
||||||
|
vmin=-1, vmax=1, # 颜色范围固定为-1到1
|
||||||
|
linewidths=0.5,
|
||||||
|
)
|
||||||
|
plt.title(title)
|
||||||
|
plt.xticks(rotation=45, ha="right") # 调整X轴标签角度
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.savefig(save_path)
|
||||||
|
plt.show()
|
||||||
|
plt.close()
|
23
readme.txt
Normal file
23
readme.txt
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
注意事项:
|
||||||
|
1. 在6个项目中,任选一个使用R或Python编程完成。
|
||||||
|
2. 组队完成,每队2-3人,按规定日期提交。
|
||||||
|
3. 项目编号对应数据所在文件夹。
|
||||||
|
4. 总得分(满分100分)=基础分60分(所展现代码水平)+难度系数×完成度(10×4=40分)
|
||||||
|
|
||||||
|
1、收集平台:北京市生态环境检测中心和rp5.ru气象网站
|
||||||
|
2、采集时间:2022年11月1日至2023年10月31日
|
||||||
|
3、样本量:2920
|
||||||
|
4、变量含义:
|
||||||
|
AQI:空气质量指数,值越高代表污染越严重
|
||||||
|
T:地面以上2米处的大气摄氏度
|
||||||
|
Po:气象站水平的大气压(毫米汞柱)
|
||||||
|
P:平均海平面的大气压(毫米汞柱)
|
||||||
|
Pa:气压趋势:观测前三小时内大气压的变化(毫米汞柱)
|
||||||
|
U:地面高度2米处的相对湿度(%)
|
||||||
|
Ff:观测前10分钟内地面高度10-12米处的平均风速(米每秒)
|
||||||
|
Tn:在过去一段时间(不超过12小时)的最低气温(摄氏度)
|
||||||
|
Tx:在过去一段时间(不超过12小时)的最高气温(摄氏度)
|
||||||
|
VV:水平能见度(千米)
|
||||||
|
Td:地面高度2米处的零点温度(摄氏度)
|
||||||
|
RRR:降水量(毫米)
|
||||||
|
tR:达到规定降水量的时间
|
36
sort_matrix.py
Normal file
36
sort_matrix.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import pandas as pd
|
||||||
|
def sort_matrix_by_diag (rotated_components_df):
|
||||||
|
# 调整因子列顺序:按方差解释降序排列
|
||||||
|
variance_explained = (rotated_components_df ** 2).sum()
|
||||||
|
column_order = variance_explained.sort_values(ascending=False).index.tolist()
|
||||||
|
rotated_components_df = rotated_components_df[column_order]
|
||||||
|
|
||||||
|
# 对载荷矩阵进行排序
|
||||||
|
sorted_loadings = rotated_components_df.copy()
|
||||||
|
|
||||||
|
# 确定每个变量的主因子(绝对值最大的列)
|
||||||
|
sorted_loadings['主因子'] = sorted_loadings.abs().idxmax(axis=1)
|
||||||
|
|
||||||
|
# 将主因子转换为按列顺序的分类变量,确保排序正确
|
||||||
|
sorted_loadings['主因子'] = pd.Categorical(
|
||||||
|
sorted_loadings['主因子'],
|
||||||
|
categories=column_order,
|
||||||
|
ordered=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# 提取每个变量在其主因子上的载荷绝对值
|
||||||
|
sorted_loadings['绝对值'] = sorted_loadings.apply(
|
||||||
|
lambda row: abs(row[row['主因子']]),
|
||||||
|
axis=1
|
||||||
|
)
|
||||||
|
|
||||||
|
# 按主因子顺序和绝对值降序排序
|
||||||
|
sorted_loadings = sorted_loadings.sort_values(
|
||||||
|
by=['主因子', '绝对值'],
|
||||||
|
ascending=[True, False]
|
||||||
|
)
|
||||||
|
|
||||||
|
# 删除临时列并输出结果
|
||||||
|
sorted_loadings = sorted_loadings.drop(['主因子', '绝对值'], axis=1)
|
||||||
|
|
||||||
|
return sorted_loadings
|
BIN
北京市空气质量指数与气象数据.xlsx
Normal file
BIN
北京市空气质量指数与气象数据.xlsx
Normal file
Binary file not shown.
54
模型分析解读(论文参考这个).md
Normal file
54
模型分析解读(论文参考这个).md
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
## 热力图解读
|
||||||
|
热力图显示了各指标之间的相关性。颜色越深表示相关性越强,颜色越浅表示相关性越弱,偏红色表示正相关,偏蓝色表示负相关。
|
||||||
|
1. 图形大致可分为四个部分:
|
||||||
|
- 左上角的颜色较深的矩形反映了AQI与数个观测指标(污染物)的关系。
|
||||||
|
- 右上侧有一些颜色较深的区块,可能反映了污染物(如CO、NO2、O3)浓度与环境因素(如温度、湿度、风速)的相关关系。
|
||||||
|
- 中心与中心正右侧的深色区块反映温度与气压间的强负相关关系。
|
||||||
|
- 右下角的颜色较深的矩形主要反映各环境指标间的相关关系。
|
||||||
|
2. 空气质量指数(AQI)与PM2.5、PM10有很强正相关关系,与CO、NO2、SO2呈现较强正相关关系。同时跟VV(水平能见度)有较强负相关关系。后者的原因显然。经过查阅资料,前者数个指标本就为AQI的计算所考虑的指标,而同为考虑指标的O3相关性低,不知道为什么,需要进一步调研。
|
||||||
|
3. 小时(hour)与O3等指标呈现一定正相关关系,这或许反映O3浓度变化具有日周期。且与U(地面高度2米处的相对湿度)等指标呈现一定负相关关系。
|
||||||
|
## 主成分分析解读
|
||||||
|
1. 检验指标:
|
||||||
|
- KMO值: 0.762>0.7。
|
||||||
|
- 巴赫利特检验卡方值: 90424.712, p值: 0.0,显著。
|
||||||
|
- 提取5个特征值大于1的因子作为主成分,累积方差贡献率为78.89%。
|
||||||
|
- 检验效果较好,说明数据适合主成分分析降维。
|
||||||
|
2. 旋转载荷矩阵解读:
|
||||||
|
1) Factor1(温度气压因子)
|
||||||
|
- 高载荷变量:
|
||||||
|
Tn (-0.963), T (-0.958), Tx (-0.954), P (0.924), Po (0.921), Td (-0.898)
|
||||||
|
- 物理意义:
|
||||||
|
主要反映温度(T, Tn, Tx)和气压(P, Po)相关指标的强负相关关系(温度越高,气压越低)。
|
||||||
|
2) Factor2(颗粒物污染因子)
|
||||||
|
- 高载荷变量:
|
||||||
|
AQI (0.967), PM10 (0.933), PM2.5 (0.879)
|
||||||
|
- 物理意义:
|
||||||
|
直接反映空气质量指数(AQI)和颗粒物污染(PM10, PM2.5),空气质量问题代表颗粒物污染主导。
|
||||||
|
3) Factor3(大气条件与污染物因子)
|
||||||
|
- 高载荷变量:
|
||||||
|
U (-0.824), Ff (0.772), NO2 (-0.728), CO (-0.695), VV (0.667)
|
||||||
|
- 物理意义:
|
||||||
|
风速增加(Ff)与相对湿度(U)负相关,与能见度(VV)正相关。
|
||||||
|
同时风速增加(Ff)与污染物浓度(NO2、CO负载荷)的负相关关系可能暗示了风对大气污染物的扩散作用。
|
||||||
|
4) Factor4(因子)
|
||||||
|
- 高载荷变量:
|
||||||
|
Pa (-0.747), SO2 (0.694)
|
||||||
|
- 物理意义:难以解释。
|
||||||
|
5) Factor5(降水因子)
|
||||||
|
- 高载荷变量:
|
||||||
|
RRR (0.819), tR (0.512)
|
||||||
|
- 物理意义:
|
||||||
|
直接反映降水量(RRR)和降水时间(tR)。
|
||||||
|
6) 交叉载荷与特殊变量
|
||||||
|
- O3(臭氧):在Factor1和Factor3上均有载荷,可能需结合气象与化学机制进一步分析。
|
||||||
|
- VV(能见度):受Factor3(风速)和Factor2(颗粒物)共同影响,符合实际物理规律。
|
||||||
|
## XGBOOST模型解读
|
||||||
|
1. 该模型使用历史AQI数据,并进行周期性编码和滞后特征构建(3小时粒度的滞后特征(最多7天)),作为特征工程。
|
||||||
|
2. 每次预测均采用该时间点以前的真实数据,即每次预测均为单步预测。
|
||||||
|
3. 使用随机搜索法参数调优。
|
||||||
|
4. 评估指标:效果很好
|
||||||
|
- RMSE: 12.30
|
||||||
|
- R-squared: 0.92
|
||||||
|
- MAE: 7.87
|
||||||
|
5. 重要特征:观察图,AQI_lag_1,2,3最为重要,即该时刻的AQI主要由前3个观测时刻决定。day_of_week显示影响较小,但不是完全没有。
|
||||||
|
6. 其实也做了利用递归直接预测一整个月的,
|
40
研究思路.md
Normal file
40
研究思路.md
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
# 预测建模
|
||||||
|
北京市空气质量指数预测(推荐难度系数10)
|
||||||
|
|
||||||
|
这个数据集是北京市2022年11月1日至2023年10月31日期间空气质量相关数据。
|
||||||
|
根据这个数据集,回答以下问题:
|
||||||
|
|
||||||
|
(1)研究单日内空气质量指数与各项指标的变化趋势,这种趋势是否具有周期性?
|
||||||
|
|
||||||
|
(2)简述各项指标间的相互关系。
|
||||||
|
|
||||||
|
(3)令2022年11月1日至2023年9月30日的空气质量数据为训练集,剩余数据为测试集。基于训练集,尝试使用两种不同的方法构建空气质量指数预测模型,并在测试集上测试。比较所选模型的预测效果。
|
||||||
|
## 题目(1)
|
||||||
|
1. 数据预处理:将数据按小时分组,计算每个小时各指标(AQI、CO、NO2等)的平均值。
|
||||||
|
2. 可视化分析:绘制各指标小时均值的折线图,观察是否存在规律性波动(如早晚高峰)。
|
||||||
|
3. 周期性检验:
|
||||||
|
- 自相关函数(ACF):对AQI和各指标的时间序列计算ACF,检查24小时(或数据采样间隔的周期,如每3小时一次则周期为8)附近的峰值。
|
||||||
|
## 题目(2)
|
||||||
|
1. 计算所有变量的Pearson/Spearman相关系数矩阵,绘制热图。
|
||||||
|
2. 关注污染物之间(如PM2.5与CO、NO2)的正相关性,以及气象因素(如风速Ff与PM2.5)的负相关性。
|
||||||
|
3. 主成分分析(PCA):提取主成分,分析哪些变量贡献最大,揭示潜在关联。
|
||||||
|
## 题目(3)
|
||||||
|
1. 数据划分:
|
||||||
|
- 训练集:2022-11-01至2023-09-30。
|
||||||
|
- 测试集:2023-10-01至2023-10-31。
|
||||||
|
2. 特征工程
|
||||||
|
- 时间特征:小时、星期几、月份。
|
||||||
|
- 滞后特征:如前1小时AQI(需确保无未来数据泄漏)。
|
||||||
|
3. 模型选择:
|
||||||
|
1) 方法1:SARIMA(季节性ARIMA)
|
||||||
|
|
||||||
|
仅使用AQI历史数据,参数通过ACF/PACF确定,例如SARIMA(1,1,1)(1,1,1,8)(假设每日8个时间点)。
|
||||||
|
模型结构:SARIMA是传统时间序列模型ARIMA的扩展,通过引入季节性参数(P, D, Q, s)来捕捉数据的季节性规律。
|
||||||
|
2) 方法2: XGBOOST
|
||||||
|
基于决策树的集成学习算法,通过梯度提升框架迭代优化多个弱分类器(树模型),最终组合成强预测模型。
|
||||||
|
使用多参数,能建模非线性关系
|
||||||
|
4. 评估指标:
|
||||||
|
MAE(平均绝对误差)、RMSE(均方根误差)、R²(拟合优度)。
|
||||||
|
5. 结果比较:
|
||||||
|
- 对比两模型在测试集上的误差指标,分析优劣。
|
||||||
|
- 可视化预测值与真实值的时间序列对比图。
|
BIN
递归预测整个月.png
Normal file
BIN
递归预测整个月.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 85 KiB |
Loading…
x
Reference in New Issue
Block a user