def test_elmk_diabetes(): # load dataset data = elm.read("elmTestData/diabetes.data") # create a regressor elmk = elm.ELMKernel() try: # search for best parameter for this dataset # elmk.search_param(data, cv="kfold", of="rmse") # split data in training and testing sets tr_set, te_set = elm.split_sets(data, training_percent=.8, perm=True) #train and test tr_result = elmk.train(tr_set) te_result = elmk.test(te_set) except: ERROR = 1 else: ERROR = 0 assert (ERROR == 0)
def run_model(X_train, X_test): print("\t\t\txxxxx ELM (polynomial) model xxxxx") # set parameters # params = ['poly', 0.5, [5]] # create a classifier ** mostly only classifier elmk = elm.ELMKernel() elmk.search_param(X_train, cv="kfold", of="accuracy", kf=["poly"], eval=10) #train and test # results are Error objects tr_result = elmk.train(np.array(X_train)) te_result = elmk.test(np.array(X_test)) # print(te_result.expected_targets) # print(te_result.predicted_targets) test_result = [1 if x > 0.5 else 0 for x in te_result.predicted_targets] # print(test_result) print( 'Accuracy: ' + str(100 * metrics.accuracy_score(te_result.expected_targets, test_result)) + '%') print('F1 score: ' + str(metrics.f1_score(te_result.expected_targets, test_result))) fpr, tpr, thresholds = metrics.roc_curve(te_result.expected_targets, test_result, pos_label=1) print("Area under curve: " + str(metrics.auc(fpr, tpr)))
def main(): # Load in 2D velocity data velocity = data.load_data() # data.example_of_data(velocity) # form testing and training sets for velocity data X_train, y_train, X_test, y_test = data.form_train_test_sets(velocity) # Data transformation #print(X_test[0]['u'].shape) print("len of y", len(y_test)) # print("shape of y", y_test.shape) #print(y_train) #print(X_train['u'].shape) import elm as standard_elm # create a classifier elmk = standard_elm.ELMKernel() nn_structure = [9, 100, 1] x, y = utils.transform_dict_for_nn(X_train, y_train, nn_structure[0]) x = np.transpose(x) y = np.transpose([y]) tr_set = np.concatenate( (y, x), 1) #standard format for elm function - y_train + x_train x_test, y_test = utils.transform_dict_for_nn(X_test[0], y_test[0], nn_structure[0]) #x_test = np.transpose(x_test) #y_test = np.transpose([y_test]) #te_set = np.concatenate((y_test, x_test), 1) # load dataset dataa = standard_elm.read("boston.data") # create a classifier elmk = standard_elm.elmk.ELMKernel() # split data in training and testing sets # use 80% of dataset to training and shuffle data before splitting tr_set, te_set = standard_elm.split_sets(dataa, training_percent=.8, perm=True) #train and test # results are Error objects tr_result = elmk.train(tr_set) te_result = elmk.test(te_set) print(te_result.get_accuracy()) te_result.predicted_targets
3), onehot_encode( test_diff, 3) # Concatenate subject and difficulty train_data = np.concatenate((train_data, train_sub, train_diff), axis=1) test_data = np.concatenate((test_data, test_sub, test_diff), axis=1) # Classification if args.clf_model == 'LDA': clf_model = LDA() elif args.clf_model == 'SVM': clf_model = SVC(C=1) elif args.clf_model == 'ELMK': clf_model = elm.ELMKernel() elif args.clf_model == 'ELMR': clf_model = elm.ELMRandom() if args.clf_model in ['ELMK', 'ELMR']: train_elm_data = np.concatenate( (train_label[:, np.newaxis], train_data), axis=1) test_elm_data = np.concatenate( (test_label[:, np.newaxis], test_data), axis=1) clf_model.search_param(train_elm_data, cv="kfold", of="accuracy", eval=10) train_acc = clf_model.train(train_elm_data).get_accuracy() test_acc = clf_model.test(test_elm_data).get_accuracy() elif args.clf_model in ['pcafc', 'pcafc_sd']:
def main(index_exp=0): dirName = '%s_%s_data%d_%s' % (args.ext_model, args.rgr_model, args.data_cate, args.append_name) fileName = '%s_exp%d' % (dirName, index_exp) # Create folder for results of this model if not os.path.exists('./results/%s' % (dirName)): os.makedirs('./results/%s' % (dirName)) print('Extraction model: %s' % (args.ext_model)) print('Regression model: %s' % (args.rgr_model)) if args.ext_model == 'vgg16': net = tv_models.vgg16(pretrained=True).to(device=device) set_parameter_requires_grad(net, True) net.classifier[6] = Identity() elif args.ext_model == 'resnet50': net = tv_models.resnet50(pretrained=True).to(device=device) set_parameter_requires_grad(net, True) net.fc = Identity() # Get dataset batchSize = 64 input_size = 224 # Load Data data_transforms = { 'train': transforms.Compose([ndl.Rescale(input_size), ndl.ToTensor()]), 'test': transforms.Compose([ndl.Rescale(input_size), ndl.ToTensor()]) } print("Initializing Datasets and Dataloaders...") # Create training and testing datasets image_datasets = { x: ndl.TopoplotLoader(args.image_folder, x, transform=data_transforms[x], index_exp=index_exp) for x in ['train', 'test'] } # Create training and testing dataloaders dataloaders_dict = { 'train': Data.DataLoader(image_datasets['train'], batch_size=batchSize, shuffle=False, num_workers=4), 'test': Data.DataLoader(image_datasets['test'], batch_size=batchSize, shuffle=False, num_workers=4) } # Extract features by VGG16 net.eval() # Disable batchnorm, dropout X_train, Y_train = extract_layer(dataloaders_dict['train'], net) X_test, Y_test = extract_layer(dataloaders_dict['test'], net) # Standardize data before PCA if args.scale: X_train, X_test = preprocessing.scale(X_train, X_test, mode=args.scale) # Apply PCA to reduce dimension if args.n_components > 1: args.n_components = int(args.n_components) pca = PCA(n_components=args.n_components, svd_solver='full') pca.fit(X_train) X_train = pca.transform(X_train) X_test = pca.transform(X_test) print('(X) Number of features after PCA: %d' % (X_train.shape[1])) print('(X) Explained variance ratio: %.3f' % (np.sum(pca.explained_variance_ratio_))) # Add conditional entropy if args.add_CE and args.data_cate == 2: print('Add conditional entropy as additional features...') with open( './raw_data/CE_sub%d_channel21_exp%d_train.data' % (args.subject_ID, index_exp), 'rb') as fp: CE_train = pickle.load(fp) with open( './raw_data/CE_sub%d_channel21_exp%d_test.data' % (args.subject_ID, index_exp), 'rb') as fp: CE_test = pickle.load(fp) # Scale CE CE_train, CE_test = preprocessing.scale(CE_train, CE_test) # Apply PCA pca = PCA(n_components=30, svd_solver='full') pca.fit(CE_train) CE_train = pca.transform(CE_train) CE_test = pca.transform(CE_test) print('(CE) Number of features after PCA: %d' % (CE_train.shape[1])) print('(CE) Explained variance ratio: %.3f' % (np.sum(pca.explained_variance_ratio_))) # Concatentate with X X_train = np.concatenate((X_train, CE_train), axis=1) X_test = np.concatenate((X_test, CE_test), axis=1) # Regression to predict solution latency X_train_Reg = X_train X_test_Reg = X_test if args.rgr_model == 'LR': rgr = linear_model.LinearRegression() elif args.rgr_model == 'Ridge': rgr = linear_model.Ridge(alpha=1) elif args.rgr_model == 'GPR': kernel = RBF(10, (1e-2, 1e2)) + ConstantKernel(10, (1e-2, 1e2)) rgr = GaussianProcessRegressor(kernel=kernel, random_state=0) elif args.rgr_model == 'ELMK': rgr = elm.ELMKernel() elif args.rgr_model == 'ELMR': params = ["sigmoid", 1, 500, False] rgr = elm.ELMRandom(params) if args.rgr_model not in ['ELMK', 'ELMR']: rgr.fit(X_train_Reg, Y_train) pred_train = rgr.predict(X_train_Reg) pred_test = rgr.predict(X_test_Reg) else: # Scale target into -1~1 if args.scale_target == 2: scaler = TargetScaler(num_step=10) scaler.fit(Y_train) Y_train, Y_test = scaler.transform(Y_train), scaler.transform( Y_test) elif args.scale_target == 1: Y_train, Y_test = (Y_train - 30) / 30, (Y_test - 30) / 30 # Concatenate data for extreme learning machine train_data = np.concatenate((Y_train[:, np.newaxis], X_train), axis=1) test_data = np.concatenate((Y_test[:, np.newaxis], X_test), axis=1) rgr.search_param(train_data, cv="kfold", of="rmse", eval=10) pred_train = rgr.train(train_data).predicted_targets pred_test = rgr.test(test_data).predicted_targets # Scale target back to 0~60 if args.scale_target == 2: [Y_train, Y_test, pred_train, pred_test] = [scaler.transform(x, mode='inverse') for x in \ [Y_train, Y_test, pred_train, pred_test]] elif args.scale_target == 1: [Y_train, Y_test, pred_train, pred_test] = [x*30+30 for x in \ [Y_train, Y_test, pred_train, pred_test]] evaluate_result.plot_scatter(Y_test, pred_test, dirName, fileName) print('Train std: %.3f' % (mean_squared_error(Y_train, pred_train)**0.5)) print('Test std: %.3f' % (mean_squared_error(Y_test, pred_test)**0.5)) # Save targets and predictions dict_target = {} dict_target['target'], dict_target['pred'] = Y_test, pred_test with open('./results/%s/%s.data' % (dirName, fileName), 'wb') as fp: pickle.dump(dict_target, fp) return
from sklearn import svm features = features.reshape(16000,38) clf1 = svm.SVC() clf1.fit(features, y_train) featurestest = featurestest.reshape(4000, 38) accuracy = clf1.score(featurestest,y_test) accuracy *= 100 print(accuracy) import h5py filename = 'resnet_svm_model_color.sav' pickle.dump(clf1, open(filename, 'wb')) !pip3 install elm !pip3 install --upgrade numpy folium imgaug import elm elmk=elm.ELMKernel() elmk.search_param(data, cv="kfold", of="accuracy", eval=10) tr_result = elmk.train(features,y_train) te_result = elmk.test(featurestest,y_test) print(te_result.get_accuracy)
def buildELM(data, iterNum, isNormal, isRegression, isPCA, n_components,normalMethod): rfList = [] elmr = elm.ELMKernel() # elmr = elm.ELMRandom() elmr.search_param(data, eval=100, cv='kfold') # print("This is X_norm: ", X_norm) for j in range(iterNum): # X_train, X_test, y_train, y_test = train_test_split(X_norm, y, test_size=0.2) # param_test1 = {'n_hidden': range(10, 21, 10)} # model = GridSearchCV(estimator=elm.ELMRegressor(), # param_grid=param_test1, cv=5, n_jobs=1) # model = elm.ELMRegressor(n_hidden=tempDim,activation_func='sine') # model = elm.ELMRegressor(activation_func='tanh') # model = elm.GenELMRegressor() # temp = model.fit(X_train, y_train) # print("The best parameters are %s with a score of %0.2f" # % (model.best_params_, model.best_score_)) # Predict on new data # y_pred = temp.predict(X_test).tolist() # y_test_list = y_test.tolist() # create a classifier # elmr = elm.ELMRandom() # search for best parameter for this dataset # define "kfold" cross-validation method, "accuracy" as a objective function # to be optimized and perform 10 searching steps. # best parameters will be saved inside 'elmk' object # split data in training and testing sets # use 80% of dataset to training and shuffle data before splitting tr_set, te_set = elm.split_sets(data, training_percent=.9, perm=True) # print("This is tr_set: ", tr_set) X_train = tr_set[:, 1:].copy() # print("This is X_train", X_train) X_test = te_set[:, 1:].copy() # print("This is X_test: ", X_test) if normalMethod == 1: print("Normalizer() is using..." ) sc = preprocessing.Normalizer() elif normalMethod == 2: print("StandardScalar() is using...") sc = preprocessing.StandardScaler() elif normalMethod == 3: print("MinMaxScalar() is using...") sc = preprocessing.MinMaxScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) if isPCA: X_train, X_test = reduceDim.featureExtraction(X_train_std, X_test_std, n_components) else: X_train = X_train_std X_test = X_test_std tr_set = np.c_[tr_set[:, 0], X_train] te_set = np.c_[te_set[:, 0], X_test] # print("This is new tr_set: ", tr_set) # print("This is new te_set: ", te_set) # train and test # results are Error objects tr_result = elmr.train(tr_set) te_result = elmr.test(te_set) y_test_list = te_result.expected_targets y_pred = te_result.predicted_targets # print("This is tr_result: ", tr_result.expected_targets) # print("This is tr_result pre: ",tr_result.predicted_targets ) # print("This is te_result: ", te_result.expected_targets) # expected is the real value # print("This is te_result predict: ", te_result.predicted_targets) # print(te_result.get_accuracy) if isRegression: return y_pred else: sum_mean = 0 for i in range(len(y_pred)): # print( # "This is REAL value %.4f, ======ELM=====> PRED value: %.4f" % (y_test_list[i], y_pred[i])) # sum_mean += (y_pred[i] - y_test[i]) ** 2 # if the target is np array sum_mean += (float("{0:.4f}".format(float(y_pred[i]))) - y_test_list[i]) ** 2 # else: # print("This is REAL value %.4f, ======Random Forest=====> PRED value: %.4f" % ( # y_test.values[i], y_pred[i])) # # sum_mean += (y_pred[i] - y_test.values[i]) ** 2 # sum_mean += (float("{0:.4f}".format(float(y_pred[i]))) - y_test.values[i]) ** 2 sum_erro = np.sqrt(sum_mean / len(y_pred)) rfList.append(sum_erro) print("This is RMSE for ELM: ", sum_erro) print("This is iteration num: ", j + 1) return rfList