当前位置:   article > 正文

基于机器学习预测未来的二氧化碳排放量(随机森林和XGBoost)_工业排放预测机器学习

工业排放预测机器学习

基于机器学习预测未来的二氧化碳排放量(随机森林和XGBoost)

简介:

CO2排放是当今全球关注的环境问题之一。本文将使用Python对OWID提供的CO2排放数据集进行分析,并尝试构建机器学习模型来预测未来的CO2排放趋势。我们将探索数据集中的CO2排放情况,分析各国/地区的排放趋势,并利用机器学习算法来预测未来的CO2排放量。

1. 数据集介绍:

我们使用的数据集是OWID(Our World in Data)提供的CO2排放数据集。该数据集包含了各国/地区自1949年至2020年的CO2排放量以及相关的经济、人口等数据。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
  • 1
  • 2
  • 3
  • 4
dataset = pd.read_csv('owid-co2-data.csv')
  • 1
print(dataset.head()) #显示数据集的前5行。 
print(dataset.shape) #显示数据集的行数和列数
  • 1
  • 2
  iso_code      country  year    co2  consumption_co2  co2_growth_prct  \
0      AFG  Afghanistan  1949  0.015              NaN              NaN   
1      AFG  Afghanistan  1950  0.084              NaN            475.0   
2      AFG  Afghanistan  1951  0.092              NaN              8.7   
3      AFG  Afghanistan  1952  0.092              NaN              0.0   
4      AFG  Afghanistan  1953  0.106              NaN             16.0   

   co2_growth_abs  trade_co2  co2_per_capita  consumption_co2_per_capita  ...  \
0             NaN        NaN           0.002                         NaN  ...   
1           0.070        NaN           0.011                         NaN  ...   
2           0.007        NaN           0.012                         NaN  ...   
3           0.000        NaN           0.012                         NaN  ...   
4           0.015        NaN           0.013                         NaN  ...   

   ghg_per_capita  methane  methane_per_capita  nitrous_oxide  \
0             NaN      NaN                 NaN            NaN   
1             NaN      NaN                 NaN            NaN   
2             NaN      NaN                 NaN            NaN   
3             NaN      NaN                 NaN            NaN   
4             NaN      NaN                 NaN            NaN   

   nitrous_oxide_per_capita  population           gdp  \
0                       NaN   7624058.0           NaN   
1                       NaN   7752117.0  9.421400e+09   
2                       NaN   7840151.0  9.692280e+09   
3                       NaN   7935996.0  1.001732e+10   
4                       NaN   8039684.0  1.063052e+10   

   primary_energy_consumption  energy_per_capita  energy_per_gdp  
0                         NaN                NaN             NaN  
1                         NaN                NaN             NaN  
2                         NaN                NaN             NaN  
3                         NaN                NaN             NaN  
4                         NaN                NaN             NaN  

[5 rows x 58 columns]
(25204, 58)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

2. 数据预处理:

df = dataset.drop(columns=[ 'consumption_co2','co2_growth_prct','co2_growth_abs' ])
df.head()
  • 1
  • 2
iso_codecountryyearco2trade_co2co2_per_capitaconsumption_co2_per_capitashare_global_co2cumulative_co2share_global_cumulative_co2...ghg_per_capitamethanemethane_per_capitanitrous_oxidenitrous_oxide_per_capitapopulationgdpprimary_energy_consumptionenergy_per_capitaenergy_per_gdp
0AFGAfghanistan19490.015NaN0.002NaN0.00.0150.0...NaNNaNNaNNaNNaN7624058.0NaNNaNNaNNaN
1AFGAfghanistan19500.084NaN0.011NaN0.00.0990.0...NaNNaNNaNNaNNaN7752117.09.421400e+09NaNNaNNaN
2AFGAfghanistan19510.092NaN0.012NaN0.00.1910.0...NaNNaNNaNNaNNaN7840151.09.692280e+09NaNNaNNaN
3AFGAfghanistan19520.092NaN0.012NaN0.00.2820.0...NaNNaNNaNNaNNaN7935996.01.001732e+10NaNNaNNaN
4AFGAfghanistan19530.106NaN0.013NaN0.00.3880.0...NaNNaNNaNNaNNaN8039684.01.063052e+10NaNNaNNaN

