-
Notifications
You must be signed in to change notification settings - Fork 0
/
2dtraining.py
157 lines (132 loc) · 5.71 KB
/
2dtraining.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
########################### 2d Simple RF training set with no imbalance ############################
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.utils import resample
import proc as prc
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import log_loss
from sklearn.metrics import roc_auc_score
from collections import OrderedDict
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve,auc
from sklearn.model_selection import train_test_split
df_train=pd.read_csv("F:/INF552/hw3/aps_failure_training_set.csv")#skiprows=20,index_col=21)
df_test=pd.read_csv("C:/Users/Shanu/PycharmProjects/APS/aps_failure_test_set.csv")
# Separate majority and minority classes
df_majority = df_train[df_train.classs == "neg"]
df_minority = df_train[df_train.classs == "pos"]
# Upsample minority class
df_minority_upsampled = resample(df_minority,
replace=True, # sample with replacement
n_samples=59000, # to match majority class 59000
random_state=123) # reproducible results
# Combine majority class with upsampled minority class
df_upsampled = pd.concat([df_majority, df_minority_upsampled])
# Display new class counts
print(df_upsampled.classs.value_counts())
# Separate majority and minority classes
df_majority_test = df_test[df_test.classs == "neg"]
df_minority_test = df_test[df_test.classs == "pos"]
# Upsample minority class
df_minority_upsampled_test = resample(df_minority_test,
replace=True, # sample with replacement
n_samples=15625, # to match majority class 59000
random_state=123) # reproducible results
# Combine majority class with upsampled minority class
df_upsampled_test = pd.concat([df_majority_test, df_minority_upsampled_test])
# Display new class counts
print(df_upsampled_test.classs.value_counts())
df_upsampled.replace('na',0,inplace=True)
df_upsampled_test.replace('na',0,inplace=True)
# df_train.replace('neg',0,inplace=True)
# df_test.replace('pos',1,inplace=True)
X_train=df_upsampled.values[:,1:171]
Y_train=df_upsampled.values[:,:1]
# X_test=df_upsampled_test.values[:,1:171]
# Y_test=df_upsampled_test.values[:,:1]
# X_train,X_test,Y_train,Y_test=train_test_split(X_train_prime,Y_train_prime,test_size=0.2, random_state=42)
model=RandomForestClassifier(max_depth=5)
model.fit(X_train,Y_train.ravel())
model_prob = model.predict_proba(X_train)
score=log_loss(Y_train,model_prob)
# score_mean=mean_squared_error(Y_test,model.predict(X_test))
print("Score:",score)
# print("MSE:",score_mean)
print("model_prob",model_prob)
model_prob=model_prob.reshape(1,-1)
#
rf = RandomForestClassifier(max_depth=3,oob_score=True)
rf_enc = OneHotEncoder()
rf_lm = LogisticRegression()
rf.fit(X_train, Y_train.ravel())
rf_enc.fit(rf.apply(X_train))
model_used=rf_lm.fit(rf_enc.transform(rf.apply(X_train)), Y_train.ravel())
preds=rf.predict(X_train)
print(type(model_used))
y_pred_rf_lm = rf_lm.predict_proba(rf_enc.transform(rf.apply(X_train)))[:, 1]
fpr_rf_lm, tpr_rf_lm, _ = roc_curve(Y_train, y_pred_rf_lm,pos_label='pos')
roc_auc = auc(fpr_rf_lm, tpr_rf_lm)
plt.plot(fpr_rf_lm, tpr_rf_lm, label=str(roc_auc))
plt.title('ROC curve-Train Set (With No Imbalance), AUC: '+str(roc_auc))
plt.xlabel('False positive rate')
plt.plot([0, 1], [0, 1], 'k--')
# plt.legend('AUC:'str(round(roc_auc)))
plt.ylabel('True positive rate')
print(roc_auc)
# RANDOM_STATE = 123
#
# ensemble_clfs = [
# ("RandomForestClassifier, max_features='sqrt'",
# RandomForestClassifier(warm_start=True, oob_score=True,
# max_features="sqrt",
# random_state=RANDOM_STATE)),
# ("RandomForestClassifier, max_features='log2'",
# RandomForestClassifier(warm_start=True, max_features='log2',
# oob_score=True,
# random_state=RANDOM_STATE)),
# ("RandomForestClassifier, max_features=None",
# RandomForestClassifier(warm_start=True, max_features=None,
# oob_score=True,
# random_state=RANDOM_STATE))
# ]
#
# # Map a classifier name to a list of (<n_estimators>, <error rate>) pairs.
# error_rate = OrderedDict((label, []) for label, _ in ensemble_clfs)
#
# # Range of `n_estimators` values to explore.
# min_estimators = 15
# max_estimators = 175
#
# for label, clf in ensemble_clfs:
# for i in range(min_estimators, max_estimators + 1):
# clf.set_params(n_estimators=i)
# clf.fit(X_train, Y_train.ravel())
#
# # Record the OOB error for each `n_estimators=i` setting.
# oob_error = 1 - clf.oob_score_
# error_rate[label].append((i, oob_error))
#
# # print("Score:",model_used.oob_score_)
# # error=1-model_used.oob_score_
# for label, clf_err in error_rate.items():
# xs, ys = zip(*clf_err)
# print("XS:",xs,"\t YS:",ys)
plt.show()
confusion_matrx=confusion_matrix(Y_train,preds)
print(confusion_matrx)
sns.heatmap(confusion_matrx,cmap="YlGnBu",annot=True,linewidths=.5,fmt='d')
plt.show()
missclassifications=0
for i in range(0,2):
for j in range(0,2):
if i!=j:
missclassifications+=confusion_matrx[i][j]
print(missclassifications)
print("missclassification rate:", missclassifications/(len(Y_train)))
print("OOB error:",(1-rf.oob_score_))
#