-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
150 lines (107 loc) · 4.6 KB
/
code.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import BaggingRegressor, RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error
from pmdarima.arima import ARIMA
from sklearn.inspection import permutation_importance
import warnings
warnings.filterwarnings("ignore") # Suppress ARIMA warnings
# Load data from CSV
defasado_com_cgpj = './cgpAA.csv'
defasado_com_juros = './txjAA.csv'
feature_importance_cgpj = './feat_cgp.csv'
feature_importance_juros = './feat_txj.csv'
feature_importance_2_cgpj = './feat_2_maiores_cgp.csv'
feature_importance_2_juros = './feat_2_maiores_txj.csv'
logar_previsoes = False
logar_feature_importance = False
media_movel_4_meses = 0
soma_4_meses = 1
valores = []
with open(feature_importance_2_cgpj, 'r') as arquivo_cru:
arquivo_lido = csv.reader(arquivo_cru)
# Skip the header row
next(arquivo_lido)
for linha in arquivo_lido:
valores.append([float(val) for val in linha])
valores = np.array(valores)
# Separate features and target variables
X = valores[:, 2:]
y = valores[:, media_movel_4_meses]
# Split data into learning and test periods
learning_end_index = 153 + 12 + 6 # Index for June 2020
X_learn, X_test = X[:learning_end_index], X[learning_end_index:]
y_learn, y_test = y[:learning_end_index], y[learning_end_index:]
# Normalize data using StandardScaler
scaler = StandardScaler()
X_learn_scaled = scaler.fit_transform(X_learn)
X_test_scaled = scaler.transform(X_test)
# -----------------------------------------------------
print("--------------- BAGGING -------------\n")
bagging_regressor = BaggingRegressor(n_estimators=100, random_state=42)
bagging_regressor.fit(X_learn_scaled, y_learn)
y_pred_bagging = bagging_regressor.predict(X_test_scaled)
mse_bagging = mean_squared_error(y_test, y_pred_bagging)
base_regressors = bagging_regressor.estimators_
# Initialize a list to store feature importances from all base regressors
feature_importances = []
# Iterate through each base regressor (decision tree) to extract feature importances
for base_regressor in base_regressors:
if hasattr(base_regressor, 'feature_importances_'):
# Some base regressors may not have feature importances, so check if the attribute exists
feature_importances.append(base_regressor.feature_importances_)
# Calculate the average feature importances across all base regressors
average_feature_importances = np.mean(feature_importances, axis=0)
if (logar_feature_importance):
for valor in average_feature_importances:
print(f"{valor}".replace(".", ","))
if (logar_previsoes):
for valor in y_pred_bagging:
print(f"{valor}".replace(".", ","))
print("\n")
print(f'RAIZ QUADRADA DE ERRO DO BAGGING: {mse_bagging}\n')
# -----------------------------------------------------------
print("-------------- RANDOM FOREST --------------\n")
random_forest_regressor = RandomForestRegressor(
n_estimators=100, random_state=42)
random_forest_regressor.fit(X_learn_scaled, y_learn)
y_pred_rf = random_forest_regressor.predict(X_test_scaled)
mse_rf = mean_squared_error(y_test, y_pred_rf)
if (logar_feature_importance):
for valor in random_forest_regressor.feature_importances_:
print(f"{valor}".replace(".", ","))
if (logar_previsoes):
for valor in y_pred_rf:
print(f"{valor}".replace(".", ","))
print("\n")
print(f'RAIZ QUADRADA DE ERRO DO RANDOM FOREST: {mse_rf}\n')
# ----------------------------------------------------------
print("--------------- XGBOOST ------------\n")
xgb_regressor = XGBRegressor(n_estimators=100, random_state=42)
xgb_regressor.fit(X_learn_scaled, y_learn)
y_pred_xgb = xgb_regressor.predict(X_test_scaled)
mse_xgb = mean_squared_error(y_test, y_pred_xgb)
if (logar_feature_importance):
for valor in xgb_regressor.feature_importances_:
print(f"{valor:.20f}".replace(".", ","))
if (logar_previsoes):
for valor in y_pred_xgb:
print(f"{valor}".replace(".", ","))
print("\n")
print(f'RAIZ QUADRADA DE ERRO DO XGBOOST: {mse_xgb}\n')
# ----------------------------------------------------------
print("--------------- ARIMA (1,1,1) ------------\n")
arima_model = ARIMA(order=(1, 1, 1))
arima_model_fit = arima_model.fit(y_learn)
y_pred_arima = arima_model_fit.predict(n_periods=len(y_test))
mse_arima = mean_squared_error(y_test, y_pred_arima)
if (logar_previsoes):
for valor in y_pred_arima:
print(f"{valor}".replace(".", ","))
print("\n")
print(f'RAIZ QUADRADA DE ERRO DO ARIMA: {mse_arima}\n')