-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRI-EXP.py
194 lines (159 loc) · 7.89 KB
/
RI-EXP.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
# -*- coding: utf-8 -*-
# Date: 2020
# Author: Yanzhe Kang <kyz1994@tju.edu.cn>
# Licence: GPL
# Desc: Reject inference using Extrapolation
import csv
import warnings
import time
import itertools
import pandas as pd
import numpy as np
from scipy import interp
from math import isnan
import matplotlib.pyplot as plt
from itertools import cycle
from sklearn.preprocessing import label_binarize, LabelEncoder, OneHotEncoder
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.semi_supervised import LabelSpreading
from sklearn.semi_supervised import LabelPropagation
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, roc_curve, auc, cohen_kappa_score, \
log_loss, brier_score_loss, hinge_loss, classification_report
from sklearn.externals import joblib
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
def baseline_extrapolation(data_path, bad_sample_num, good_sample_num, reject_sample_num, random_state_for_each_epoch, classifier, resampling_model):
'''Data input'''
warnings.filterwarnings("ignore")
raw_data_train = pd.read_csv(data_path, index_col='ID')
data_bad = raw_data_train[raw_data_train['label'] == 1]
# print data_bad.shape
data_good = raw_data_train[(raw_data_train['label'] == 0)]
data_reject = raw_data_train[raw_data_train['label'] == -1]
data_bad_sampling = data_bad.sample(n=bad_sample_num, random_state=random_state_for_each_epoch)
data_good_sampling = data_good.sample(n=good_sample_num, random_state=random_state_for_each_epoch)
data_train = pd.concat([data_bad_sampling, data_good_sampling], axis=0)
# print("All Data Size:" + str(data_train.shape))
feature_name = list(data_train.columns.values)
# print(feature_name)
s = 0
np.random.seed(s)
sampler = np.random.permutation(len(data_train.values))
data_train_randomized = data_train.take(sampler)
y = data_train_randomized['label'].as_matrix()
data_X = data_train_randomized.drop(['label'], axis=1).iloc[:, :]
data_X = data_X.fillna(data_X.median())
data_X_continue = data_X.iloc[:, :11]
data_X_one_hot = data_X.iloc[:, 11:]
data_X_continue = (data_X_continue - data_X_continue.mean()) / data_X_continue.std()
X = pd.concat([data_X_continue, data_X_one_hot], axis=1).as_matrix()
'''Split train/test data sets'''
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=0)
""" **************************************************** reject ************************************************************"""
data_reject_sampling = data_reject.sample(n=reject_sample_num, random_state=random_state_for_each_epoch)
data_reject_X = data_reject_sampling.drop(['label'], axis=1)
data_reject_X = data_reject_X.fillna(data_X.median())
data_reject_X_continue = data_reject_X.iloc[:, :11]
data_reject_X_one_hot = data_reject_X.iloc[:, 11:]
data_reject_X_continue = (data_reject_X_continue - data_reject_X_continue.mean()) / data_reject_X_continue.std()
X_reject = pd.concat([data_reject_X_continue, data_reject_X_one_hot], axis=1).as_matrix()
y_reject_predict = classifier.fit(X_train, y_train).predict(X_reject)
X_train_and_reject = np.r_[X_train, X_reject]
y_train_and_reject = np.r_[y_train, y_reject_predict]
# print(y_train_and_reject.sum())
'''Supervised Learning'''
y_proba = classifier.fit(X_train_and_reject, y_train_and_reject).predict_proba(X_test)
y_predict = classifier.fit(X_train_and_reject, y_train_and_reject).predict(X_test)
# y_predict = y_proba[:, 1].copy()
# y_predict[y_predict >= 0.2] = 1
# y_predict[y_predict < 0.2] = 0
'''Accuracy'''
accuracy_result = accuracy_score(y_test, y_predict)
# print("Accuracy Score:" + str(accuracy_result))
'''Precision'''
precision_result = precision_score(y_test, y_predict)
# print("Precision Score:" + str(precision_result))
'''Recall'''
recall_result = recall_score(y_test, y_predict)
# print("Recall Score:" + str(recall_result))
'''F1'''
f1_result = f1_score(y_test, y_predict)
# print("F1 Score:" + str(f1_result))
'''Log loss'''
log_loss_result = log_loss(y_test, y_proba[:, 1])
# print("logloss Score:" + str(log_loss_result))
'''Cohen-Kappa'''
cohen_kappa_result = cohen_kappa_score(y_test, y_predict)
# print("Cohen-Kappa Score:" + str(cohen_kappa_result))
'''brier score'''
brier_result = brier_score_loss(y_test, y_predict)
# print("brier Score:" + str(brier_result))
'''AUC and ROC curve'''
fpr, tpr, _ = roc_curve(y_test, y_proba[:, 1])
auc_result = auc(fpr, tpr)
# print("AUC Score:" + str(auc_result))
# plt.figure()
# lw = 2
# plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.4f)' % roc_auc)
# plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel('False Positive Rate')
# plt.ylabel('True Positive Rate')
# plt.title('Receiver operating characteristic example')
# plt.legend(loc="lower right")
# plt.show()
'''K-S Value'''
ks_result = max(tpr - fpr)
'''Classification Report'''
# target_names = ['class 0', 'class 1', 'class 2']
# print(classification_report(y_test, y_predict, target_names=target_names))
'''Confusion Matrix'''
# # Compute confusion matrix
# cnf_matrix = confusion_matrix(y_test, y_predict)
# np.set_printoptions(precision=2)
#
# # Plot non-normalized confusion matrix
# plt.figure()
# plot_confusion_matrix(cnf_matrix, classes=[0, 1], title='Confusion matrix, without normalization')
#
# # Plot normalized confusion matrix
# plt.figure()
# plot_confusion_matrix(cnf_matrix, classes=[0, 1], normalize=True, title='Normalized confusion matrix')
#
# plt.show()
# print("Accuracy Score:" + str(accuracy_result) + " Precision Score:" + str(precision_result) + " Recall Score:" + str(recall_result) +
# " F1 Score:" + str(f1_result) + " logloss Score:" + str(log_loss_result) + " Cohen-Kappa Score:" + str(cohen_kappa_result) +
# " brier Score:" + str(brier_result) + " AUC Score:" + str(auc_result))
return accuracy_result, precision_result, recall_result, f1_result, log_loss_result, cohen_kappa_result, brier_result, ks_result, auc_result