-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
161 lines (107 loc) · 5.21 KB
/
data.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
import os
import pathlib
import pandas as pd
import numpy as np
import warnings
from sklearn.tree import DecisionTreeRegressor
warnings.filterwarnings('ignore')
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_absolute_error
APP_PATH = str(pathlib.Path(__file__).parent.resolve())
url = os.path.join(APP_PATH, os.path.join('data', 'F1_4_clean_data.csv'))
df = pd.read_csv(url, on_bad_lines='skip', sep=',', na_filter=True)
df = df.rename(columns={'countryName': 'country', 'reportingYear':'year'}).drop(columns=['Unnamed: 0'], axis=1)
df_pivoted = df.pivot_table(index=['City', 'year'], columns=['pollutant'], values='emissions', aggfunc=np.sum, fill_value=0)
df_pivoted.reset_index(inplace=True)
models = {'Random Forest Regressor': RandomForestRegressor(n_estimators=1000, max_depth=2, random_state=1),
'Decision Tree Regressor': DecisionTreeRegressor(random_state=1),
'Linear Regression': LinearRegression()}
# -----------------------------------------------------------------------------------------------------------------------
def get_clrtap_df(country):
url = os.path.join(APP_PATH, os.path.join('data', 'clrtap_data_' + country + '.csv'))
df = pd.read_csv(url, on_bad_lines='skip', sep='\t')
df.dropna()
return df
def get_air_df(country):
url = os.path.join(APP_PATH, os.path.join('data', 'air_data_' + country + '.csv'))
df = pd.read_csv(url, on_bad_lines='skip', sep='\t')
df.dropna()
return df
# ----------------------------------------------------------------------------------------------------------------------
def country_df_map(country, city, sector):
map_df = get_air_df(country)
if city != 'All':
index_city = map_df[map_df['City'] != city].index
map_df.drop(index_city, inplace=True)
if sector != 'All':
index_sector = map_df[map_df['eprtrSectorName'] != sector].index
map_df.drop(index_sector, inplace=True)
# index_pollutant = map_df[map_df['pollutant'] != 'Sulphur oxides (SOX)'].index
# map_df.drop(index_pollutant, inplace=True)
return map_df
def sector_emissions_per_country(country, pollutant, sector):
df = get_clrtap_df(country)
if pollutant != 'All':
index_pollutant = df[df['Pollutant_name'] != pollutant].index
df.drop(index_pollutant, inplace=True)
if sector != 'All':
index_sector = df[df['Sector_label_EEA'] != sector].index
df.drop(index_sector, inplace=True)
return df
# ----------------------------------------------------------------------------------------------------------------------
def get_test_train_city(city):
df_pivoted = df.pivot_table(index=['City', 'year'], columns=['pollutant'], values='emissions', aggfunc=np.sum, fill_value=0)
df_pivoted.reset_index(inplace=True)
index_country = df_pivoted[df_pivoted['City'] != city].index
df_pivoted.drop(index_country, inplace=True)
df_pivoted['total'] = df_pivoted.iloc[:,2:50].sum(axis=1)
years = df_pivoted['year'].unique().tolist()
splits = {'train': [], 'test': []}
for index, yr in enumerate(years[:-1]):
df_test = df_pivoted.loc[df_pivoted.year.isin([years[index+1]]), :]
df_train = df_pivoted.loc[df_pivoted.year.isin(years[:index+1]), :]
splits['test'].append(df_test[['total', 'year']])
splits['train'].append(df_train[['total', 'year']])
year_list = df_pivoted['year'].unique().tolist()
return splits, year_list
def get_predictions(splits, year_list, model, option):
predictions = []
expectations = []
for i in range(1, len(year_list)-1):
X_train = splits['train'][i][['year']]
y_train = splits['train'][i][['total']]
X_test = splits['test'][i][['year']]
y_test = splits['test'][i][['total']]
StandardScaler().fit_transform(X_train, X_test)
model.fit(X_train, y_train.values.ravel())
y_pred = model.predict(X_test)
predictions.append(y_pred)
expectations.append(y_test.values.ravel())
mean_absolute_error(y_test, y_pred)
if option:
return expectations
else:
return predictions
def get_predictions_with_model(city, model):
splits, year_list = get_test_train_city(city)
predictions = get_predictions(splits, year_list, models[model], False)
expectations = get_predictions(splits, year_list, models[model], True)
return predictions, expectations
def get_scatter(city, year, model):
years = [2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
splits, y = get_test_train_city(city)
index = years.index(year)
print(index)
X_train = splits['train'][index][['year']]
y_train = splits['train'][index][['total']]
X_test = splits['test'][index][['year']]
y_test = splits['test'][index][['total']]
StandardScaler().fit_transform(X_train, X_test)
model = models[model]
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred, y_test
# print(f'RMSE of {model} :', np.sqrt(mean_squared_error(y_pred, y_test)))
# print(f'R² score on test set using {model} :', r2_score(y_test, y_pred))