def get_classifier(self, traindata, kf): x_tr, x_te, y_tr, y_te = fac.to_kfold(traindata, kf) acc_max, bestK, acc = 0, 0, [[] for a in range(kf)] for i in range(kf): # print('DOAO round', i, 'begin') # svm 00 print('test00') clf_svm = SVC() clf_svm.fit(x_tr[i], y_tr[i].ravel()) label_svm = clf_svm.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_svm)[0]) # KNN 01 print('test01') acc_k = [] aux_k = [3, 5, 7] # for k in range(3, 12, 2): for k in aux_k: clf_knn = KNN_GPU(k=k) clf_knn.fit(x_tr[i], y_tr[i]) label_knn = clf_knn.predict(x_te[i]) acc_k.append(fac.get_acc(y_te[i], label_knn)[0]) acc[i].append(max(acc_k)) bestK = aux_k[acc_k.index(max(acc_k))] # LR 02 print('test02') clf_lr = LogisticRegression() clf_lr.fit(x_tr[i], y_tr[i]) label_LR = clf_lr.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_LR)[0]) # XgBoost 03 print('test03') clf_xgb = DecisionTreeClassifier() clf_xgb.fit(x_tr[i], y_tr[i]) label_xgb = clf_xgb.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_xgb)[0]) # RF 04 print('test04') clf_rf = TGBMClassifier() clf_rf.fit(x_tr[i], y_tr[i]) label_rf = clf_rf.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_rf)[0]) print('DOAO round', i, 'end') acc = np.array(acc) acc_mean = acc.mean(axis=0) # fun_best = np.where(acc_mean == max(acc_mean)) fun_best = np.argmax(acc_mean) return fun_best, bestK
def get_classifier(self, train, kf): x_tr, x_te, y_tr, y_te = fac.to_kfold(train, kf) acc_max, bestK, acc = 0, 0, [[] for a in range(kf)] for i in range(kf): # print('DECOC round', i, 'begin') # svm 00 clf_svm = SVC() clf_svm.fit(x_tr[i], y_tr[i].ravel()) label_svm = clf_svm.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_svm)[0]) # KNN 01 acc_k = [] aux_k = [3, 5, 7] # for k in range(3, 12, 2): for k in aux_k: clf_knn = KNN_GPU(k=k) clf_knn.fit(x_tr[i], y_tr[i]) label_knn = clf_knn.predict(x_te[i]) acc_k.append(fac.get_acc(y_te[i], label_knn)[0]) acc[i].append(max(acc_k)) bestK = aux_k[acc_k.index(max(acc_k))] # # LR 02 # clf_lr = LR_GPU() # clf_lr.fit(x_tr[i], y_tr[i]) # label_LR = clf_lr.predicted(x_te[i]) # acc[i].append(fac.get_acc(y_te[i], label_LR)[0]) # LR 02 clf_lr = LogisticRegression() clf_lr.fit(x_tr[i], y_tr[i]) label_LR = clf_lr.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_LR)[0]) # CART 03 clf_cart = DecisionTreeClassifier() clf_cart.fit(x_tr[i], y_tr[i]) label_cart = clf_cart.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_cart)[0]) # # RF 04 clf_rf = TGBMClassifier() clf_rf.fit(x_tr[i], y_tr[i].ravel()) label_rf = clf_rf.predict(x_te[i]) acc[i].append(fac.get_acc(y_te[i], label_rf)[0]) print('DECOC round', i, 'end') acc = np.array(acc) acc_mean = acc.mean(axis=0) # fun_best = np.where(acc_mean == max(acc_mean)) fun_best = np.argmax(acc_mean) return fun_best, bestK
def fun_predict(self, x_te, C, D, L): print('func_predict') num = len(D) cf = C[0] ck = C[1] allpre = np.zeros((len(x_te), num)) for i in range(num): train = D[i] traindata = train[:, 0:-1] trainlabel = train[:, -1] if cf[i] == 0: # svm print('SVM predict') clf_svm = SVC() clf_svm.fit(traindata, trainlabel.ravel()) label_svm = clf_svm.predict(x_te) allpre[:, i] = label_svm elif cf[i] == 1: # knn clf_knn = KNN_GPU(k=ck[i]) clf_knn.fit(traindata, trainlabel) label_knn = clf_knn.predict(x_te) allpre[:, i] = label_knn elif cf[i] == 2: # LR print('LR predict') clf_lr = LogisticRegression() clf_lr.fit(traindata, trainlabel.ravel()) label_LR = clf_lr.predict(x_te) allpre[:, i] = label_LR elif cf[i] == 3: # CART print('CART predict') clf_xgb = DecisionTreeClassifier() clf_xgb.fit(traindata, trainlabel) label_xgb = clf_xgb.predict(x_te) allpre[:, i] = label_xgb elif cf[i] == 4: # Rf print('RF predict') clf_rf = TGBMClassifier() clf_rf.fit(traindata, trainlabel.ravel()) label_rf = clf_rf.predict(x_te) allpre[:, i] = label_rf else: print('error !!!! DOAO.fun_predict') label = L[i] for j in range(len(x_te)): allpre[j, i] = label[0] if allpre[j, i] == 0 else label[1] # print('predict end for') pre = mode(allpre, axis=1)[0] return pre
def funcPreEDOVO(self, x_test, y_test, C, D): numC = np.asarray(C).shape[0] num_set = len(y_test) allpre = np.zeros([num_set, numC]) for i in range(numC): train = D[i] traindata = np.array(train[:, 0:-1]) trainlabel = np.array(train[:, -1], dtype='int64') if C[i, 0] == 0: print('test0') # svm clf_svm = SVC() clf_svm.fit(traindata, trainlabel.ravel()) label_svm = clf_svm.predict(x_test) allpre[:, i] = label_svm elif C[i, 0] == 1: # print('test1') # knn clf_knn = KNN_GPU(k=C[i][1]) # clf_knn = KNN_torch(k=C[i][1]) clf_knn.fit(traindata, trainlabel) label_knn = clf_knn.predict(x_test) allpre[:, i] = label_knn.ravel() elif C[i, 0] == 2: print('test2') # LR clf_lr = LogisticRegression() clf_lr.fit(traindata, trainlabel) label_LR = clf_lr.predict(x_test) allpre[:, i] = label_LR # # LR # clf_lr = LR_GPU() # clf_lr.fit(traindata, trainlabel) # label_LR = clf_lr.predicted(x_test) # allpre[:, i] = label_LR elif C[i, 0] == 3: print('test3') # CART clf_cart = DecisionTreeClassifier() clf_cart.fit(traindata, trainlabel) label_cart = clf_cart.predict(x_test) allpre[:, i] = label_cart elif C[i, 0] == 4: print('test4') # RandomForest clf_ada = TGBMClassifier() clf_ada.fit(traindata, trainlabel.ravel()) label_ada = clf_ada.predict(x_test) allpre[:, i] = label_ada else: print('error !!!! DECOC.funcPreEDOVO') return allpre