from pyxvis.io.data import load_features from pyxvis.learning.classifiers import clf_model from pyxvis.learning.evaluation import cross_validation # List of classifiers ss_cl = [ 'dmin', 'lda', 'qda', 'maha', 'knn3', 'knn5', 'knn7', 'knn11', 'knn15', 'bayes-naive', 'bayes-kde', 'adaboost', 'lr', 'rf', 'tree', 'svm-lin', 'svm-rbf(0.1,1)', 'svm-rbf(0.1,0.5)', 'svm-rbf(0.5,1)', 'svm-pol(0.05,0.1,2)', 'svm-pol(0.05,0.5,2)', 'svm-pol(0.05,0.5,3)', 'svm-sig(0.1,1)', 'svm-sig(0.1,0.5)', 'svm-sig(0.5,1)', 'nn(10,)', 'nn(20,)', 'nn(12,6)', 'nn(20,10,4)' ] (X, d) = load_features('../data/G3/G3', full=1) # load training and testing data n = len(ss_cl) folds = 10 acc = np.zeros((n, )) for k in range(n): (name, params) = clf_model(ss_cl[k]) # function name and parameters acc[k] = cross_validation([name, params], X, d, folds=folds) print(f'{k:3d}' + ') ' + f'{ss_cl[k]:20s}' + ': ' + f'CV-Accuracy = {acc[k]:.4f}') ks = np.argmax(acc) print('-----------------------------------------------') print('Best Classifier:') print(f'{ks:3d}' + ') ' + f'{ss_cl[ks]:20s}' + ': ' + f'CV-Accuracy = {acc[ks]:.4f}')
from sklearn.model_selection import train_test_split from pyxvis.io.data import load_features from pyxvis.io.plots import show_confusion_matrix from pyxvis.learning.classifiers import clf_model from pyxvis.learning.evaluation import hold_out # load available dataset (X0, d0) = load_features('../data/F2/F2', full=1) # definition of training and testing data X, Xt, d, dt = train_test_split(X0, d0, test_size=0.2, stratify=d0) # definition of the classifier cl_name = 'svm-rbf(0.1,1)' # generic name of the classifier (name, params) = clf_model(cl_name) # function name and parameters # Hold-out (train on (X,d), test on (Xt), compare with dt) ds, acc, _ = hold_out([name, params], X, d, Xt, dt) # hold out print(cl_name + ': ' + f'Accuracy = {acc:.4f}') # display confusion matrix show_confusion_matrix(dt, ds, 'Testing subset')
from pyxvis.io.data import load_features from pyxvis.io.plots import show_confusion_matrix from pyxvis.learning.classifiers import clf_model, define_classifier from pyxvis.learning.classifiers import train_classifier, test_classifier (X, d, Xt, dt) = load_features('../data/F2/F2') # load training and testing data # Classifier definition ss_cl = ['dmin', 'svm-rbf(0.1,1)'] n = len(ss_cl) for k in range(n): (name, params) = clf_model(ss_cl[k]) # function name and parameters clf = define_classifier([name, params]) # classifier definition clf = train_classifier(clf, X, d) # classifier training ds = test_classifier(clf, Xt) # classification of testing show_confusion_matrix(dt, ds, ss_cl[k]) # display confusion matrix
import numpy as np from pyxvis.learning.classifiers import nn_definition from pyxvis.learning.classifiers import nn_forward_propagation, nn_backward_propagation from pyxvis.learning.classifiers import nn_parameters_update, nn_loss_function from pyxvis.io.plots import plot_features_y,show_confusion_matrix, plot_loss from pyxvis.io.data import load_features # Load training and testing data (Xtrain,Ytrain,Xtest,Ytest) = load_features('../data/G4/G4',categorical=1) plot_features_y(Xtrain,Ytrain,'Training Subset') # Definitions N = Xtrain.shape[1] # training samples n_0 = Xtrain.shape[0] # number of inputs (X) n_m = Ytrain.shape[0] # number of outputs (Y) tmax = 1000 # max number of iterations alpha = 10 # learning rate loss_eps = 0.01 # stop if loss<loss_eps nh = [6,12] # nodes of hidden layers n = [n_0]+nh+[n_m] # nodes of each layer m = len(n)-1 ltrain = np.zeros([tmax,1]) # training loss # Training t = -1 train = 1 W,b = nn_definition(n,N) # (step 1) while train: t = t+1 a = nn_forward_propagation(Xtrain,W,b) # (step 2) dW,db = nn_backward_propagation(Ytrain,a,W,b) # (step 3)
from sklearn.neural_network import MLPClassifier from pyxvis.io.plots import plot_features2,show_confusion_matrix, plot_loss from pyxvis.io.data import load_features # Load training and testing data (Xtrain,Ytrain,Xtest,Ytest) = load_features('../data/G4/G4') plot_features2(Xtrain,Ytrain,'Training+Testing Subsets') # Definitions alpha = 1e-5 # learning rate nh = (6,12) # nodes of hidden layers tmax = 2000 # max number of iterations solver = 'adam' # optimization approach ('lbfgs','sgd', 'adam') # Training net = MLPClassifier(solver=solver, alpha=alpha,hidden_layer_sizes=nh, random_state=1,max_iter=tmax) print(Xtrain.shape) print(Ytrain.shape) net.fit(Xtrain, Ytrain) # Evaluation Ym = net.predict(Xtrain) show_confusion_matrix(Ym,Ytrain,'Training') Ys = net.predict(Xtest) show_confusion_matrix(Ys,Ytest,'Testing')
from pyxvis.learning.evaluation import best_features_classifier dataname = 'fbdata' # prefix of npy files of training and testing data fxnew = 1 # the features are (0) loaded or (1) extracted and saved if fxnew: # features to extract fx = ['basicint', 'gabor-ri', 'lbp-ri', 'haralick-2', 'fourier', 'hog'] # feature extraction in training images path = '../images/fishbones/' X, d = extract_features_labels(fx, path + 'train', 'jpg') # feature extraction in testing images Xt, dt = extract_features_labels(fx, path + 'test', 'jpg') # backup of extracted features save_features(X, d, Xt, dt, dataname) else: X, d, Xt, dt = load_features(dataname) X, sclean, a, b = clean_norm(X) Xt = clean_norm_transform(Xt, sclean, a, b) # Classifiers to evaluate ss_cl = [ 'maha', 'bayes-kde', 'svm-lin', 'svm-rbf', 'qda', 'lda', 'knn3', 'knn7', 'nn' ] # Number of features to select ff = [3, 5, 10, 12, 15] # Feature selectors to evaluate ss_fs = ['fisher', 'qda', 'svm-lin', 'svm-rbf']
from pyxvis.io.plots import show_clf_results from pyxvis.learning.classifiers import clf_model, define_classifier from pyxvis.learning.classifiers import train_classifier, test_classifier # List of classifiers ss_cl = [ 'dmin', 'lda', 'qda', 'maha', 'knn3', 'knn5', 'knn7', 'knn11', 'knn15', 'bayes-naive', 'bayes-kde', 'adaboost', 'lr', 'rf', 'tree', 'svm-lin', 'svm-rbf(0.1,1)', 'svm-rbf(0.1,0.5)', 'svm-rbf(0.5,1)', 'svm-pol(0.05,0.1,2)', 'svm-pol(0.05,0.5,2)', 'svm-pol(0.05,0.5,3)', 'svm-sig(0.1,1)', 'svm-sig(0.1,0.5)', 'svm-sig(0.5,1)', 'nn(10,)', 'nn(20,)', 'nn(12,6)', 'nn(20,10,4)' ] (X, d, Xt, dt) = load_features('../data/G3/G3') # load training and testing data n = len(ss_cl) acc_train = np.zeros((n, )) acc_test = np.zeros((n, )) for k in range(n): (name, params) = clf_model(ss_cl[k]) # function name and parameters clf = define_classifier([name, params]) # classifier definition clf = train_classifier(clf, X, d) # classifier training d0 = test_classifier(clf, X) # classification of training ds = test_classifier(clf, Xt) # classification of testing acc_train[k] = accuracy_score(d, d0) # accuracy in training acc_test[k] = accuracy_score(dt, ds) # accuracy in testing print(f'{k:3d}' + ') ' + f'{ss_cl[k]:20s}' + ': ' + f'Acc-Train = {acc_train[k]:.4f}' + ' ' + f'Acc-Test = {acc_test[k]:.4f}')
Xt : matrix of testing features, one sample per row dt : vector of testing labels s : string with the name of the model p : number of features to be selected [OUTPUT] q : indices of selected features (columns) X : new matrix of training features Xt : new matrix of testing features """ from sklearn.neighbors import KNeighborsClassifier as KNN from pyxvis.features.selection import fse_model, fsel from pyxvis.io.data import load_features from pyxvis.io.plots import print_confusion # Definition of input variables (X, d, Xt, dt) = load_features('../data/F40/F40') s = 'lda' p = 5 # Feature selection (name, params) = fse_model(s) q = fsel([name, params], X, d, p, cv=5, show=1) print(str(len(q)) + ' from ' + str(X.shape[1]) + ' features selected.') # New training and testing data X = X[:, q] Xt = Xt[:, q] # Classification and Evaluation clf = KNN(n_neighbors=5) clf.fit(X, d)
""" [INPUT] X : training features (matrix of N x p elements) d : vector of training labels (vector of N elements) Xt : testing features (matrix of Nt x p elements) dt : vector of training labels (vector of Nt elements) s : string with the name of the model [OUTPUT] ds : classification (vector of Nt elements) clf: trained classifier """ from pyxvis.io.data import load_features from pyxvis.io.plots import print_confusion from pyxvis.learning.classifiers import clf_model, define_classifier from pyxvis.learning.classifiers import train_classifier, test_classifier # Definition of input variables (X, d, Xt, dt) = load_features('../data/G3/G3') s = 'knn5' # Training and Testing (name, params) = clf_model(s) # function name and parameters clf = define_classifier([name, params]) # classifier definition clf = train_classifier(clf, X, d) # classifier training ds = test_classifier(clf, Xt) # classification on testing # Evaluation of performance print_confusion(dt, ds)
from sklearn.metrics import precision_recall_curve, average_precision_score from sklearn.metrics import roc_curve, roc_auc_score from pyxvis.io.data import load_features from pyxvis.io.plots import plot_features, plot_ROC, plot_precision_recall from pyxvis.learning.classifiers import clf_model, define_classifier, train_classifier (X, d, Xt, dt) = load_features('../data/F2/F2') # load train/test data plot_features(X, d, 'F2 dataset') # plot of feature space ss_cl = ['nn(3,)', 'nn(4,)', 'nn(8,)'] # classifiers to evaluate curve = 1 # 0 = ROC curve, 1 = precision/recall curve n = len(ss_cl) # number of models for k in range(n): cl_name = ss_cl[k] name, params = clf_model(cl_name) # function name and parameters clf = define_classifier([name, params]) # classifier definition clf = train_classifier(clf, X, d) # classifier training p = clf.predict_proba(Xt)[:, 1] # classification probabilities if curve == 0: # ROC curve auc = roc_auc_score(dt, p) # area under curve fpr, tpr, _ = roc_curve(dt, p) # false and true positive rates plot_ROC(fpr, tpr, cl_name, auc, (k, n)) # ROC curve else: # precision/recall curve ap = average_precision_score(dt, p) # area under curve pr, re, _ = precision_recall_curve(dt, p) # precision and recall values plot_precision_recall(pr, re, cl_name, ap,