5 rows × 55 columns

df1 = df[['country', 'year','co2','coal_co2','cement_co2', 'flaring_co2','gas_co2','oil_co2', 'other_industry_co2','methane', 'nitrous_oxide', 'population' ]]
df1
  • 1
  • 2
countryyearco2coal_co2cement_co2flaring_co2gas_co2oil_co2other_industry_co2methanenitrous_oxidepopulation
0Afghanistan19490.0150.015NaNNaNNaNNaNNaNNaNNaN7624058.0
1Afghanistan19500.0840.021NaNNaNNaN0.063NaNNaNNaN7752117.0
2Afghanistan19510.0920.026NaNNaNNaN0.066NaNNaNNaN7840151.0
3Afghanistan19520.0920.032NaNNaNNaN0.060NaNNaNNaN7935996.0
4Afghanistan19530.1060.038NaNNaNNaN0.068NaNNaNNaN8039684.0
.......................................
25199Zimbabwe201610.7386.9590.639NaNNaN3.139NaN11.926.5514030338.0
25200Zimbabwe20179.5825.6650.678NaNNaN3.239NaNNaNNaN14236599.0
25201Zimbabwe201811.8547.1010.697NaNNaN4.056NaNNaNNaN14438812.0
25202Zimbabwe201910.9496.0200.697NaNNaN4.232NaNNaNNaN14645473.0
25203Zimbabwe202010.5316.2570.697NaNNaN3.576NaNNaNNaN14862927.0

25204 rows × 12 columns

final_df = df1[df1['year' ]>1995]
final_df
  • 1
  • 2
countryyearco2coal_co2cement_co2flaring_co2gas_co2oil_co2other_industry_co2methanenitrous_oxidepopulation
47Afghanistan19961.1650.0070.0470.0220.3080.780NaN9.933.2918853444.0
48Afghanistan19971.0840.0040.0470.0220.2830.728NaN10.603.5919357126.0
49Afghanistan19981.0290.0040.0470.0220.2650.691NaN11.103.8819737770.0
50Afghanistan19990.8100.0040.0470.0220.2420.495NaN11.874.1520170847.0
51Afghanistan20000.7580.0040.0100.0220.2240.498NaN10.593.6220779957.0
.......................................
25199Zimbabwe201610.7386.9590.639NaNNaN3.139NaN11.926.5514030338.0
25200Zimbabwe20179.5825.6650.678NaNNaN3.239NaNNaNNaN14236599.0
25201Zimbabwe201811.8547.1010.697NaNNaN4.056NaNNaNNaN14438812.0
25202Zimbabwe201910.9496.0200.697NaNNaN4.232NaNNaNNaN14645473.0
25203Zimbabwe202010.5316.2570.697NaNNaN3.576NaNNaNNaN14862927.0

6073 rows × 12 columns

final_df = final_df[(final_df['country'].isin(['United States', 'Africa', 'Antartica','South Korea', 'Bangladesh', 'Canada', 'Germany', 'Brazil', 'Argentina','Japan', 'India', 'United Kingdom', 'Saudi Arabia', 'China', 'Australia','Russia']) & (final_df['co2'] > 0))]
final_df
  • 1
  • 2
countryyearco2coal_co2cement_co2flaring_co2gas_co2oil_co2other_industry_co2methanenitrous_oxidepopulation
184Africa1996783.254353.13027.68123.787108.019270.637NaNNaNNaN735361106.0
185Africa1997812.903360.83728.35023.39495.205305.117NaNNaNNaN753737584.0
186Africa1998838.022355.51429.20322.961112.712317.632NaNNaNNaN772437161.0
187Africa1999830.397366.52330.31123.569114.377295.618NaNNaNNaN791504165.0
188Africa2000886.562370.24731.51055.282114.350315.173NaNNaNNaN810984230.0
.......................................
24063United States20165248.0241379.74439.43951.9081502.4752246.52427.933629.38251.7323015992.0
24064United States20175207.7511338.66740.32456.1861480.0592265.32627.190NaNNaN325084758.0
24065United States20185375.4911283.53238.97171.0081641.0412316.81124.128NaNNaN327096263.0
24066United States20195255.8161098.85440.89684.5101694.8942313.37223.291NaNNaN329064917.0
24067United States20204712.771888.64940.79584.5101654.9882020.53823.291NaNNaN331002647.0

