def __update__(self): '''Updates self.CONFS to new ACTUAL or PREDS values''' for model in self.PREDS: assert len(self.ACTUAL)==len(self.PREDS[model]),"Predicted and actual data must be of equal length" if len(set(self.PREDS[model]))>2: self.CONFS.update( {model: ConfusionMatrix.ConfusionMatrix(actual=self.ACTUAL,pred=self.PREDS[model],bins=1000).getConfs()} ) else: self.CONFS.update( {model: ConfusionMatrix.ConfusionMatrix(actual=self.ACTUAL,pred=self.PREDS[model])} )
# dataset.print_label() optimalParameter = True #determine if the optimal value for gamma and cost are used (True) #or if personnalized values are used (False) #-------PARAMETERS TO MODIFY (works only if optimalParameter = False------- cost = 10 #optimal value: 10 gamma = 0.5 #optimal value: 0.5 #--- MAIN print("Problem D") if SVM: print("Number of input data: " + str(NBR_POINT)) X_train, X_test, y_train, y_test = train_test_split(dataset.input, dataset.label, train_size=0.5, random_state=2) if optimalParameter: svc = svm.run_svm_nonlinear(X_train, y_train, kernel="rbf") else: svc = svm.run_svm_nonlinear(X_train, y_train, kernel="rbf", cost=cost, gamma=gamma) svm.plot_svc(svc, X_test, y_test) cm = ConfusionMatrix.ConfusionMatrix(svc, X_test, y_test) print("Support Vector Machine: ") cm.print_matrix()
import matplotlib as mpl from sklearn.svm import SVC from sklearn.model_selection import GridSearchCV NBR_EPOCH_MAX = 2000 PERCEPTRON = True SVM = True dataset = data.data1 #--- MAIN print("Problem A") if PERCEPTRON: print("Perceptron learning rule: ") (W, b) = per.train_nn_perceptron(dataset.inputON, dataset.inputOFF, NBR_EPOCH_MAX, "hardlims", 1) W = np.append(W, b) print("W = ") print(W) per.plot_perceptron(dataset, W) if SVM: # tuned_parameters = [{'C': [0.001, 0.01, 0.1, 1, 5, 10, 100]}] # clf = GridSearchCV(SVC(kernel='linear'), tuned_parameters, cv=5, scoring='accuracy') # clf.fit(dataset.input, dataset.label) # print("Best param: ") # print(clf.best_params_) svc = svm.run_svm(dataset, cost=1, kernel="linear") #cost value doesn't have any impact here svm.plot_svc(svc, dataset.input,dataset.label) cm = ConfusionMatrix.ConfusionMatrix(svc, dataset) cm.print_matrix()
NBR_EPOCH_MAX = 2000 PERCEPTRON = True SVM = True dataset = data.data1 optimalParameter = True #determine if the optimal value for cost is used (True) #or if personnalized value is used (False) #-------PARAMETERS TO MODIFY (works only if optimalParameter = False------- cost = 1 #optimal value: 100 #--- MAIN print("Problem A") if PERCEPTRON: print("Perceptron learning rule: ") (W, b) = per.train_nn_perceptron(dataset.inputON, dataset.inputOFF, NBR_EPOCH_MAX, "hardlims", 1) W = np.append(W, b) print("W = ") print(W) per.plot_perceptron(dataset, W) if SVM: if (optimalParameter): svc = svm.run_svm_linear(dataset.input, dataset.label , kernel="linear") else: svc = svm.run_svm_linear(dataset.input, dataset.label , kernel="linear", cost=1) svm.plot_svc(svc,dataset.input,dataset.label) cm = ConfusionMatrix.ConfusionMatrix(svc, dataset.input,dataset.label) print("Support Vector Machine: ") cm.print_matrix()
for forest_idx in range(NUM_CLASSES): choices = decision_forest_vote_weighted(forest[forest_idx], weights[:, forest_idx], x) vote_block[:, forest_idx] = choices prediction = np.zeros((x.shape[0], 1)) for vote_idx in range(vote_block.shape[0]): prediction[vote_idx] = int(np.argmax(vote_block[vote_idx])) + 1 else: print "Testing Tree by Max Depth" prediction = TestTreesByDepth(trees, x, ambiguityHandlingStyle) print "Test Done" print "" cm = ConfusionMatrix(prediction, y, NUM_CLASSES) error = sampleError(prediction, y) measures = MeasurePerFold(cm, NUM_CLASSES) print "Confusion Matrix" PrintMatrix(cm) print "" PrintMatrix(measures) print "Recall = %.3f, Precision = %.3f, F1 = %.3f, CR = %.3f" % ( np.sum(measures[0, :] / 6.0), np.sum(measures[1, :] / 6.0), np.sum(measures[2, :] / 6.0), np.sum(measures[3, :] / 6.0)) print "" print "Error Rate: %.3f" % error print ""
y_test_predictions = np.zeros((x_test.shape[0], 1)) for vote_idx in range(vote_block.shape[0]): y_test_predictions[vote_idx] = int( np.argmax(vote_block[vote_idx])) + 1 depth_correct = 0 for prediction_idx in range(0, y_test_predictions.shape[0]): if y_test_predictions[prediction_idx] == y_test[ prediction_idx]: depth_correct += 1 depth_correct /= float(len(y_test_predictions)) stacked_sample_error_depth += 1 - depth_correct stacked_confusion_matrices_depth += ConfusionMatrix( y_test_predictions, y_test, NUM_CLASSES) confusion_matrices_depth.append( ConfusionMatrix(y_test_predictions, y_test, NUM_CLASSES)) print 'Fold accuracy:', depth_correct else: forestList = [] y_predictions = np.zeros((y_test.shape[0], 1), dtype=np.int32) for class_idx in range(NUM_CLASSES): class_num = class_idx + 1 forestList.append([]) for tree_idx in range(NUM_TREES_IN_FOREST): x_sample, y_sample = sample(x_train, y_train, SAMPLE_PROPORTION) train_targets = PreProcess(y_sample, class_num)
import pandas as pd import KnnClassifier as knn import ConfusionMatrix as Cm import RandomSubsampling as Rs data = pd.read_csv("Iris.csv") data = data.drop('Id', axis=1) classifier = knn.KnnClassifier() cm = Cm.ConfusionMatrix(len(data['Species'].unique())) Random_Sub = Rs.Random_Subsampling() X = data.drop('Species', axis=1) y = data['Species'] error, accuracy = Random_Sub.Random_Subsampling(classifier, cm, X, y) cm.show_conf_matrix() print() print("Accuracy: ", accuracy) print("Error: ", error)
# print depth_cr stacked_sample_error_depth += 1 - (depth_cr) stacked_sample_error_depth_min += 1 - (depth_min_cr) stacked_sample_error_occ += 1 - (occ_correct) if depth_cr > best_depth_cr: best_depth_cr = depth_cr best_trees_depth = DeepCopyTreeList(treeList) if depth_min_cr > best_depth_min_cr: best_depth_min_cr = depth_min_cr best_trees_depth_min = DeepCopyTreeList(treeList) if occ_correct > best_occ_correct: best_occ_correct = occ_correct best_trees_occ = DeepCopyTreeList(treeList) confusion_matrices_depth.append( ConfusionMatrix(predictions_by_depth, y_test, NUM_CLASSES)) confusion_matrices_depth_min.append( ConfusionMatrix(predictions_by_min_depth, y_test, NUM_CLASSES)) confusion_matrices_occ.append( ConfusionMatrix(predictions_by_occ, y_test, NUM_CLASSES)) stacked_confusion_matrices_depth += ConfusionMatrix( predictions_by_depth, y_test, NUM_CLASSES) stacked_confusion_matrices_depth_min += ConfusionMatrix( predictions_by_min_depth, y_test, NUM_CLASSES) stacked_confusion_matrices_occ += ConfusionMatrix(predictions_by_occ, y_test, NUM_CLASSES) depth_parameters = [ np.zeros(NUM_CLASSES, dtype=np.float64), np.zeros(NUM_CLASSES, dtype=np.float64),