-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmodule_regression.py
178 lines (136 loc) · 5.89 KB
/
module_regression.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import statsmodels.api as sm
from scipy import stats
from sklearn.metrics import mean_absolute_error, mean_squared_error, mean_absolute_percentage_error
### Boston Housing Price
# 자료형 변환 + 결측값 처리
def prep(df_origin):
df = df_origin.copy()
# 자료형 변환
col = []
if df['ZN'].dtype == 'object':
for i in df['ZN']:
col.append(float(i[1:-1]))
df['ZN'] = col
col = []
if df['CHAS'].dtype == 'object':
for i in df['CHAS']:
col.append(float(i[1:-1]))
df['CHAS'] = col
# 결측값 처리
for i in df.columns[df.isnull().sum() != 0]:
if i not in ['INDUS', 'RM']:
df[i].fillna(df[i].mean(), inplace=True)
else:
df[i].fillna(df[i].median(), inplace=True)
return df
# 데이터 변환
def feature_engineering(df_origin):
df = df_origin.copy()
interval = [100, 200, 300, 400, 500, 600, 700, 800]
if df['TAX'].max() >= 100:
df['TAX'] = np.digitize(df['TAX'], bins=interval)
if 'TAX' in df.columns:
df_dummy = pd.get_dummies(df['TAX'], prefix='TAX', drop_first=True)
df = pd.concat([df, df_dummy], axis=1)
del df['TAX']
if 'CHAS' in df.columns:
df['CHAS'] = df['CHAS'].astype(int)
df_dummy = pd.get_dummies(df['CHAS'], prefix='CHAS', drop_first=False)
df = pd.concat([df, df_dummy], axis=1)
del df['CHAS']
return df
# 데이터 분리
def datasplit(df, Y_colname, test_size=0.2, random_state=123):
X_colname = [x for x in df.columns if x not in Y_colname]
X_train, X_test, Y_train, Y_test = train_test_split(df[X_colname], df[Y_colname],
test_size=test_size, random_state=random_state)
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape)
return X_train, X_test, Y_train, Y_test
# 데이터 변환 후 X_train과 X_test의 변수 갯수 일치
def col_mapping(X_train, X_test):
X_tr = X_train.copy()
X_te = X_test.copy()
# Train & Test 변수명 체크
X_te_noncol = [i for i in X_tr.columns if i not in X_te.columns]
X_tr_noncol = [i for i in X_te.columns if i not in X_tr.columns]
# 변수 갯수 일치
if X_te_noncol != []:
for i in X_te_noncol:
X_te[i] = 0
X_te = X_te[X_tr.columns].copy()
if X_tr_noncol != []:
for i in X_tr_noncol:
X_tr[i] = 0
X_tr = X_tr[X_te.columns].copy()
return X_tr, X_te
# 스케일 조정
def scale(scaler, X_train, X_test):
scaler_fit = scaler.fit(X_train)
X_train_scaling = pd.DataFrame(scaler_fit.transform(X_train),
index=X_train.index, columns=X_train.columns)
X_test_scaling = pd.DataFrame(scaler_fit.transform(X_test),
index=X_test.index, columns=X_test.columns)
return X_train_scaling, X_test_scaling
# 실제 Y와 예측치 시각화
def plot_prediction(Y_true_pred):
plt.figure(figsize=(16, 8))
plt.plot(Y_true_pred, linewidth=5, label=Y_true_pred.columns)
plt.xticks(fontsize=25, rotation=0)
plt.yticks(fontsize=25)
plt.xlabel('Index', fontname='serif', fontsize=28)
plt.legend(fontsize=20)
plt.grid()
plt.show()
# 검증 함수화
def evaluation_reg(Y_real, Y_pred):
MAE = mean_absolute_error(Y_real, Y_pred)
MSE = mean_squared_error(Y_real, Y_pred)
MAPE = mean_absolute_percentage_error(Y_real, Y_pred)
Score = pd.DataFrame([MAE, MSE, MAPE], index=['MAE', 'MSE', 'MAPE'], columns=['Score']).T
return Score
# Train & Test 모두의 검증 함수화
def evaluation_reg_trte(Y_real_tr, Y_pred_tr, Y_real_te, Y_pred_te):
Score_tr = evaluation_reg(Y_real_tr, Y_pred_tr)
Score_te = evaluation_reg(Y_real_te, Y_pred_te)
Score_trte = pd.concat([Score_tr, Score_te], axis=0)
Score_trte.index = ['Train', 'Test']
return Score_trte
# 에러 분석
def error_analysis(X_Data, Y_Pred, Residual, graph_on=False):
if graph_on == True:
##### 시각화
# 잔차의 정규본포성 확인
sns.distplot(Residual, norm_hist='True', fit=stats.norm)
plt.show()
# 잔차의 등분산성 확인
temp = pd.concat([Y_Pred, Residual.reset_index().iloc[:,[1]]], axis=1)
sns.scatterplot(x='Pred', y='Error', data=temp)
plt.show()
# 잔차의 자기상관성 확인
sm.graphics.tsa.plot_acf(Residual, lags=50, use_vlines=True)
plt.show()
##### 통계량
# 정규분포
# Null Hypothesis: The residuals are normally distributed
Normality = pd.DataFrame([stats.shapiro(Residual)],
index=['Normality'], columns=['Test Statistics', 'p-value']).T
# 등분산성
# Null Hypothesis: Error terms are homoscedastic
Heteroscedasticity = pd.DataFrame([sm.stats.diagnostic.het_goldfeldquandt(Residual, X_Data.values, alternative='two-sided')],
index=['Heteroscedasticity'],
columns=['Test Statistics', 'p-value', 'Alternative']).T
# 자기상관
# Null Hypothesis: Autocorrelation is absent
Autocorrelation = pd.concat([pd.DataFrame(sm.stats.diagnostic.acorr_ljungbox(Residual, lags=[10,50]).iloc[:,0]),
pd.DataFrame(sm.stats.diagnostic.acorr_ljungbox(Residual, lags=[10,50]).iloc[:,1])], axis=1).T
Autocorrelation.index = ['Test Statistics', 'p-value']
Autocorrelation.columns = ['Autocorr(lag10)', 'Autocorr(lag50)']
Error_Analysis = pd.concat([Normality, Heteroscedasticity, Autocorrelation], join='outer', axis=1)
return Error_Analysis