375 rows × 12 columns

final_df.isnull().sum()

  • 1
  • 2
country                 0
year                    0
co2                     0
coal_co2               25
cement_co2              0
flaring_co2            77
gas_co2                 0
oil_co2                 0
other_industry_co2    125
methane                81
nitrous_oxide          81
population              0
dtype: int64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.数据可视化

我们将根据我们的数据集绘制图表并分析一些结果。 我们绘制一下随时间线的co2排放趋势图:

px.line(dataset, x = 'year', y = 'co2', color='country')

  • 1
  • 2

在这里插入图片描述

dataset = dataset.dropna(subset=['co2'])

px.scatter(dataset[dataset['year']==2019], x="co2_per_capita", y="energy_per_capita", size="co2", color="country", hover_name="country", log_x=True, size_max=60)
  • 1
  • 2
  • 3

在这里插入图片描述

continent_data =  dataset[(dataset['country'].isin(['Europe', 'Africa', 'North America', 'South America', 'Oceania', 'Asia'])) & (dataset['co2'] > 0)]
continent_data
  • 1
  • 2
iso_codecountryyearco2consumption_co2co2_growth_prctco2_growth_abstrade_co2co2_per_capitaconsumption_co2_per_capita...ghg_per_capitamethanemethane_per_capitanitrous_oxidenitrous_oxide_per_capitapopulationgdpprimary_energy_consumptionenergy_per_capitaenergy_per_gdp
72NaNAfrica18840.022NaNNaNNaNNaN0.005NaN...NaNNaNNaNNaNNaN130848603.0NaNNaNNaNNaN
73NaNAfrica18850.037NaN66.670.015NaN0.008NaN...NaNNaNNaNNaNNaN131563803.0NaNNaNNaNNaN
74NaNAfrica18860.048NaN30.000.011NaN0.010NaN...NaNNaNNaNNaNNaN132284841.0NaNNaNNaNNaN
75NaNAfrica18870.048NaN0.000.000NaN0.010NaN...NaNNaNNaNNaNNaN133011765.0NaNNaNNaNNaN
76NaNAfrica18880.081NaN69.230.033NaN0.017NaN...NaNNaNNaNNaNNaN133744628.0NaNNaNNaNNaN
..................................................................
20888NaNSouth America20161164.8981240.096-3.32-40.06475.1982.7992.980...NaNNaNNaNNaNNaN416164871.0NaNNaNNaNNaN
20889NaNSouth America20171156.7341238.620-0.70-8.16481.8862.7552.950...NaNNaNNaNNaNNaN419903920.0NaNNaNNaNNaN
20890NaNSouth America20181091.4501173.851-5.64-65.28482.4012.5772.771...NaNNaNNaNNaNNaN423581098.0NaNNaNNaNNaN
20891NaNSouth America20191065.5101139.737-2.38-25.94074.2282.4942.668...NaNNaNNaNNaNNaN427199425.0NaNNaNNaNNaN
20892NaNSouth America2020994.160NaN-6.70-71.349NaN2.308NaN...NaNNaNNaNNaNNaN430759771.0NaNNaNNaNNaN

1111 rows × 58 columns

px.pie(final_df, names='country', values='co2')

  • 1
  • 2

在这里插入图片描述

final_df_2020 = final_df[(final_df[ 'year' ]==2020) ]
final_df_2020
final_df_2020[['country','coal_co2','cement_co2','flaring_co2','gas_co2', 'oil_co2','other_industry_co2']].plot(x='country', kind='bar',figsize=(9,5),width=0.9)
plt.title('2020 CO2 consumption')
plt.xlabel('Countries' )
plt.ylabel('CO2 measured in million tonnes')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

