scoring='accuracy') # print(lam, scores) acc = np.mean(scores) if not best_results or best_results['score'] < acc: best_results = {'lam': lam, 'score': acc} # EasyMKL-BASED ############################################################################################# clf = EasyMKL(learner=base_learner, lam=best_results['lam']).fit(k1 + k2 + k3 + k4 + k5 + k6, y_tr_A) print(clf) ############################################################################################# # evaluate the solution from sklearn.metrics import accuracy_score, roc_auc_score y_pred = clf.predict(k8) # predictions y_score = clf.decision_function(k8) # rank accuracy = accuracy_score(y_te_A, y_pred) roc_auc = roc_auc_score(y_te_A, y_score) print('Accuracy score: %.3f, roc_AUC score: %.3f' % (accuracy, roc_auc)) print('accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam'])) ############################################################################################### # # Calling confusion matrix and plotting classification report from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score, roc_curve, auc, roc_auc_score print(classification_report(y_te_A, y_pred)) cm = cnf_matrix = confusion_matrix(y_te_A, y_pred) print(cm) total = sum(sum(cnf_matrix)) ACC = (cnf_matrix[0, 0] + cnf_matrix[1, 1]) / total
#MKL algorithms from MKLpy.algorithms import EasyMKL, KOMD #KOMD is not a MKL algorithm but a simple kernel machine like the SVM from MKLpy.model_selection import cross_val_score, cross_val_predict from sklearn.svm import SVC import numpy as np print('tuning lambda for EasyMKL...', end='') base_learner = SVC(C=10000) #simil hard-margin svm best_results = {} for lam in [0, 0.01, 0.1, 0.2, 0.9, 1]: #possible lambda values for the EasyMKL algorithm #MKLpy.model_selection.cross_val_predict performs the cross validation automatically, it optimizes the accuracy #the counterpart cross_val_score optimized the roc_auc_score (use score='roc_auc') #WARNING: these functions will change in the next version scores = cross_val_predict(KLtr, Ytr, EasyMKL(estimator=base_learner, lam=lam), n_folds=5, score='accuracy') acc = np.mean(scores) if not best_results or best_results['score'] < acc: best_results = {'lam': lam, 'score': acc} #evaluation on the test set from sklearn.metrics import accuracy_score print('done') clf = EasyMKL(estimator=base_learner, lam=best_results['lam']).fit(KLtr, Ytr) y_pred = clf.predict(KLte) accuracy = accuracy_score(Yte, y_pred) print('accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam']))
def parallelised_function(file): select_file_path = os.path.join(jointFeatureLocation, file) # formulate the path print('Symbol:----->', file.split("_")[0]) symbol = file.split("_")[0] select_hmm_date = select_file_path.split("_")[ 3] # pull out the hmm_date - strip it out select_feature_label_date = select_file_path.split("_")[ 6] # pull out the label_feature_date select_label_idx = select_file_path.split("_")[ 9] # pull out the label _idx unpickled_select_file = open_pickle_filepath( select_file_path) # unplickle the select file hmm_keys = sorted(list( unpickled_select_file.keys())) # hmm keys for the select file. for hmm_date_key in hmm_keys: # pick and hmm date feature_label_keys = sorted( unpickled_select_file[hmm_date_key].keys( )) # each key here unlocks a feature and label set for feature_label_date in feature_label_keys: # make a list of all the feature dates features_file_path = unpickled_select_file[hmm_date_key][ feature_label_date][0] # this is the feature path labels_file_path = unpickled_select_file[hmm_date_key][ feature_label_date][1] # this is the labels path if os.path.isfile(features_file_path ): # if label file exists I can traing print( 'ok----->', feature_label_date ) # if you got to this point we have data so we can mov eon labels = pd.read_csv(labels_file_path) # open labels file label_name = str( labels.columns[labels.columns.str.contains( pat='label')].values[0]) features = open_pickle_filepath( features_file_path) # opens features file hmm_features = nfu.hmm_features_df( features ) # get the hmm features out, so unpack the tuples! print('loaded features and labels ') if hmm_features.isnull().values.all( ): # checking that the HMM features are actually not null continue else: # if features not null then start moving on! market_features_df = CreateMarketFeatures( CreateMarketFeatures( CreateMarketFeatures(df=CreateMarketFeatures( df=labels).ma_spread_duration()).ma_spread( )).chaikin_mf()).obv_calc( ) # market features dataframe df_concat = pd.DataFrame( pd.concat([hmm_features, market_features_df], axis=1, sort='False').dropna()) df = df_concat[df_concat[label_name].notna()] df_final = df.drop(columns=[ 'TradedPrice', 'Duration', 'TradedTime', 'ReturnTradedPrice', 'Volume', label_name ]) y_train = df[df.columns[df.columns.str.contains( pat='label')]].iloc[:, 0] # training labels if df_final.shape[ 0] < 10: # make sure it all looks reasonable print( ' the ratio of classes is too low. try another label permutation' ) continue else: print("starting model fit") # put the features in a tensor format X = np.asarray( df_final.values) # need this for torch Xtr = normalization(rescale_01(torch.Tensor( X))) # features in a tensor format Ytr = torch.Tensor( y_train.values ) # put the labels in a tensor format print( '-----------------first bit done------------------' ) KLrbf = generators.RBF_generator( Xtr, gamma=[.01, .1, .25, .5] ) # get a few RBF Kernels ready - maybe need more here print('done with kernel') best_results = {} C_range = [0.1, 1] lam_range = [0.2] try: for C_choice in C_range: base_learner = SVC( C=C_choice) # "hard"-margin svm # clf = EasyMKL(lam=0.2, multiclass_strategy='ova', learner=base_learner).fit(KLrbf, # Ytr) # print('done') # print('the combination weights are:') # # for sol in clf.solution: # print('(%d vs all): ' % sol, # clf.solution[ # sol].weights) # need to store these results somewhere for lam in lam_range: # possible lambda values for the EasyMKL algorithm # MKLpy.model_selection.cross_val_score performs the cross validation automatically, it may returns # accuracy, auc, or F1 scores scores = cross_val_score( KLrbf, Ytr, EasyMKL(learner=base_learner, lam=lam), n_folds=5, scoring='accuracy' ) # get the cross-validation scores acc = np.mean(scores) if not best_results or best_results[ 'score'] < acc: best_results = { 'C': C_choice, 'lam': lam, 'score': acc, 'scores': scores } # these should get dumped somewhere print('done') best_learner = SVC(C=best_results['C']) clf = EasyMKL(learner=best_learner, lam=best_results['lam']).fit( KLrbf, Ytr) y_pred = clf.predict(KLrbf) accuracy = accuracy_score(Ytr, y_pred) print( 'accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam'])) print(scores) pickle_out_filename = os.path.join( mainPath, "ExperimentCommonLocs/CrossValidationResults", "_".join((symbol, 'feature_label_date', str(select_feature_label_date), str(select_label_idx), 'hmm_date:', hmm_date_key, 'RBF', 'MultiKernelSVC.pkl'))) # pickle_out = open(pickle_out_filename, 'wb') # pickle.dump(best_results, pickle_out) # pickle_out.close() except ValueError: continue else: print('PROBLEM----->in one of of your locations') continue
print(i, "hin failed here!") continue else: print('Shapes dont match.') pass print('Average Kernel Testing') try: y_pred_te = clf.predict(KLte) # predictions average kernel y_pred_tr = clf.predict(KLtr) y_score_te = clf.decision_function(KLte) y_score_tr = clf.decision_function(KLtr) fpr_avg, tpr_avg, thresholds_avg = roc_curve( yte.ravel(), y_score_te.ravel()) y_predMKL_te = clfEasy.predict( KLte) # predictions mkl test y_predMKL_tr = clfEasy.predict( KLtr) # predictions mkl train average_kernel_results['fpr'].append(fpr_avg) average_kernel_results['tpr'].append(tpr_avg) average_kernel_results['train_date'].append(testDate) average_kernel_results['data_date'].append(dataDate) average_kernel_results['Average precision-recall score'].append(\ average_precision_score(y_pred_tr, y_score_tr)) average_kernel_results['thresholds'].append(thresholds_avg) average_kernel_results['test_recall'].append( recall_score(yte, y_pred_te, average='weighted')) average_kernel_results['train_recall'].append( recall_score(ytr, y_pred_tr, average='weighted')) fpr_avg = None tpr_avg = None
#MKL algorithms from MKLpy.algorithms import AverageMKL, EasyMKL print('training EasyMKL with one-vs-all multiclass strategy...', end='') from sklearn.svm import SVC base_learner = SVC(C=0.1) clf = EasyMKL(lam=0.1, multiclass_strategy='ova', learner=base_learner).fit(KLtr, Ytr) from MKLpy.multiclass import OneVsRestMKLClassifier, OneVsOneMKLClassifier print('done') print('the combination weights are:') for sol in clf.solution: print('(%d vs all): ' % sol, clf.solution[sol].weights) #evaluate the solution from sklearn.metrics import accuracy_score, roc_auc_score import numpy as np y_pred = clf.predict(KLte) #predictions y_score = clf.decision_function(KLte) #rank accuracy = accuracy_score(Yte, y_pred) print('Accuracy score: %.3f' % (accuracy)) print('training EasyMKL with one-vs-one multiclass strategy...', end='') clf = EasyMKL(lam=0.1, multiclass_strategy='ovo', learner=base_learner).fit(KLtr, Ytr) print('done') print('the combination weights are:') for sol in clf.solution: print('(%d vs %d): ' % (sol[0], sol[1]), clf.solution[sol].weights)
KL = [kernel_normalization(pairwise.monotone_conjunctive_kernel(Xbin, c=c)) for c in range(5)] print ('done') #train/test KL split (N.B. here we split a kernel list directly) from MKLpy.model_selection import train_test_split KLtr,KLte,Ytr,Yte = train_test_split(KL, Y, test_size=.3, random_state=42) #MKL algorithms from MKLpy.algorithms import EasyMKL, KOMD #KOMD is not a MKL algorithm but a simple kernel machine like the SVM from MKLpy.model_selection import cross_val_score, cross_val_predict from sklearn.svm import SVC import numpy as np print ('tuning lambda for EasyMKL...', end='') base_learner = SVC(C=10000) #simil hard-margin svm best_results = {} for lam in [0, 0.01, 0.1, 0.2, 0.9, 1]: #possible lambda values for the EasyMKL algorithm #MKLpy.model_selection.cross_val_predict performs the cross validation automatically, it optimizes the accuracy #the counterpart cross_val_score optimized the roc_auc_score (use score='roc_auc') #WARNING: these functions will change in the next version scores = cross_val_predict(KLtr, Ytr, EasyMKL(estimator=base_learner, lam=lam), n_folds=5, score='accuracy') acc = np.mean(scores) if not best_results or best_results['score'] < acc: best_results = {'lam' : lam, 'score' : acc} #evaluation on the test set from sklearn.metrics import accuracy_score print ('done') clf = EasyMKL(estimator=base_learner, lam=best_results['lam']).fit(KLtr,Ytr) y_pred = clf.predict(KLte) accuracy = accuracy_score(Yte, y_pred) print ('accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam']))
def MultiView_learning(): """MultiView learning""" print('loading dataset...', end='') training_data = io.loadmat( r"D:\CVProject\CBAM-keras-master\handcraft\features_with_pca_file_0202.mat" ) length = len(training_data['array'][0]) X, Y = training_data['array'][:, 0:length - 2], training_data['array'][:, -1] print('done') # preprocess data print('preprocessing data...', end='') from MKLpy.preprocessing import normalization, rescale_01 X = rescale_01(X) # feature scaling in [0,1] X = normalization(X) # ||X_i||_2^2 = 1 # train/test split from sklearn.model_selection import train_test_split Xtr, Xte, Ytr, Yte = train_test_split(X, Y, test_size=.1, random_state=42, shuffle=True) print(numpy.array(Xtr).shape) print(numpy.array(Ytr).shape) print('done') print('Training on {0} samples, Testing on {1} samples'.format( len(Xtr), len(Xte))) print('computing RBF Kernels...', end='') from MKLpy.metrics import pairwise from MKLpy.generators import Multiview_generator X1_tr = numpy.array(Xtr[:, :2]) # time X2_tr = numpy.array(Xtr[:, 2:92]) # color X3_tr = numpy.array(Xtr[:, 92:124]) # Gabor X4_tr = numpy.array(Xtr[:, 124:156]) # lbp X5_tr = numpy.array(Xtr[:, 156:348]) # cloud X6_tr = numpy.array(Xtr[:, 348:432]) # haze X7_tr = numpy.array(Xtr[:, 432:603]) # contrast X8_tr = numpy.array(Xtr[:, 603:606]) # shadow X9_tr = numpy.array(Xtr[:, 606:608]) # snow X10_tr = numpy.array(Xtr[:, 608:]) # pca X1_te = numpy.array(Xte[:, :2]) # time X2_te = numpy.array(Xte[:, 2:92]) # color X3_te = numpy.array(Xte[:, 92:124]) # Gabor X4_te = numpy.array(Xte[:, 124:156]) # lbp X5_te = numpy.array(Xte[:, 156:348]) # cloud X6_te = numpy.array(Xte[:, 348:432]) # haze X7_te = numpy.array(Xte[:, 432:603]) # contrast X8_te = numpy.array(Xte[:, 603:606]) # shadow X9_te = numpy.array(Xte[:, 606:608]) # snow X10_te = numpy.array(Xte[:, 608:]) # pca KLtr = Multiview_generator([ X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr ], kernel=pairwise.rbf_kernel) KLte = Multiview_generator([ X1_te, X2_te, X3_te, X4_te, X5_te, X6_te, X7_te, X8_te, X9_te, X10_te ], [X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr], kernel=pairwise.rbf_kernel) print('done') from MKLpy.algorithms import AverageMKL, EasyMKL print('training EasyMKL with one-vs-all multiclass strategy...', end='') from sklearn.svm import SVC base_learner = SVC(C=8) clf = EasyMKL(lam=0.1, multiclass_strategy='ova', learner=base_learner).fit(KLtr, Ytr) print('the combination weights are:') for sol in clf.solution: print('(%d vs all): ' % sol, clf.solution[sol].weights) from sklearn.metrics import accuracy_score, roc_auc_score, recall_score, confusion_matrix y_pred = clf.predict(KLte) # predictions y_score = clf.decision_function(KLte) # rank accuracy = accuracy_score(Yte, y_pred) print('Accuracy score: %.4f' % (accuracy)) recall = recall_score(Yte, y_pred, average='macro') print('Recall score: %.4f' % (recall)) cm = confusion_matrix(Yte, y_pred) print('Confusion matrix', cm) print('training EasyMKL with one-vs-one multiclass strategy...', end='') clf = EasyMKL(lam=0.1, multiclass_strategy='ovo', learner=base_learner).fit(KLtr, Ytr) print('done') print('the combination weights are:') for sol in clf.solution: print('(%d vs %d): ' % (sol[0], sol[1]), clf.solution[sol].weights) y_pred = clf.predict(KLte) # predictions y_score = clf.decision_function(KLte) # rank accuracy = accuracy_score(Yte, y_pred) print('Accuracy score: %.4f' % (accuracy)) recall = recall_score(Yte, y_pred, average='macro') print('Recall score: %.4f' % (recall)) cm = confusion_matrix(Yte, y_pred) print('Confusion matrix', cm)
def Learning_curve_using_weather_data(): ''' Cross validation using weather data: PASS: 2021.02.05 ''' # load data print('loading dataset...', end='') # from sklearn.datasets import load_breast_cancer as load # ds = load() # X, Y = ds.data, ds.target # # Files training_data = io.loadmat( r"D:\CVProject\CBAM-keras-master\handcraft\features_with_pca.mat") # training_data = io.loadmat(r"D:\CVProject\CBAM-keras-master\handcraft\features_with_pca_file.mat") # training_data = io.loadmat(r"D:\CVProject\CBAM-keras-master\handcraft\features_with_pca_file_0202.mat") results_data = open( r"D:\CVProject\CBAM-keras-master\handcraft\results\learning_curve_results_0202_01.txt", "w") # length = len(training_data['array'][0]) length = len(training_data['array'][0]) # X, Y = training_data['array'][:, 0:length - 1], training_data['array'][:, -1] X, Y = training_data['array'][:, 0:length - 1], training_data['array'][:, -1] print('done') # preprocess data print('preprocessing data...', end='') from MKLpy.preprocessing import normalization, rescale_01 X = rescale_01(X) # feature scaling in [0,1] X = normalization(X) # ||X_i||_2^2 = 1 print('done') from MKLpy.algorithms import EasyMKL, KOMD # KOMD is not a WeatherClsMKL algorithm but a simple kernel machine like the SVM from MKLpy.model_selection import cross_val_score from sklearn.svm import SVC import numpy as np # base_learner = SVC(C=10000) # "hard"-margin svm print("Build a base learner") base_learner = SVC(C=20) # "hard"-margin svm # # # === parameters selection === # best_results = {} # # for lam in [0, 0.01, 0.1, 0.2, 0.9, 1]: # possible lambda values for the EasyMKL algorithm # for lam in [0]: # possible lambda values for the EasyMKL algorithm # # MKLpy.model_selection.cross_val_score performs the cross validation automatically, it may returns # # accuracy, auc, or F1 scores # # evaluation on the test set # print("Model training with lam {}".format(lam)) # clf = EasyMKL(lam=0.1, multiclass_strategy='ova', learner=base_learner).fit(KLtr, Ytr) # scores = cross_val_score(KLtr, Ytr, clf, n_folds=5, scoring='accuracy') # acc = np.mean(scores) # if not best_results or best_results['score'] < acc: # best_results = {'lam': lam, 'score': acc} print("Build EasyMKL classifier") # clf = EasyMKL(lam=0.1, multiclass_strategy='ova', learner=base_learner).fit(KLtr, Ytr) # scores = cross_val_score(KLtr, Ytr, clf, n_folds=5, scoring='accuracy') # acc = np.mean(scores) # print("acc:", acc) # ====== Learning curve ======= # # X1_tr = numpy.array(Xtr[:, :2]) # time # X2_tr = numpy.array(Xtr[:, 2:92]) # color # X3_tr = numpy.array(Xtr[:, 92:124]) # Gabor # X4_tr = numpy.array(Xtr[:, 124:156]) # lbp # X5_tr = numpy.array(Xtr[:, 156:348]) # cloud # X6_tr = numpy.array(Xtr[:, 348:432]) # haze # X7_tr = numpy.array(Xtr[:, 432:603]) # contrast # X8_tr = numpy.array(Xtr[:, 603:651]) # shadow # X9_tr = numpy.array(Xtr[:, 606:683]) # snow # X10_tr = numpy.array(Xtr[:, 683:]) # pca # # X1_te = numpy.array(Xte[:, :2]) # time # X2_te = numpy.array(Xte[:, 2:92]) # color # X3_te = numpy.array(Xte[:, 92:124]) # Gabor # X4_te = numpy.array(Xte[:, 124:156]) # lbp # X5_te = numpy.array(Xte[:, 156:348]) # cloud # X6_te = numpy.array(Xte[:, 348:432]) # haze # X7_te = numpy.array(Xte[:, 432:603]) # contrast # X8_te = numpy.array(Xte[:, 603:651]) # shadow # X9_te = numpy.array(Xte[:, 606:683]) # snow # X10_te = numpy.array(Xte[:, 683:]) # pca # # # # # # all features # KLtr = Multiview_generator([X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr], kernel=pairwise.rbf_kernel) # KLte = Multiview_generator([X1_te, X2_te, X3_te, X4_te, X5_te, X6_te, X7_te, X8_te, X9_te, X10_te], [X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr], kernel=pairwise.rbf_kernel) # # KYtr = Ytr[:] # KYte = Yte[:] # for elem in [0.02, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]: for elem in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]: # for elem in [1]: learn_count = int(elem * X.shape[0]) KLtr, KYtr, KLte, KYte = bulid_kernel_transform( X[:learn_count], Y[:learn_count]) train_count, test_count = len(KYtr), len(KYte) clf = EasyMKL(lam=0.1, multiclass_strategy='ova', learner=base_learner).fit(KLtr, KYtr) # scores = cross_val_score(KLtr, Ytr, clf, n_folds=5, scoring='accuracy') # acc = np.mean(scores) y_train_pred = clf.predict(KLtr) y_test_pred = clf.predict(KLte) train_set_accuracy = accuracy_score(KYtr, y_train_pred) tests_et_accuracy = accuracy_score(KYte, y_test_pred) # display the results print("Test on {0} train samples and {1} test samples,".format( train_count, test_count), end="") print( 'accuracy on the train set: %.3f and accuracy on the test set : %.3f' % (train_set_accuracy, tests_et_accuracy)) # save the results in txt print("Test on {0} train samples and {1} test samples,".format( train_count, test_count), end="", file=results_data) print( 'accuracy on the train set: %.3f and accuracy on the test set : %.3f' % (train_set_accuracy, tests_et_accuracy), file=results_data) # from sklearn.metrics import accuracy_score print('done') # ============================== pass # # # ===== evaluate the model ===== # # # Chose the model with high performance # # # Transform # X1_tr = numpy.array(Xtr[:, :2]) # time # X2_tr = numpy.array(Xtr[:, 2:92]) # color # X3_tr = numpy.array(Xtr[:, 92:124]) # Gabor # X4_tr = numpy.array(Xtr[:, 124:156]) # lbp # X5_tr = numpy.array(Xtr[:, 156:348]) # cloud # X6_tr = numpy.array(Xtr[:, 348:432]) # haze # X7_tr = numpy.array(Xtr[:, 432:603]) # contrast # X8_tr = numpy.array(Xtr[:, 603:606]) # shadow # X9_tr = numpy.array(Xtr[:, 606:608]) # snow # X10_tr = numpy.array(Xtr[:, 608:]) # pca # # X1_te = numpy.array(Xte[:, :2]) # time # X2_te = numpy.array(Xte[:, 2:92]) # color # X3_te = numpy.array(Xte[:, 92:124]) # Gabor # X4_te = numpy.array(Xte[:, 124:156]) # lbp # X5_te = numpy.array(Xte[:, 156:348]) # cloud # X6_te = numpy.array(Xte[:, 348:432]) # haze # X7_te = numpy.array(Xte[:, 432:603]) # contrast # X8_te = numpy.array(Xte[:, 603:606]) # shadow # X9_te = numpy.array(Xte[:, 606:608]) # snow # X10_te = numpy.array(Xte[:, 608:]) # pca # # # # all features # KLtr = Multiview_generator([X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr], kernel=pairwise.homogeneous_polynomial_kernel) # KLte = Multiview_generator([X1_te, X2_te, X3_te, X4_te, X5_te, X6_te, X7_te, X8_te, X9_te, X10_te], [X1_tr, X2_tr, X3_tr, X4_tr, X5_tr, X6_tr, X7_tr, X8_tr, X9_tr, X10_tr], kernel=pairwise.homogeneous_polynomial_kernel) # # KYtr = Ytr[:] # KYte = Yte[:] # # clf = EasyMKL(learner=base_learner, lam=0.1).fit(KLtr, KYtr) # y_train_pred = clf.predict(KLtr) # y_test_pred = clf.predict(KLte) # # train_set_accuracy = accuracy_score(KYtr, y_train_pred) # tests_et_accuracy = accuracy_score(KYte, y_test_pred) # # # print('accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam'])) # print('accuracy on the train set: %.3f, and accuracy on the test set : %.3f' % (train_set_accuracy, tests_et_accuracy)) # # ====================== pass
print(i, "hin failed here!") continue else: print('Shapes dont match.') pass print('Average Kernel Testing') try: y_pred_te = clf.predict(KLte) # predictions average kernel y_pred_tr = clf.predict(KLtr) y_score_te = clf.decision_function(KLte) y_score_tr = clf.decision_function(KLtr) fpr_avg, tpr_avg, thresholds_avg = roc_curve( yte.ravel(), y_score_te.ravel()) y_predMKL_te = clfEasy.predict(KLte) # predictions mkl test y_predMKL_tr = clfEasy.predict(KLtr) # predictions mkl train average_kernel_results['fpr'].append(fpr_avg) average_kernel_results['tpr'].append(tpr_avg) average_kernel_results['train_date'].append(testDate) average_kernel_results['data_date'].append(dataDate) average_kernel_results[ 'Average precision-recall score'].append( average_precision_score(y_pred_tr, y_score_tr)) average_kernel_results['thresholds'].append(thresholds_avg) average_kernel_results['test_recall'].append( recall_score(yte, y_pred_te, average='weighted')) average_kernel_results['train_recall'].append( recall_score(ytr, y_pred_tr, average='weighted')) fpr_avg = None tpr_avg = None
n_folds=5, scoring='accuracy') # print(lam, scores) acc = np.mean(scores) if not best_results or best_results['score'] < acc: best_results = {'lam': lam, 'score': acc} # EasyMKL-BASED ############################################################################################# clf = EasyMKL(learner=base_learner, lam=best_results['lam']).fit(k1, y_train_A) print(clf) ############################################################################################# # evaluate the solution from sklearn.metrics import accuracy_score, roc_auc_score y_pred = clf.predict(k11) # predictions y_score = clf.decision_function(k11) # rank accuracy = accuracy_score(y_test_A, y_pred) roc_auc = roc_auc_score(y_test_A, y_score) print('Accuracy score: %.3f, roc_AUC score: %.3f' % (accuracy, roc_auc)) print('accuracy on the test set: %.3f, with lambda=%.2f' % (accuracy, best_results['lam'])) ############################################################################################### # # Calling confusion matrix and plotting classification report from sklearn.metrics import classification_report, confusion_matrix, cohen_kappa_score, roc_curve, auc, roc_auc_score print(classification_report(y_test_A, y_pred)) cm = cnf_matrix = confusion_matrix(y_test_A, y_pred) print(cm) total = sum(sum(cnf_matrix)) ACC = (cnf_matrix[0, 0] + cnf_matrix[1, 1]) / total