-
Notifications
You must be signed in to change notification settings - Fork 2
/
beijingdata_tradaboost.py
199 lines (150 loc) · 8.42 KB
/
beijingdata_tradaboost.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
################################################################################
###### This code is used for UCI beijing dataset: TrAdaBoost.R2 ################
################################# Header files #################################
from TwoStageTrAdaBoostR2 import TwoStageTrAdaBoostR2 ## For two-stage TrAdaBoost.R2
import pandas as pd
import sys
import numpy as np
from pandas import DataFrame
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import AdaBoostRegressor
from keras.models import Sequential, load_model, Model
from keras.layers import Input, Dense, Activation, Conv2D, Dropout, Flatten
from keras import optimizers, utils, initializers, regularizers
import keras.backend as K
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.preprocessing import StandardScaler #Importing the StandardScaler
from itertools import combinations
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats.stats import pearsonr
from math import sqrt
#Geo plotting libraries
#import geopandas as gdp
#from matplotlib.colors import ListedColormap
#import geoplot as glpt
import xgboost as xgb
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn import linear_model
from keras.models import Sequential
from keras.callbacks import ModelCheckpoint
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.model_selection import KFold
import matplotlib.lines as mlines
import folium
import glob
from statistics import mean
print("Done uploading repositories")
################# UCI Beijing dataset (2013 - 2017) #####################################################################
############### Spacio-temporal dataset. (multi-year and multi-terrain) #################################################
path_beijing = r'/home/parulsharma/shrey/Beijing_AQI/PRSA_Data_20130301-20170228/' ## Path for all the files
allFiles = glob.glob(path_beijing + "/*.csv")
beijing_aqi_df = pd.DataFrame()
list_beijing = []
for file_ in allFiles:
temp_df = pd.read_csv(file_, index_col = None, header=0)
list_beijing.append(temp_df)
beijing_aqi_df = pd.concat(list_beijing)
cols = ['No', 'year', 'month', 'day', 'hour', 'PM2.5', 'PM10', 'SO2', 'NO2', 'CO', 'O3', 'TEMP',
'PRES', 'DEWP', 'RAIN', 'wd', 'WSPM', 'station']
beijing_aqi_df = beijing_aqi_df[cols]
drop_index = ['No']
beijing_aqi_df = beijing_aqi_df.drop(drop_index, axis=1)
beijing_aqi_df = beijing_aqi_df.sort_values(['station', 'year'])
# beijing_aqi_df.head(20)
# beijing_aqi_df.year.value_counts()
# beijing_aqi_df.wd.value_counts()
# beijing_aqi_df.shape
# beijing_aqi_df['station'].nunique()
######################## Seperate the dataset into predictors and target variable. #########################################
predictors = ['year', 'month', 'SO2', 'NO2', 'CO', 'TEMP', 'PRES', 'DEWP', 'RAIN', 'wd', 'WSPM', 'station', 'O3']
beijing_predictors_df = beijing_aqi_df[predictors]
wd_codes = {'N':1, 'E': 2, 'W': 3, 'S': 4, 'NE': 5, 'NW': 6, 'SE': 7, 'SW': 8, 'NNE': 9, 'NNW': 10, 'SSE': 11,
'SSW': 12, 'WNW': 13, 'WSW': 14, 'ENE': 15, 'ESE': 16 }
beijing_predictors_df.replace(wd_codes, inplace=True)
print(beijing_predictors_df.wd.value_counts())
############# Replacing missing values ####################################################################################
beijing_predictors_df = beijing_predictors_df.dropna(axis=0) # , subset=['O3'])
###### percentage of missing values in each column ########################################################################
round(beijing_predictors_df.isna().sum()/len(beijing_predictors_df.index), 2)*100
corrMatrix_beijing = beijing_predictors_df.corr()
corrMatrix_beijing["O3"].sort_values(ascending=False)
### Drop stations (dropping stations at this stage so more combination of splits can be made later) #######################
drop_stations = ['station']
beijing_predictors_df = beijing_predictors_df.drop(drop_stations, axis=1)
######################################## Splitting the dataset by the year #################################################
beijing_predictors_df.year.value_counts()
beijing_predictors_df_target = beijing_predictors_df[beijing_predictors_df['year'].isin([2014, 2015])]
beijing_predictors_df_source = beijing_predictors_df[beijing_predictors_df['year'].isin([2016])]
print(beijing_predictors_df_source.shape)
print(beijing_predictors_df_target.shape)
#################################### Standardize the dataset. ###############################################################
cols_to_norm = ['SO2', 'NO2', 'CO', 'TEMP', 'PRES', 'DEWP', 'RAIN', 'WSPM']
ss = StandardScaler()
beijing_predictors_df_target[cols_to_norm] = ss.fit_transform(beijing_predictors_df_target[cols_to_norm])
beijing_predictors_df_source[cols_to_norm] = ss.fit_transform(beijing_predictors_df_source[cols_to_norm])
################################# Splitting the dataset into train and test set ###############################################
target_beijing_col = ['O3']
beijing_predictors_df_target_Y = beijing_predictors_df_target[target_beijing_col]
beijing_predictors_df_target_X = beijing_predictors_df_target.drop(target_beijing_col, axis =1)
beijing_predictors_df_source_Y = beijing_predictors_df_source[target_beijing_col]
beijing_predictors_df_source_X = beijing_predictors_df_source.drop(target_beijing_col, axis =1)
X_train, X_test, y_train, y_test = train_test_split(beijing_predictors_df_target_X, beijing_predictors_df_target_Y, test_size = 0.999, random_state = 1)
X_source = beijing_predictors_df_source_X
y_source = beijing_predictors_df_source_Y
################################### Prediction for TrAdaBoost.R2 ##############################################################
predictionlist = []
r2scorelist = []
rmselist = []
print("TrAdaBoost.R2")
for x in range(0, 10):
TF_train_X = pd.concat([X_source, X_train], sort= False)
TF_train_y = pd.concat([y_source, y_train], sort= False)
np_TF_train_X = TF_train_X.to_numpy()
np_TF_train_y = TF_train_y.to_numpy()
np_test_X = X_test.to_numpy()
np_test_y = y_test.to_numpy()
np_TF_train_y_list = np_TF_train_y.ravel()
np_test_y_list = np_test_y.ravel()
sample_size = [len(X_source), len(X_train)]
n_estimators = 100
steps = 30
fold = 10
random_state = np.random.RandomState(1)
################################################TrAdaBoost.R2############################################################################
regr_1 = TwoStageTrAdaBoostR2(DecisionTreeRegressor(max_depth=6), #xgb.XGBRegressor(objective ='reg:squarederror',learning_rate = 0.0001,max_depth = 6,n_estimators = 2000),
n_estimators = n_estimators, sample_size = sample_size,
steps = steps, fold = fold,
random_state = random_state)
regr_1.fit(np_TF_train_X, np_TF_train_y_list)
y_pred1 = regr_1.predict(np_test_X)
predictionlist.append(y_pred1)
mse_twostageboost = np.sqrt(mean_squared_error(np_test_y_list, y_pred1))
print("RMSE of TrAdaboostR2:", mse_twostageboost)
rmselist.append(mse_twostageboost)
r2_score_twostageboost_values = pearsonr(np_test_y_list, y_pred1)
r2_score_twostageboost = (r2_score_twostageboost_values[0])**2
print("R^2 of TrAdaboostR2:", r2_score_twostageboost)
r2scorelist.append(r2_score_twostageboost)
predict_tradaboost = np.mean(predictionlist, axis=0)
list_orginal_tradaboost = np_test_y_list
print("mean RMSE of TrAdaboostR2:", mean(rmselist))
print("mean R^2 of TrAdaboostR2:", mean(r2scorelist))
# plt.xlabel('Predicted')
# plt.ylabel('Actual')
# plt.title('TrAdaBoost.R2 Predicted vs Actual')
# # plt.savefig("AQI_datasets/UCI_AQI_Results/NOx/AdaBoostR2_Transfer.png")
# plt.show()
with open('/home/parulsharma/shrey/Beijing_AQI/Results/TrAdaBoostO3_R2_std_clust.txt', 'w') as filehandle1:
for listitem in r2scorelist:
filehandle1.write('%s\n' % listitem)
with open('/home/parulsharma/shrey/Beijing_AQI/Results/TrAdaBoostO3_RMSE_std_clust.txt', 'w') as filehandle2:
for listitem in rmselist:
filehandle2.write('%s\n' % listitem)
with open('/home/parulsharma/shrey/Beijing_AQI/Results/TrAdaBoostO3_prediction_std_clust.txt', 'w') as filehandle3:
for listitem in predict_tradaboost:
filehandle3.write('%s\n' % listitem)
###########################################################################################################################################