print(dataset.info)
print(dataset.head())
  • 1
  • 2
<bound method DataFrame.info of       iso_code      country  year     co2  consumption_co2  co2_growth_prct  \
0          AFG  Afghanistan  1949   0.015              NaN              NaN   
1          AFG  Afghanistan  1950   0.084              NaN           475.00   
2          AFG  Afghanistan  1951   0.092              NaN             8.70   
3          AFG  Afghanistan  1952   0.092              NaN             0.00   
4          AFG  Afghanistan  1953   0.106              NaN            16.00   
...        ...          ...   ...     ...              ...              ...   
25199      ZWE     Zimbabwe  2016  10.738           12.153           -12.17   
25200      ZWE     Zimbabwe  2017   9.582           11.248           -10.77   
25201      ZWE     Zimbabwe  2018  11.854           13.163            23.72   
25202      ZWE     Zimbabwe  2019  10.949           12.422            -7.64   
25203      ZWE     Zimbabwe  2020  10.531              NaN            -3.82   

       co2_growth_abs  trade_co2  co2_per_capita  consumption_co2_per_capita  \
0                 NaN        NaN           0.002                         NaN   
1               0.070        NaN           0.011                         NaN   
2               0.007        NaN           0.012                         NaN   
3               0.000        NaN           0.012                         NaN   
4               0.015        NaN           0.013                         NaN   
...               ...        ...             ...                         ...   
25199          -1.488      1.415           0.765                       0.866   
25200          -1.156      1.666           0.673                       0.790   
25201           2.273      1.308           0.821                       0.912   
25202          -0.905      1.473           0.748                       0.848   
25203          -0.418        NaN           0.709                         NaN   

       ...  ghg_per_capita  methane  methane_per_capita  nitrous_oxide  \
0      ...             NaN      NaN                 NaN            NaN   
1      ...             NaN      NaN                 NaN            NaN   
2      ...             NaN      NaN                 NaN            NaN   
3      ...             NaN      NaN                 NaN            NaN   
4      ...             NaN      NaN                 NaN            NaN   
...    ...             ...      ...                 ...            ...   
25199  ...           4.703    11.92                0.85           6.55   
25200  ...             NaN      NaN                 NaN            NaN   
25201  ...             NaN      NaN                 NaN            NaN   
25202  ...             NaN      NaN                 NaN            NaN   
25203  ...             NaN      NaN                 NaN            NaN   

       nitrous_oxide_per_capita  population           gdp  \
0                           NaN   7624058.0           NaN   
1                           NaN   7752117.0  9.421400e+09   
2                           NaN   7840151.0  9.692280e+09   
3                           NaN   7935996.0  1.001732e+10   
4                           NaN   8039684.0  1.063052e+10   
...                         ...         ...           ...   
25199                     0.467  14030338.0  2.096179e+10   
25200                       NaN  14236599.0  2.194784e+10   
25201                       NaN  14438812.0  2.271535e+10   
25202                       NaN  14645473.0           NaN   
25203                       NaN  14862927.0           NaN   

       primary_energy_consumption  energy_per_capita  energy_per_gdp  
0                             NaN                NaN             NaN  
1                             NaN                NaN             NaN  
2                             NaN                NaN             NaN  
3                             NaN                NaN             NaN  
4                             NaN                NaN             NaN  
...                           ...                ...             ...  
25199                        47.5           3385.574           1.889  
25200                         NaN                NaN             NaN  
25201                         NaN                NaN             NaN  
25202                         NaN                NaN             NaN  
25203                         NaN                NaN             NaN  

[23949 rows x 58 columns]>
  iso_code      country  year    co2  consumption_co2  co2_growth_prct  \
0      AFG  Afghanistan  1949  0.015              NaN              NaN   
1      AFG  Afghanistan  1950  0.084              NaN            475.0   
2      AFG  Afghanistan  1951  0.092              NaN              8.7   
3      AFG  Afghanistan  1952  0.092              NaN              0.0   
4      AFG  Afghanistan  1953  0.106              NaN             16.0   

   co2_growth_abs  trade_co2  co2_per_capita  consumption_co2_per_capita  ...  \
