def eval_band_cv(X, y, times=10): """ :param X: :param y: :param times: n times k-fold cv :return: knn/svm/elm=>(OA+std, Kappa+std) """ p = Processor() img_ = maxabs_scale(X) estimator = [ KNN(n_neighbors=5), SVC(C=1e4, kernel='rbf', gamma=1.), ELM_Classifier(200) ] estimator_pre, y_test_all = [[], [], []], [] for i in range(times): # repeat N times K-fold CV skf = StratifiedKFold(n_splits=3, shuffle=True) for train_index, test_index in skf.split(img_, y): X_train, X_test = X[train_index], X[test_index] y_train, y_test = y[train_index], y[test_index] y_test_all.append(y_test) for c in range(3): estimator[c].fit(X_train, y_train) estimator_pre[c].append(estimator[c].predict(X_test)) clf = ['knn', 'svm', 'elm'] score = [] for z in range(3): ca, oa, aa, kappa = p.save_res_4kfolds_cv(estimator_pre[z], y_test_all, file_name=clf[z] + 'score.npz', verbose=True) score.append([oa, kappa]) return score
def eval_feature_cv(self, X, y, times=3, test_size=0.95, random_state=None): print(X.shape) # X = normalize(X) p = Processor() estimator = [KNN(n_neighbors=5), SVC(C=1e6, kernel='rbf')] estimator_pre, y_test_all = [[], []], [] for i in range(times): # repeat N times K-fold CV X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=test_size, random_state=random_state, shuffle=True, stratify=y) # train_index, test_index = p_Cora.stratified_train_test_index(y, test_size) # X_train, X_test = X[train_index], X[test_index] # y_train, y_test = y[train_index], y[test_index] y_test_all.append(y_test) for c in range(len(estimator)): estimator[c].fit(X_train, y_train) y_pre = estimator[c].predict(X_test) estimator_pre[c].append(y_pre) # score_Cora = [] score_dic = { 'knn': { 'ca': [], 'oa': [], 'aa': [], 'kappa': [] }, 'svm': { 'ca': [], 'oa': [], 'aa': [], 'kappa': [] } } key_ = ['knn', 'svm'] for z in range(len(estimator)): ca, oa, aa, kappa = p.save_res_4kfolds_cv(estimator_pre[z], y_test_all, file_name=None, verbose=False) # score_Cora.append([oa, kappa, aa, ca]) score_dic[key_[z]]['ca'] = ca score_dic[key_[z]]['oa'] = oa score_dic[key_[z]]['aa'] = aa score_dic[key_[z]]['kappa'] = kappa return score_dic
def eval_band_cv(X, y, times=10, test_size=0.95): p = Processor() estimator = [KNN(n_neighbors=3), SVC(C=1e5, kernel='rbf', gamma=1.)] estimator_pre, y_test_all = [[], []], [] for i in range(times): # repeat N times K-fold CV X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=test_size, random_state=None, shuffle=True, stratify=y) # skf = StratifiedKFold(n_splits=20, shuffle=True) # for test_index, train_index in skf.split(img_correct, gt_correct): # X_train, X_test = img_correct[train_index], img_correct[test_index] # y_train, y_test = gt_correct[train_index], gt_correct[test_index] y_test_all.append(y_test) for c in range(len(estimator)): estimator[c].fit(X_train, y_train) y_pre = estimator[c].predict(X_test) estimator_pre[c].append(y_pre) # score = [] score_dic = { 'knn': { 'ca': [], 'oa': [], 'aa': [], 'kappa': [] }, 'svm': { 'ca': [], 'oa': [], 'aa': [], 'kappa': [] } } key_ = ['knn', 'svm'] for z in range(len(estimator)): ca, oa, aa, kappa = p.save_res_4kfolds_cv(estimator_pre[z], y_test_all, file_name=None, verbose=False) # score.append([oa, kappa, aa, ca]) score_dic[key_[z]]['ca'] = ca score_dic[key_[z]]['oa'] = oa score_dic[key_[z]]['aa'] = aa score_dic[key_[z]]['kappa'] = kappa return score_dic
all_aa, all_oa, all_kappa, all_ca = [], [], [], [] skf = StratifiedKFold(n_splits=3, random_state=55, shuffle=True) for j in range(X.shape[0]): X_ = X[j] y_pres = [] y_tests = [] for i in range(10): for train_index, test_index in skf.split(X_, y): # [index] X_train, X_test = X_[train_index], X_[test_index] # [index] y_train, y_test = y[train_index], y[test_index] elm = BaseELM(500, C=1e8) y_predicted = elm.fit(X_train, y_train).predict(X_test) y_pres.append(y_predicted) y_tests.append(y_test) ca, oa, aa, kappa = p.save_res_4kfolds_cv(np.asarray(y_pres), np.asarray(y_tests), file_name=None, verbose=True) all_oa.append(oa) all_aa.append(aa) all_kappa.append(kappa) all_ca.append(ca) np.savez('./experimental_results/Acc_comparison/IndianPines-Box_plot_data.npz', ca=all_ca, oa=all_oa, aa=all_aa, kappa=all_kappa) print('DONE')