forked from yc14600/beta3_IRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_irt_data.py
280 lines (228 loc) · 10.1 KB
/
gen_irt_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import division
import os
import sys
import pandas as pd
import argparse
import numpy as np
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.ensemble import RandomForestClassifier,AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis,LinearDiscriminantAnalysis
from sklearn.metrics import *
from models.tree import MyDecisionTreeClassifier
from data.synthetic import get_synthetic_data
from data.load_data import load_mnist_fashion_data
from visualization.plots import *
classifiers = {
'MLP': MLPClassifier(max_iter=500,hidden_layer_sizes=(256,64,)),##
#'MLP2': MLPClassifier(max_iter=500,hidden_layer_sizes=(128,32,)),
#'RBF SVM': SVC(probability=True),
#'Linear SVM': SVC(kernel='linear', probability=True),
'Logistic Regression': LogisticRegression(),##
'Decision Tree': MyDecisionTreeClassifier(),##
'Nearest Neighbors': KNeighborsClassifier(3),##
#'Gaussian process': GaussianProcessClassifier(1.0 * RBF(1.0)),
'Random Forest': RandomForestClassifier(),
'Adaboost': AdaBoostClassifier(),
'Naive Bayes': GaussianNB(),#
'LDA': LinearDiscriminantAnalysis(),
'QDA': QuadraticDiscriminantAnalysis(),
#'Weak Classifier': 'weak',
#'Strong Classifier': 'strong',
'Positive Classifier': 'positive',
'Negative Classifier': 'negative',
'Constant Classifier': 'uncertain',##
#'Perturb-MLP': 'perturb-MLP',
#'Perturb-LR': 'perturb-Logistic Regression'
}
def weak_baseline(y_test):
probas = np.ones(len(y_test)).reshape(-1, 1)
probas[y_test == 1] = 0.0
probas = np.hstack((1.0 - probas, probas))
predictions = np.logical_not(y_test)
return probas, predictions.astype(int)
def strong_baseline(y_test):
probas = np.zeros(len(y_test)).reshape(-1, 1)
probas[y_test == 1] = 1.0
probas = np.hstack((1.0 - probas, probas))
predictions = np.logical_not(y_test)
return probas, predictions.astype(int)
def positive_baseline(y_test):
probas = np.ones(len(y_test)).reshape(-1, 1)
predictions = np.ones(len(y_test))
probas = np.hstack((1.0 - probas, probas))
return probas, predictions.astype(int)
def negative_baseline(y_test):
probas = np.zeros(len(y_test)).reshape(-1, 1)
predictions = np.zeros(len(y_test))
probas = np.hstack((1.0 - probas, probas))
return probas, predictions.astype(int)
def uncertain_baseline(y_test):
probas = 0.5 * np.ones(len(y_test)).reshape(-1, 1)
#probas = np.random.normal(loc=0.5,scale=0.1,size=len(y_test)).reshape(-1,1)
probas = np.hstack((1.0 - probas, probas))
#predictions = np.ones(len(y_test))
#predictions = np.argmax(probas,axis=1)
predictions = np.random.choice(2,len(y_test))
#print(predictions.shape)
return probas, predictions.astype(int)
def get_probas_and_predictions(classifier, X_test, y_test, pclassifier=None):
if classifier == 'weak':
return weak_baseline(y_test)
if classifier == 'strong':
return strong_baseline(y_test)
if classifier == 'positive':
return positive_baseline(y_test)
if classifier == 'negative':
return negative_baseline(y_test)
if classifier == 'uncertain':
return uncertain_baseline(y_test)
if isinstance(classifier,str) and 'perturb' in classifier:
print(classifier)
probas = pclassifier.predict_proba(X_test)
#predictions = pclassifier.predict(X_test).astype(int)
n = len(y_test)
pidx = np.random.choice(n, int(0.4 * n), replace=False)
probas[pidx,0] = np.random.uniform(0.,1.,size=len(pidx))
probas[pidx,1] = 1 - probas[pidx,0]
predictions = np.argmax(probas,axis=1)
#print(probas.shape,predictions.shape)
return probas,predictions
probas = classifier.predict_proba(X_test)
predictions = classifier.predict(X_test).astype(int)
return probas, predictions
def evaluate(classifier, X_test, y_test,recal,pclassifier=None):
if isinstance(classifier,str) and 'perturb' in classifier:
probas, predictions = get_probas_and_predictions(classifier, X_test, y_test, pclassifier)
else:
probas, predictions = get_probas_and_predictions(classifier, X_test, y_test)
incorrect = predictions != y_test
correct = predictions == y_test
if recal:
lr = LogisticRegression()
lr.fit(probas,predictions)
probas = lr.predict_proba(probas)
chance = probas[np.arange(len(probas)), predictions]
chance[incorrect] = 1.0 - chance[incorrect]
return probas[:, 1], chance, np.array([[np.sum(correct) + 1, np.sum(
incorrect) + 1]]), correct, incorrect
def cl_info(classifier, X_train, y_train, X_test, y_test,recal=False):
pclassifier = None
if not isinstance(classifier,str):
classifier.fit(X_train, y_train)
elif 'perturb' in classifier:
pclassifier = classifiers[classifier.split('-')[1]]
pclassifier.fit(X_train, y_train)
p, p_irt, prior, correct, incorrect = evaluate(classifier, X_test, y_test,recal,pclassifier)
return p, p_irt, prior, correct, incorrect
def gen_IRT_data(dataset_type,size,nfrac,seed,results_path,cl1,cl2,train_noise=True,recal=False):
if not dataset_type in ['moons','clusters','circles','fashion','mnist']:
raise TypeError('Non-supported dataset type!')
if not os.path.exists(results_path):
os.makedirs(results_path)
np.random.seed(seed)
if dataset_type in ['fashion','mnist']:
#print(nfrac,(100*nfrac))
spec_name = dataset_type+'_s'+str(size)+'_f'+str(int(100*nfrac))+'_sd'+str(seed)+'_cl'+str(cl1)+str(cl2)
else:
spec_name = dataset_type+'_s'+str(size)+'_f'+str(int(100*nfrac))+'_sd'+str(seed)
if recal:
spec_name += '_cal'
spec_name+='_m'+str(len(classifiers))
if train_noise:
tnfrac = nfrac
else:
tnfrac = 0.
if dataset_type == 'fashion':
X,y,X_test,y_test,nidx = load_mnist_fashion_data(train_size=1000,test_size=size,\
dpath='../data/fashion',noise_frac=nfrac,train_noise_frac=tnfrac,\
class1=cl1, class2=cl2,seed=seed)
elif dataset_type == 'mnist':
X,y,X_test,y_test,nidx = load_mnist_fashion_data(train_size=1000,test_size=size,\
dpath='../data/mnist',noise_frac=nfrac,train_noise_frac=tnfrac,\
class1=cl1,class2=cl2,seed=seed)
else:
#print(tnfrac)
X, y, _ = get_synthetic_data(dataset_type, size,noise_frac=tnfrac)
X_test, y_test, nidx = get_synthetic_data(dataset_type, size, noise_frac=nfrac)
correct = np.ones(len(y_test))
incorrect = np.ones(len(y_test))
probas = []
probas_irt = []
cl_priors = []
labels = []
accuracy = []
f1 = []
for classifier in classifiers:
labels.append(classifier)
cl = classifiers[classifier]
p, p_irt, prior, c, i = cl_info(cl, X, y, X_test, y_test,recal)
accuracy.append(c.sum()*1./len(y_test))
pred = np.zeros_like(c)
pred[c] = y_test[c]
pred[i] = 1 - y_test[i]
f1.append(f1_score(y_test,pred))
correct += c
incorrect += i
probas.append(p)
probas_irt.append(p_irt)
cl_priors.append(prior)
if dataset_type in ['moons','clusters']:
fig = plot_probabilities(X_test, [y_test], ['labels'], 'original')
fig.savefig(os.path.join(results_path, 'labels_{}.pdf'.format(spec_name)))
probas = np.vstack(probas)
probas_irt = np.vstack(probas_irt)
probas_irt = np.clip(probas_irt, 1e-3, 1. - 1e-3)
it_priors = np.hstack((incorrect.reshape(-1, 1), correct.reshape(-1, 1)))
cl_priors = np.vstack(cl_priors)
sdata = pd.DataFrame()
pdata = pd.DataFrame()
acu = pd.DataFrame()
acu['classifier'] = classifiers.keys()
acu['accuracy'] = 0.
acu['f1_score'] = 0.
for i,cname in enumerate(classifiers.keys()):
print(i,cname,accuracy[i])
sdata[cname] = probas_irt[i]
pdata[cname] = probas[i]
acu.loc[i,'accuracy'] = accuracy[i]
acu.loc[i,'f1_score'] = f1[i]
print('average response:',np.mean(probas_irt[i]))
sdata.to_csv('irt_data_'+spec_name+'.csv',index=False)
pdata.to_csv('predict_data_prob_'+spec_name+'.csv',index=False)
acu.to_csv(results_path+'/model_acu_'+spec_name+'.csv',index=False)
xtest = pd.DataFrame(index=range(size))
if not dataset_type in ['fashion','mnist']:
xtest['x'] = X_test[:,0]
xtest['y'] = X_test[:,1]
xtest['noise'] = 0
xtest['label'] = y_test
if not nidx is None:
xtest.loc[nidx,'noise'] = 1
print(xtest.shape)
#print(nidx)
xtest.to_csv('xtest_'+spec_name+'.csv',index=False)
def str2bool(x):
if x.lower() == 'false':
return False
else:
return True
parser = argparse.ArgumentParser()
parser.add_argument('-ds','--dataset', default='moons', type=str, help='can be moons, clusters, mnist,fashion')
parser.add_argument('-rp','--result_path', default='./results/', type=str, help='result path')
parser.add_argument('-dsz','--data_size', default=400, type=int, help='number of data points in each class')
parser.add_argument('-nf','--noise_fraction', default=0.2, type=float, help='noise fraction')
parser.add_argument('-cl1','--class1', default=7, type=int, help='specify first class in mnist or fashion mnist')
parser.add_argument('-cl2','--class2', default=9, type=int, help='specify second class in mnist or fashion mnist')
parser.add_argument('-tn','--train_noise', default=False, type=str2bool, help='if inject noise in training set, set to True')
parser.add_argument('-sd','--seed', default=42, type=int, help='random seed')
args = parser.parse_args()
print('seed',args.seed)
np.random.seed(args.seed)
gen_IRT_data(args.dataset, args.data_size, args.noise_fraction, args.seed, \
args.result_path+args.dataset,args.class1,args.class2,args.train_noise)