0             NaN        NaN           0.002                         NaN  ...   
1           0.070        NaN           0.011                         NaN  ...   
2           0.007        NaN           0.012                         NaN  ...   
3           0.000        NaN           0.012                         NaN  ...   
4           0.015        NaN           0.013                         NaN  ...   

   ghg_per_capita  methane  methane_per_capita  nitrous_oxide  \
0             NaN      NaN                 NaN            NaN   
1             NaN      NaN                 NaN            NaN   
2             NaN      NaN                 NaN            NaN   
3             NaN      NaN                 NaN            NaN   
4             NaN      NaN                 NaN            NaN   

   nitrous_oxide_per_capita  population           gdp  \
0                       NaN   7624058.0           NaN   
1                       NaN   7752117.0  9.421400e+09   
2                       NaN   7840151.0  9.692280e+09   
3                       NaN   7935996.0  1.001732e+10   
4                       NaN   8039684.0  1.063052e+10   

   primary_energy_consumption  energy_per_capita  energy_per_gdp  
0                         NaN                NaN             NaN  
1                         NaN                NaN             NaN  
2                         NaN                NaN             NaN  
3                         NaN                NaN             NaN  
4                         NaN                NaN             NaN  

[5 rows x 58 columns]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

4. 数据预处理:

在进行数据分析之前,我们需要对数据进行预处理,包括处理缺失值和选择感兴趣的特征列。

features = ['year', 'population', 'gdp', 'primary_energy_consumption', 'energy_per_capita', 'energy_per_gdp']
target = 'co2'
data = dataset[features + [target]].dropna()
  • 1
  • 2
  • 3

5. 机器学习建模:

我们将尝试使用随机森林回归和XGBoost算法建立CO2排放的预测模型,并评估其性能。

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error


# 分割数据集
X = data[features]
y = data[target]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 随机森林回归
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)
mse_rf = mean_squared_error(y_test, y_pred_rf)
print("随机森林回归均方误差:", mse_rf)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
随机森林回归均方误差: 5564.344690878827
  • 1
from xgboost import XGBRegressor

# XGBoost 回归
xgb_model = XGBRegressor(n_estimators=100, random_state=42)
xgb_model.fit(X_train, y_train)
y_pred_xgb = xgb_model.predict(X_test)
mse_xgb = mean_squared_error(y_test, y_pred_xgb)
print("XGBoost 回归均方误差:", mse_xgb)

# 查看测试集的预测结果
results_rf = pd.DataFrame({'Actual': y_test, 'Predicted_RF': y_pred_rf})
results_xgb = pd.DataFrame({'Actual': y_test, 'Predicted_XGB': y_pred_xgb})

print("随机森林回归预测结果:\n", results_rf.head())
print("XGBoost 回归预测结果:\n", results_xgb.head())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
XGBoost 回归均方误差: 6550.179319235124
随机森林回归预测结果:
          Actual  Predicted_RF
4678   1183.215    1140.30654
22293     2.536       3.88285
1552    187.609     171.56567
22409     7.440       9.76497
11248   129.475      76.18446
XGBoost 回归预测结果:
          Actual  Predicted_XGB
4678   1183.215    1219.047852
22293     2.536       5.083570
1552    187.609     184.427612
22409     7.440       9.800858
11248   129.475      98.655746
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

6.结论:

通过对CO2排放数据集的分析和机器学习建模,我们可以更好地理解全球CO2排放的情况,并为未来的环境保护和可持续发展提供数据支持。未来的研究可以进一步探索CO2排放与气候变化、经济增长等因素之间的关系,并提出相应的政策建议。

如有遇到问题可以找小编沟通交流哦。另外小编帮忙辅导大课作业,学生毕设等。不限于MapReduce, MySQL, python,java,大数据,模型训练等。 hadoop hdfs yarn spark Django flask flink kafka flume datax sqoop seatunnel echart可视化 机器学习等
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/632686
推荐阅读
相关标签
  

闽ICP备14008679号