print("-----------------以下为OSELM-KNN(本文)算法(10)---------------") acc_rem = [] #初始化精度列表 for ii in range(10): #data.target = data.target + 1 #stdsc = StandardScaler() #label_size = 0.5 (train_data, iter_data, test_data) = elmUtils.splitDataWithIter(data.data, data.target, label_size, 0.3) iter_y = BvsbUtils.KNNClassifierResult(train_data[0], train_data[1], iter_data[0]) bvsbc = BvsbClassifier(train_data[0], train_data[1], iter_data[0], iter_y, test_data[0], test_data[1], iterNum=0.1) bvsbc.createOSELM(n_hidden=1000, active_function="sigmoid") bvsbc.trainOSELMWithKNNButBvsb() print(f'OSELM-BVSB-KNN 正确率为{bvsbc.score(test_data[0], test_data[1])}') acc_temp = bvsbc.score(test_data[0], test_data[1]) #记录每次的精度 acc_rem.append(acc_temp) #将每次的精度存入列表 for i in acc_rem: print(f'{i*100:0.2f}', ) #打印每次精度 acc_mean = np.mean(acc_rem) #求出平均精度 print("{:.2f}".format(acc_mean * 100)) #打印平均精度 print('---------------------以上为OSELM-BVSB-KNN(10次)算法----------------------'
Y_train = train_data[1].copy() X_iter = iter_data[0].copy() len_iter = len(X_iter) i = 1 while len(X_iter) > (len_iter / 2): nbr = BvsbUtils.KNNClassifier(X_train, Y_train) # KNN # iter_y=nbr.predict(X_iter) pred = nbr.predict_proba(X_iter) iter_y = np.argmax(pred, axis=1) classMax = np.max(pred, axis=1) # 获取未标记样本的最大隶属度 sortIndex = np.argsort(classMax) # 排序后的原下标 iter_index = np.sort(sortIndex[-select_h:]) sort_h_y = iter_y[iter_index] # 返回原来数据置信度最大的h个数 sort_h_data = X_iter[iter_index] len_curr_iter = len(sort_h_y) bvsbc = BvsbClassifier(X_train, Y_train, sort_h_data, sort_h_y, test_data[0], test_data[1], iterNum=0.1) bvsbc.createELM(n_hidden=hidden_nums, activation_func="tanh", alpha=1.0, random_state=0) _data_index = bvsbc.fitAndGetUpdateDataIndex(limit=int(0.2 * len_curr_iter)) if len(_data_index) != 0: X_train = np.r_[bvsbc.X_train, sort_h_data[_data_index]] Y_train = np.r_[bvsbc.Y_train, sort_h_y[_data_index]] X_iter = np.delete(X_iter, iter_index[_data_index], axis=0) else: print("没有数据被加入训练集,训练结束") break print(f"第{ii} 次训练,第{i}次迭代: 正确率为:{bvsbc.score(test_data[0], test_data[1])}") i += 1 acc_temp = bvsbc.score(test_data[0], test_data[1]) # 记录每次的精度 acc_rem.append(acc_temp) # 将每次的精度存入列表 print("***************ELM-BVSB-KNN加权算法(10次精度)********************")
from elm import BvsbClassifier, BvsbUtils from sklearn import datasets from elm import elmUtils from sklearn.preprocessing import StandardScaler import time print("---------OSELM-BVSB-----------") data = elmUtils.readDataFileToData("data/zoo.data", targetIndex=0) print(f'数据集大小问{data.target.size}') data.data = StandardScaler().fit_transform(data.data) (train, iter, test) = elmUtils.splitDataWithIter(data.data, data.target, 0.05, 0.3) print(f'训练集大小为{train[1].size}') print(f'迭代训练集大小为{iter[1].size}') print(f'测试集大小为{test[1].size}') tic = time.perf_counter_ns() bvsbc = BvsbClassifier(train[0], train[1], iter[0], iter[1], test[0], test[1], iterNum=0.1) bvsbc.createOSELM(n_hidden=1000) bvsbc.trainOSELMWithoutKNN() toc = time.perf_counter_ns() print(f'OSELM-BVSB-KNN 正确率为{bvsbc.score(test[0], test[1])}') print(f'OSELM-BVSB-KNN 项目用时:{(toc - tic) / 1000 / 1000} ms')
#---------------------------数据集--------------------------------- print("-----------------以下为ELM--KNN(本文)算法(10次)---------------") stdc = StandardScaler() label_size = 0.05 #已标记样本比例,分别取0.05-0.1-0.2-0.3-0.4 acc_rem = [] #初始化精度列表 for ii in range(10): data.data = stdc.fit_transform(data.data / 16.0) (train_data, iter_data, test_data) = elmUtils.splitDataWithIter(data.data, data.target, label_size, 0.3) iter_y = BvsbUtils.KNNClassifierResult(train_data[0], train_data[1], iter_data[0]) # KNN bvsbc = BvsbClassifier(train_data[0], train_data[1], iter_data[0], iter_y, test_data[0], test_data[1], iterNum=0.1) bvsbc.createELM(n_hidden=1000, activation_func="tanh", alpha=1.0, random_state=0) bvsbc.X_test = test_data[0] bvsbc.Y_test = test_data[1] bvsbc.trainELMWithKNNButBvsb() print("+++++++++++++++++++") print(bvsbc.score(test_data[0], test_data[1])) acc_temp = bvsbc.score(test_data[0], test_data[1]) #记录每次的精度
data = datasets.load_digits() stdc = StandardScaler() # 均值归一化 label_size = 0.3 data.data = stdc.fit_transform(data.data / 16.0) train, iter, test = elmUtils.splitDataWithIter(data.data, data.target, label_size, 0.2) Y_iter = BvsbUtils.KNNClassifierResult(train[0], train[1], iter[0]) print(Y_iter.size) tic = time.perf_counter_ns() bvsbc = BvsbClassifier(train[0], train[1], iter[0], Y_iter, test[0], test[1], iterNum=0.1) bvsbc.createELM(n_hidden=1000, activation_func="sigmoid", alpha=1.0, random_state=0) bvsbc.X_test = test[0] bvsbc.Y_test = test[1] bvsbc.trainELMWithKNNButBvsb() toc = time.perf_counter_ns() print(bvsbc.score(test[0], test[1])) print("ELM-BVSB 项目用时:%d" % ((toc - tic) / 1000 / 1000))
label_size = 0.3 #已标记样本比例,分别取0.05-0.1-0.2-0.3-0.4 acc_rem = [] #初始化精度列表 for ii in range(10): #循环次数 print(f'数据集大小问{data.target.size}') data.data = StandardScaler().fit_transform(data.data) (train, iter, test) = elmUtils.splitDataWithIter(data.data, data.target, 0.2, 0.3) print(f'训练集大小为{train[1].size}') print(f'迭代训练集大小为{iter[1].size}') print(f'测试集大小为{test[1].size}') #tic = time.perf_counter_ns() bvsbc = BvsbClassifier(train[0], train[1], iter[0], iter[1], test[0], test[1], iterNum=0.1) bvsbc.createOSELM(n_hidden=1000) bvsbc.trainOSELMWithoutKNN() #toc = time.perf_counter_ns() print(f'OSELM-BVSB 正确率为{bvsbc.score(test[0], test[1])}') # print(f'OSELM-BVSB项目用时:{(toc - tic) / 1000 / 1000} ms') acc_temp = bvsbc.score(test[0], test[1]) #记录每次的精度 acc_rem.append(acc_temp) #将每次的精度存入列表 print("*****************************************************") for i in acc_rem: print(f'{i*100:0.2f}', ) #打印每次精度
from sklearn.svm import SVC from elm import BvsbClassifier, BvsbUtils from elm import elmUtils print("---------OSELM-BVSB-KNN-----------") data = datasets.load_digits() #data=elmUtils.readDataFileToData("data/zoo.data", targetIndex=0) #data=elmUtils.readDataFileToData("./data/abalone.data", targetIndex=-1, transformIndex=[0]) stdc = StandardScaler() #data.data, data.target = stdc.fit_transform(data.data) / 16.0, data.target #data.data = BvsbUtils.dimensionReductionWithPCA(data.data, 0.95) label_size = 0.05 (train_data, iter_data, test_data) = elmUtils.splitDataWithIter(data.data, data.target, label_size, 0.2) iter_y = BvsbUtils.SVMClassifierResult(train_data[0], train_data[1], iter_data[0]) tic = time.perf_counter_ns() bvsbc = BvsbClassifier(train_data[0], train_data[1], iter_data[0], iter_y, test_data[0], test_data[1], iterNum=0.2) bvsbc.createOSELM(n_hidden=1000,active_function="sigmoid") bvsbc.trainOSELMWithBvsb() toc = time.perf_counter_ns() tdata=np.vstack((iter_data[0],test_data[0])) ttarget=np.hstack((iter_data[1],test_data[1])) print(ttarget.shape) print(f'OSELM-BVSB-KNN 正确率为{bvsbc.score(tdata,ttarget)}') print(f'OSELM-BVSB-KNN 正确率为{bvsbc.score(test_data[0],test_data[1])}') print(f'OSELM-BVSB-KNN 项目用时:{(toc - tic) / 1000 / 1000} ms')
#data = sklearn.datasets.fetch_covtype() #data = datasets.load_iris() #data = datasets.load_digits() #ata = datasets.load_wine()#############################################有问题,跑不了 #data = datasets.load_breast_cancer() stdc=StandardScaler() #data.data,data.target=stdc.fit_transform(data.data)/16.0, data.target #data.data=BvsbUtils.dimensionReductionWithPCA(data.data,0.95) label_size=0.3 (train_data, iter_data, test_data) = elmUtils.splitDataWithIter(data.data,data.target, label_size, 0.2) iter_y=BvsbUtils.KNNClassifierResult(train_data[0],train_data[1],iter_data[0]) tic = time.perf_counter_ns() bvsbc = BvsbClassifier(train_data[0], train_data[1], iter_data[0], iter_y, test_data[0], test_data[1], iterNum=0.1) bvsbc.createOSELM(n_hidden=1000) bvsbc.trainOSELMWithBvsb() toc = time.perf_counter_ns() acc_temp = bvsbc.score(test_data[0], test_data[1]) #记录每次的精度 acc_rem.append(acc_temp) #将每次的精度存入列表 time_temp = (toc - tic) / 1000 / 1000 #记录每次的时间 time_rem.append(time_temp) #将每次的时间存入列表 print(f'OSELM-BVSB-KNN 正确率为{bvsbc.score(test_data[0], test_data[1])}') print(f'OSELM-BVSB-KNN 项目用时:{(toc - tic) / 1000 / 1000} ms') for i in acc_rem: print(f'每一次的精度:{i*100:0.2f}',) #打印每次精度