def create(jsonFilePath, dataset): try: with open(jsonFilePath) as json_file: try: jsonData = json.load(json_file) validate(instance=jsonData, schema=estimatorSchema) except jsonschema.exceptions.ValidationError as err: template = "An exception of type {0} occurred. Arguments: {1!r}" message = template.format(type(err).__name__, err.args) print(message) raise ValueError(error.errors['estimator_config']) except ValueError as err: template = "An exception of type {0} occurred. Arguments: {1!r}" message = template.format(type(err).__name__, err.args) print(message) raise ValueError(error.errors['estimator_config']) if jsonData['estimator'].startswith('KNeighbors'): import Knn #as Knn esti = Knn.Knn(jsonData) elif jsonData['estimator'].startswith('DecisionTree'): import DecisionTree esti = DecisionTree.DecisionTree(jsonData) elif jsonData['estimator'] == 'LinearSVC' or jsonData[ 'estimator'] == 'LinearSVR': import SVM esti = SVM.SVM(jsonData) elif jsonData['estimator'].startswith('ANN'): import ANN esti = ANN.ANN(jsonData) elif jsonData['estimator'] == 'TripleES': import TripleES esti = TripleES.TripleES(jsonData) else: est_str = jsonData['estimator'] print(f'Invalid value for estimator name: {est_str}') raise ValueError(error.errors['estimator_config']) #esti.parse(jsonData) # right??? esti.assign_dataset(dataset) return esti except FileNotFoundError as err: template = "An exception of type {0} occurred. Arguments: {1!r}" message = template.format(type(err).__name__, err.args) print(message) raise ValueError(error.errors['estimator_config'])
def callTestSelectionMethods(self): naiveTestResults=[] sVMTestResults=[] testSetName=[] if(len(self.testSet.curselection())!=0): testSet=self.selectTest() for curSet in testSet: testPath="" #get selected test sets if(curSet=="IMDB Movie set(25000)"): testPath="Datasets/aclImdb/test" if(curSet=="IMDB Movie subset(650)"): testPath="Datasets/debugSets/test" if(curSet=="Stanford Movie Set(2000)"): testPath="Datasets/stanfordMR/test" if(curSet=="Custom Set 1"): testPath="Datasets/custom1/test" if(curSet=="Custom Set 2"): testPath="Datasets/custom2/test" if(curSet=="Custom Set 3"): testPath="Datasets/custom3/test" testSetName.append(curSet) #Calculate accuracy of testVars of Naive bayes and SVM svm=SVM.SVM() sVMTestResults.append(svm.testSVM(testPath)) nb=naiveBayes.NaiveBayes() naiveTestResults.append(nb.validate(testPath)) else: print("Please select an algorithm and a test set.") for k in range(1,6): self.f_TResults.grid_slaves(k, 0)[0].delete(0,tk.END) self.f_TResults.grid_slaves(k, 2)[0].delete(0,tk.END) self.f_TResults.grid_slaves(k, 1)[0].delete(0,tk.END) for i in range(1,len(testSetName)+1): self.f_TResults.grid_slaves(i, 0)[0].insert(0,testSetName[i-1]) self.f_TResults.grid_slaves(i, 2)[0].insert(0,sVMTestResults[i-1]) self.f_TResults.grid_slaves(i, 1)[0].insert(0,naiveTestResults[i-1]) print(testSetName) print(sVMTestResults) print(naiveTestResults) self.f_Test.update()
def run(): args = sys.argv[1:] trainxFileName = args[0] trainyFileName = args[1] testxFileName = args[2] # from file to nparray trainx = initDataToDS(trainxFileName) trainy = initDataToDS(trainyFileName) testx = initDataToDS(testxFileName) # change to correct types: trainx = transformCategoralLabels(trainx) testx = transformCategoralLabels(testx) trainx = transformToFloat(trainx) trainy = transformToFloat(trainy) testx = transformToFloat(testx) trainx, trainy = shuffle2arr(trainx, trainy) # trainx, trainy = shuffle(trainx, trainy, random_state=1) # normalize: choose zScore or MinMax ****normlize without the category column******* # trainx, testx = normalizeZscore(trainx, testx) trainx, testx = normalizeMinMax(trainx, testx) # normalize: choose zScore or MinMax # trainx, testx = normalizeZscore(trainx, testx) # trainx, testx = normalizeMinMax(trainx, testx) # train the models: percpetron_w = Percpetron.perceptron(trainx, trainy) svm_w = SVM.SVM(trainx, trainy) pa_w = PA.PA(trainx, trainy) # predict labels: percpetron_predict = predict_y(testx, percpetron_w) svm_predict = predict_y(testx, svm_w) pa_predict = predict_y(testx, pa_w) for i in range(len(testx)): print("perceptron: " + str(percpetron_predict[i][0].astype(np.int)) + ", svm: " + str(svm_predict[i][0].astype(np.int)) + ", pa: " + str(pa_predict[i][0].astype(np.int)))
def multiClassPredict(self, HOG): votes = defaultdict(int) data = np.load('storedSVM/model.npz') w = data['w'].item() b = data['b'].item() labels = list(w.keys()) svm = SVM.SVM() for i in range(0, len(labels)): svm.w = w[labels[i]] svm.b = b[labels[i]] if svm.predict(HOG) < 0: votes[labels[i][0]] = votes[labels[i][0]] + 1 else: votes[labels[i][1]] = votes[labels[i][1]] + 1 return max(votes, key=votes.get)
def multiClassFit(self): features = defaultdict(list) w = {} b = {} clf = SVM.SVM() for root, dirs, files in os.walk('./Train Data'): for sample in files: filename = os.path.join(sample) path = 'Train Data/' + filename im = cv2.imread(path) im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) hog = utils.calc_HOG(im) features[filename[0]].append(hog.transpose(1, 0)[0]) labels = list(features.keys()) for i in range(0, len(labels)): for j in range(i + 1, len(labels)): print labels[i] + labels[j] for i in range(0, len(labels)): for j in range(i + 1, len(labels)): neg = np.full((len(features[labels[i]])), -1.0) pos = np.full((len(features[labels[j]])), 1.0) tempLabel = np.concatenate((neg, pos), axis=0) tempFeatures = np.concatenate((np.array( features[labels[i]]), np.array(features[labels[j]])), axis=0) clf.fit(tempFeatures, tempLabel) self.w[labels[i] + labels[j]] = clf.w self.b[labels[i] + labels[j]] = clf.b np.savez('storedSVM\\model', w=self.w, b=self.b)
def test(self): tobeTrained = [] for i in self.datatest: tobeTrained.append(self.x[i]) #for i in self.generes: # tobeTrained.append(self.Data[i]) tobeTrained = np.array(tobeTrained) tobeTrained = tobeTrained.T print(tobeTrained[:, 0]) y = [] for i in self.Data["rate"]: if i == "High": y.append(1) elif i == "Low": y.append(-1) else: y.append(0) #del y[0] X_train, X_test, y_train, y_test = train_test_split(tobeTrained, y, test_size=0.20) print("length of X_train ", np.shape(X_train)) X_trainWithoutPCA = X_train X_train, X_test = self.dimensionaltyReduction(5, X_train, X_test) obj1 = LR.Logistic_Regression(X_train, y_train, X_test, y_test) obj1.FitModel() obj1.TrainAndTestModel() obj2 = svm.SVM(X_train, y_train, X_test, y_test) obj2.FitModel() obj2.TrainAndTestModel() testaya = [0, 1.929883, 0, 90, 16] self.fillMissingTestData(X_trainWithoutPCA, testaya)
def main_from_matrix(name, file_to_save, params): f = open(name, "r") M = read_matrix(f, 4, 1, "***") f.close() f = open(name, "r") Y = read_row(f, "***", 1, "") f.close() k = "" kern = kernel_matrix.kernel_matrix(k, np.sign, params["radial"]) #clf = svm.SVC(kernel = "precomputed",C=params["c"]) clf = SVM.SVM(params["c"], params["cutoff"], np.sign) res = cross_validation_matrix(clf, kern, M, 10, params, Y) txt = statistics_to_txt(res, 0, "roc_x roc_y score") params_learing_str = "_results_using_radial:" + str( params["radial"]) + "_normalized:" + str( params["normalized"]) + "_centered:" + str( params["centered"]) + "_c:" + str( params["c"]) + "_cutoff:" + str(params["cutoff"]) g = open(file_to_save + "/" + name.split("/").pop() + params_learing_str, "w") g.write(txt)
def continueAddFaceImage(self): firstName = self.lineEdit.text() lastName = self.lineEdit_2.text() if not firstName or not lastName: self.msgLbl.setText("<font color='red'>Please enter a full name</font>") else: lock = True lock1 = True lock2 = True self.msgLbl.setText("<font color='red'>Processing Face</font>") lock = exaction.webcamScreenshot(firstName + ' ' + lastName) while lock: pass if not lock: lock1 = dataGeneration.dataGeneration() while lock1: pass if not lock1: lock2 = SVM.SVM() while lock2: pass if not lock2: self.msgLbl.setText("<font color='red'>Done</font>")
class SVMTester: ndata = 300 nfeat = 2 dg = DataGenerator.DataGenerator(nfeat, ndata, 0.1) data = dg.generate() data.to_csv('2d_test_err.data', index=False, header=False) #data=pd.read_csv('2d_test_err.data', header = None, encoding='utf-8') pData = data.iloc[:ndata, :nfeat].values labels = data.iloc[:ndata, nfeat].values model = SVM.SVM() model.fit(pData, labels) nerror = 0 for i in range(ndata): pl = model.predict(pData[i]) if pl != labels[i]: nerror += 1 print(nerror) ws = model.weights print(ws) plt.scatter(pData[:, 0], pData[:, 1], c=labels) x = np.linspace(np.min(pData[:, 0]), np.max(pData[:, 0]), 1000) plt.plot(x, -x * ws[0] / ws[1]) plt.quiver(ws[0] / np.linalg.norm(ws), ws[1] / np.linalg.norm(ws), color='g', scale=10) plt.axis([ np.min(pData[:, 0]), np.max(pData[:, 0]), np.min(pData[:, 1]), np.max(pData[:, 1]) ]) plt.show()
def choix_classifieurs(self, X_train, y_train, X_test, y_test): print( " \n\t\t--- Recherche des meilleurs classifieurs pour chaque méthode ---\n\n" ) #Choix des classifieurs print(" --- Recherche pour Naive Bayes ---\n") #Naive Bayes nB = nb.NaiveBayes() clfNB = nB.choixNB(X_train, y_train, X_test, y_test) #Arbre de décision print(" --- Recherche pour Arbre de Decision ---\n") tree = dt.DecisionTree() clfTree, _ = tree.recherche_param(X_train, y_train, X_test, y_test) #K plus proches voisins print( "\n --- Pas de recherche de paramètres pour les K plus proches voisins ---\n" ) kNN = knn.KNN() #SVM print(" --- Recherche pour la SVM ---\n") sVM = svm.SVM() clfSVM = sVM.hyperParameter(X_train, y_train) #Perceptron print(" --- Recherche pour le Perceptron ---\n") perceptron = perceptr.Perceptr() clfPerceptr = perceptron.rechercheHypParm(X_train, y_train, X_test, y_test) return (clfNB, clfTree, kNN, clfPerceptr, clfSVM)
result.append(predictor.predict(point)) Z = np.array(result).reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=cm.Paired, levels=[-0.001, 0.001], extend='both', alpha=0.8) plt.scatter(flatten(X[:, 0]), flatten(X[:, 1]), c=flatten(y), cmap=cm.Paired) plt.xlim(x_min, x_max) plt.ylim(y_min, y_max) plt.show() num_samples = 500 num_features = 2 grid_size = 20 samples = np.matrix( np.random.normal(size=num_samples * num_features).reshape( num_samples, num_features)) labels = 2 * (samples.sum(axis=1) > 0) - 1.0 model = SVM.SVM(1., Kernel.Kernel.linear()) model.fit(samples, labels) plot(model, samples, labels, grid_size)
i += 1 return X_decom if __name__ == "__main__": X, y = load_data("heart_scale") X, y = (X.A, y.A.flatten()) if type(X) == np.matrixlib.defmatrix.matrix else (X, y) # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0) kernel_ = kernel.Kernel.gaussian(0.5) # X_train_decom = svm_decom(X_train,y_train,kernel_) # print(X_train_decom[:5, :]) X_decom = svm_decom(X, y, kernel_) X_train, X_test, y_train, y_test = train_test_split(X_decom, y, test_size=0.4, random_state=0) import SVM svm = SVM.SVM( kernel=kernel_, c=0.5, ) svm = svm.training(X_train, y_train) accuracy = svm.calc_accuracy(X_train, y_train) print("The training accuracy is: %.3f%%" % (accuracy * 100)) accuracy = svm.calc_accuracy(X_test, y_test) print("The testing accuracy is: %.3f%%" % (accuracy * 100))
import SVM, table as Table svm = SVM.SVM() tab = Table.FeatureTable() def guess(attr_vecs): """ Guesses which class the sub-images belong to. e.g. Given an image it could return 'tree' :param attr_vec: The long list of numerical representations of the attributes extracted (by olivia module) of a sub-image :return: The name of the class the classifier believes the sub-image belongs to """ prediction_ids, probabilities = svm.predict(attr_vecs) prediction_names = [] for i in range(0, len(prediction_ids)): prediction_names.append(tab.find_name(prediction_ids[i])) return prediction_names, probabilities def learn(attr_vecs, true_class_names): """ Trains the SVM on an array of attribute vectors and their corresponding class names :param attr_vec: The long list of numerical representations of the attributes extracted (by olivia module) of a sub-image :param true_class: The class that the sub-image belongs to
from django.http import HttpResponse import test import rawData import time import json from django.views.decorators.csrf import csrf_exempt from MLFF import * from MLFFData import * from SVM import * from SVMData import * CLICK = 0 SVM = SVM() MLFF = MLFF() rateSVM = -1.0 rateMLFF = -1.0 def home(request): dt = datetime.datetime.now() html = ''' <html><body><h1>From django</h1> <p>Time now: %s. </body></html>''' % (dt, ) return HttpResponse(html) def main(request):
import numpy as np import scipy.io as sio import matplotlib.pyplot as plt from SVM import * mat = sio.loadmat('ex6data1.mat') x = mat['X'] y = mat['y'] y = y.astype(int) y=np.squeeze(y) plt.scatter(x[:,0],x[:,1],c=y,cmap='PuOr') S = SVM(x,y,1) # print S.info() # print S.Xdata # print np.dot(S.Ydata,S.Xdata) S.train() print S.Ydata S.result_print() # print S.Ydata.shape # print S.alphas.shape print S.E # print S.Ydata xx = np.linspace(1., 4., 6) yy = (np.linspace(S.b,S.b,6)-S.w[0]*xx)/S.w[1] plt.plot(xx,yy) plt.show()
import imProc import SVM import tkinter as tk from PIL import Image, ImageTk #np.set_printoptions(threshold=np.nan) ##################TRAINING hogD = hog.Hog() hog_descriptors = hogD.getHOG() #print("Descriptor: ", hog_descriptors) print("Descriptor length: ", hog_descriptors.shape) responses = hogD.getResp() print('Training SVM model ...') model = SVM.SVM() model.train(hog_descriptors, responses) ####################Set up GUI window = tk.Tk() #Makes main window window.wm_title("Hand Gesture Detection") window.config(background="#FFFFFF") #Graphics window imageFrame = tk.Frame(window, width=800, height=480) imageFrame.grid(row=0, column=0, padx=10, pady=2) #Button window buttonFrame = tk.Frame(window, width=300, height=100) buttonFrame.grid(row=300, column=0, padx=10, pady=2)
pq = [0.7, 0.8] n = [2, 4] b = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] nst = [1, 2, 3, 4, 5, 6, 7, 8, 9] lambs = [0.01, 0.05, 0.075, 0.1, 0.125, 0.15, 0.2, 0.3] lambs = [0.075, 0.125] #lambs = [0.25,0.35,0.45,0.7,0.8] morgan = [3] for p in nst: #lambs: try: kern = kernel_random.kernel_random(p) learn_set.set_morgan_to_all_molecules(p) #kern = kernel_nth.kernel_nth(p) #kern = kernel_geometric.kernel_geometric(p) #kern = kernel_subtrees.kernel_subtrees(p,0.1)#2,p) clf = SVM.SVM(params["c"], params["cutoff"], np.sign) kern_perc = kernel_matrix.kernel_matrix(kern, np.sign, params["radial"]) gram = kern_perc.K_computation( learn_set.my_set ) #stat_toolkit.training(clf,kern_perc,learn_set,params) header_test = stat_toolkit.generate_legend_for_results( params, param_kernel, "matrix", "parameter teste: morgan = " + str(p)) grama = gram.tolist() #print grama gram_txt = stat_toolkit.statistics_to_txt(grama, 0, header_test) gram_txt += "***\n" for yi in Y: gram_txt += str(yi) + " " f = open(
# RAW train_path = './preprocessed_training_set/train_df.csv' test_path = './preprocessed_training_set/test_df.csv' KNN_obj = knn.KNN(train_path, test_path) run_KNN_trainer() elif usr_input == 2: # RAW train_path = './preprocessed_training_set/train_df.csv' test_path = './preprocessed_training_set/test_df.csv' RF_obj = rf.RF(train_path, test_path) run_RF_trainer() elif usr_input == 3: # RAW train_path = './preprocessed_training_set/train_df.csv' test_path = './preprocessed_training_set/test_df.csv' SVM_obj = svm.SVM(train_path, test_path) run_SVM_trainer() elif usr_input == 4: # RAW train_path = './preprocessed_training_set/train_df.csv' test_path = './preprocessed_training_set/test_df.csv' XGB_obj = xgb.XGB(train_path, test_path) run_XGB_trainer() elif usr_input == 5: # RAW train_path = './preprocessed_training_set/train_df.csv' test_path = './preprocessed_training_set/test_df.csv' ann_obj = ann.ANN(train_path, test_path) run_ann_trainer() elif usr_input == 6: # #WITH Preprocessing
to_add, predicted_labels, actual_labels = kNN.kNN_weighted(training_labels, training_attributes, test_labels, test_attributes, 1, False, True) # accumulate the percentage of each total += to_add AccPreRecF1(actual_labels, predicted_labels) print("-----") print("the total percentage: " + str(total / 10)) # get the average """ print("-----") total = 0 for x in range(0, 10): test_labels, test_attributes, training_labels, training_attributes = IO.read_in_everything( ) toadd, actual_labels, predicted_labels = SVM.SVM(training_labels, training_attributes, test_labels, test_attributes, "SVM_RBF", 100, 0.01, 0) total += toadd AccPreRecF1(actual_labels, predicted_labels) print("-----") print(str(total / 10)) #####################################################################
import numpy as np import scipy.io as spio from SVM import * mat = spio.loadmat('ex_data_frm_coursera_linear.mat',squeeze_me=True) X=mat['X'] y=mat['y'] sigma = 0.1 gamma = 1/(2*(sigma**2)) clf = SVM(kernel= 'linear',gamma = gamma, C = 1.0) clf.fit(X ,y) clf.visualize_linear_hyperplane(X,y)
from Feature_Extraction import * from Cleaning import * from Classification import * from Naive_Bayes import * from Logistic_Regression import * from Ensemble import * from SVM import * if __name__ == '__main__': train = train_data['Clean Description'].tolist() test = test_data['Clean Description'].tolist() NaiveBayes(train, train_data['INDEX New'].tolist(), test, test_data['INDEX New'].tolist()) LogRegr(train, train_data['INDEX New'].tolist(), test, test_data['INDEX New'].tolist()) SVM(train, train_data['INDEX New'].tolist(), test, test_data['INDEX New'].tolist()) Ensemble(train, train_data['INDEX New'].tolist(), test, test_data['INDEX New'].tolist())
def main(): #data_set = pd.read_csv('creditdata.csv', index_col=0) data_set = pd.read_csv('creditSmall.csv', index_col=0) data_set = fix_header(data_set) data_set.EDUCATION[data_set.EDUCATION == '0'] = '4' data_set.EDUCATION[data_set.EDUCATION == '5'] = '4' data_set.EDUCATION[data_set.EDUCATION == '6'] = '4' data_set.MARRIAGE[data_set.MARRIAGE == '0'] = '3' data_set = data_set.astype(float) print(data_set.DEFAULTER.mean()*100) data_set['BILL_PAY_RATIO1'] = (data_set['BILL_AMT1'] - data_set['PAY_AMT1']) / data_set['LIMIT_BAL'] data_set['BILL_PAY_RATIO2'] = (data_set['BILL_AMT2'] - data_set['PAY_AMT2']) / data_set['LIMIT_BAL'] data_set['BILL_PAY_RATIO3'] = (data_set['BILL_AMT3'] - data_set['PAY_AMT3']) / data_set['LIMIT_BAL'] data_set['BILL_PAY_RATIO4'] = (data_set['BILL_AMT4'] - data_set['PAY_AMT4']) / data_set['LIMIT_BAL'] data_set['BILL_PAY_RATIO5'] = (data_set['BILL_AMT5'] - data_set['PAY_AMT5']) / data_set['LIMIT_BAL'] data_set['BILL_PAY_RATIO6'] = (data_set['BILL_AMT6'] - data_set['PAY_AMT6']) / data_set['LIMIT_BAL'] x = data_set.drop(['DEFAULTER'], axis=1) y = data_set.DEFAULTER # rescale the metrics to the same mean and standard deviation scaler = preprocessing.StandardScaler() x = scaler.fit(x).transform(x) # Further divide the train data into train test split 70% & 30% respectively x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, stratify=y, random_state=2) # creating classifier # classifier = neighbors.KNeighborsClassifier(n_neighbors=3) # classifier.fit(X_train, Y_train) # accuracy = classifier.score(X_test, Y_test) # print("Accuracy :",accuracy) #myaccuracy= KNN(x_train,y_train,y_test,x_test,3) #print(myaccuracy) # Predicition using Naive Bayes # model = fit(X_train, Y_train) # predictions = getPredictions(model, X_test) # accuracies = getAccuracy(Y_test, predictions) # print('Accuracy: ', accuracies) # # gaussNb = GaussianNB() # gaussNb.fit(X_train, Y_train) # print(gaussNb) # y_expect = Y_test # y_pred = gaussNb.predict(X_test) # print(accuracy_score(y_expect, y_pred)) # # classification = classification_report(Y_test, predictions) # print(classification) # label = [0, 1] # cmatrix = confusion_matrix(Y_test, predictions, label) # print(cmatrix) # # classification1 = classification_report(Y_test, y_pred) # print(classification1) # label1 = [0, 1] # cmatrix1 = confusion_matrix(Y_test, y_pred, label1) # print(cmatrix1) # plot_confusion_matrix(cmatrix, label) # plot_confusion_matrix(cmatrix1, label1, title="2") clf = SVM() y_svmTrain = np.where(y_train == 0, -1, 1) clf.fit(x_train, y_svmTrain) y_predict = clf.predict(x_test) correct = np.sum(y_predict == y_test) print("%d out of %d predictions correct" % (correct, len(y_predict))) print() print(y_test) plot_margin(x_train[y_train == 1], x_train[y_train == -1], clf) print(x_test)
from SVM import * from data import * #如果为gauss核的话 ['Gauss',标准差] svm=SVM(data,'Line',1000,0.02,0.001) svm.train() print("*******************************") print(svm.predict([4,0])) print(svm.a) print(svm.w) print(svm.b)
cadPatientsByDM() elif n == 'Prepare': before_res() after_res() cadbyecp() elif n == 'LR': lr = LR(X_train,X_train_res,X_test,y_train,y_train_res,y_test) lr.logreg_imbal() lr.logreg_bal() lr.roc_auc_logreg_imbal() lr.roc_auc_logreg_bal() elif n == 'SVM': svm = SVM(X_train,X_train_res,X_test,y_train,y_train_res,y_test) svm.svm_imbal() svm.roc_auc_svm_imbal() svm.svm_bal() svm.roc_auc_svm_bal() elif n == 'ANN': ann = ANN(X_train,X_train_res,X_test,y_train,y_train_res,y_test) ann.ann_imbal() ann.roc_auc_ann_imbal() ann.ann_bal() ann.roc_auc_ann_bal()
import GlobalDefine from sklearn import preprocessing dp = data_processor.DataProcessor() dp.init_data(train_size=50) # get training data and test data train_set = np.load("train_set.npy") train_set_label = np.load("train_set_label.npy").astype(np.int64) test_set = np.load("test_set.npy") test_set_label = np.load("test_set_label.npy") print(train_set.shape, test_set.shape) SVM0 = SVM.SVM(model_id=0) SVM0.train(train_set, train_set_label) res = SVM0.test(test_set, test_set_label) print('using original data only result is', res) # get generative data G = NN.Generator() G.load_state_dict(torch.load('G_state' + GlobalDefine.run_version + '.pkl')) num_data = dp.TrainNumPerClass * dp.LabelSize noise = torch.rand((num_data, NN.GeneratorNoiseDim)) y_ = torch.zeros((num_data, 1)).type(torch.LongTensor) start = 0 for i in range(dp.LabelSize): y_[start:(start + dp.TrainNumPerClass)] = i
import data_process from tree import * from SVM import * from CNN import * import numpy as np if __name__ == "__main__": acc_sequence, gyr_sequence, label_sequence = data_process.get_sequence() # tree(acc_sequence, gyr_sequence, label_sequence) SVM(acc_sequence, gyr_sequence, label_sequence) # CNN(acc_sequence, gyr_sequence, label_sequence) print("Hello World!")
def generate_svm(self): if self.debug is True: print "DirectionEstimator: generating SVMs - debug mode ..." if self.testing_images_path is None: self.svm_person_back = svm.SVM() self.svm_person_back.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_BACK)) self.svm_person_forward = svm.SVM() self.svm_person_forward.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_FRONT)) else: self.svm_person_back = svm.SVM() self.svm_person_back.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_BACK), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_BACK)) self.svm_person_forward = svm.SVM() self.svm_person_forward.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_FRONT), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_FRONT)) return print "DirectionEstimator: generating SVMs - one for each direction ..." if self.testing_images_path is None: self.svm_person_back = svm.SVM() self.svm_person_back.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_BACK)) self.svm_person_forward = svm.SVM() self.svm_person_forward.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_FRONT)) self.svm_person_left = svm.SVM() self.svm_person_left.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_LEFT)) self.svm_person_right = svm.SVM() self.svm_person_right.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_RIGHT)) else: self.svm_person_back = svm.SVM() self.svm_person_back.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_BACK), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_BACK)) self.svm_person_forward = svm.SVM() self.svm_person_forward.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_FRONT), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_FRONT)) self.svm_person_left = svm.SVM() self.svm_person_left.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_LEFT), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_LEFT)) self.svm_person_right = svm.SVM() self.svm_person_right.set_data(os.path.join(self.training_images_path, value.STR_PEDESTRIAN_RIGHT), os.path.join(self.testing_images_path, value.STR_PEDESTRIAN_RIGHT))
#F1: 2 tp / (2 tp + fp + fn) f1 = f1_score(y_test, y_predict_D, average="macro") print("F1 score: ", f1) print("__Confusion Matrix Of Decision Tree__") print("0:Erzincan, 1:Erzurum, 2:Sivas") print(cf_4) print("_____________________________________") #5-Support Vector Machine print("Support Vector Machine") #SVM classification y_predict_S = SVM(x_train, x_test, y_train) accuracy_S = round(accuracy_score(y_predict_S, y_test) * 100, 2) print("Classification Accuracy: ", accuracy_S, '%') #confusion matrix cf_5 = confusion_matrix(y_test, y_predict_S) cf_5 = pd.DataFrame(cf_5, index=cf_labels, columns=cf_labels) #precision tp / (tp + fp) precision = precision_score(y_test, y_predict_S, average="macro") print("Precision: ", precision) #Recall tp / (tp + fn)
# ------------------------------------- Setosa vs Versicolor ---------------------------------- dataTrain = data[10:50] # split data on train set and test set dataTrain = dataTrain.append(data[50:90], ignore_index=True) label = dataTrain.Species label = label.replace(['setosa', 'versicolor'], ['1', '-1']) # replace setosa, versicolor with '-1', '1' dataTrain.drop(['Species'], axis=1, inplace=True) dataTest = data[0:10] dataTest = dataTest.append(data[90:100], ignore_index=True) target = dataTest.Species target = target.replace(['setosa', 'versicolor'], ['1', '-1']) # replace setosa, versicolor with '-1', '1' dataTest.drop(['Species'], axis=1, inplace=True) model = SVM.SVM() # initialize model model.train(dataTrain, label) # train model predict = model.predict(dataTest) # get predict ROC.ROC(predict, target, '-1') # plot ROC curve # ----------------------------------- Versicolor vs Virginica ----------------------------------- import SVM import ROC import pandas as pd # ------------------------------------- Data preparation ------------------------------------- data = pd.read_csv('iris.csv') # read data from file # ------------------------------------- Setosa vs Versicolor ---------------------------------- dataTrain = data[10:50] # split data on train set and test set dataTrain = dataTrain.append(data[50:90], ignore_index=True)
except (OSError, IOError) as e: #model = logistic_regression(X_train,Y_train,x_test,y_test) model = logistic_regression(X_train,Y_train) load(x_test,y_test) elif ch == 2: try: load(x_test,y_test) except (OSError, IOError) as e: model = knn(X_train, Y_train) load(x_test,y_test) else: try: load(x_test,y_test) except (OSError, IOError) as e: model = SVM(X_train, Y_train) load(x_test,y_test) if choice == 2: data = pd.read_csv("Movies_training.csv") X_train, Y_train, x_test, y_test = Data_Preprocessing(data) ch = int(input("Please choose 1- linear regression , 2- Lasso Regression ")) if ch == 1: try: loadRegressor(x_test, y_test) except (OSError, IOError) as e: