def main(): m = 350 random.seed(2) X = np.empty([m, 2]) X[:, 0] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) X[:, 1] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) #not separable y = np.empty([m, 1]) for i in range(X.shape[0]): y[i] = func2(X[i, :]) #plot data and decision surface ax = pu.plot_data(X, y) pu.plot_surface(X, y, X[:, 0], X[:, 1], disc_func=func, ax=ax) plt.show() #train svm #change c to hard/soft margins w, w0, support_vectors_idx = svm.train(X, y, c=99999, eps=0.1) #plot result predicted_labels = svm.classify_all(X, w, w0) print("Accuracy: {}".format(svm.getAccuracy(y, predicted_labels))) ax = pu.plot_data(X, y, support_vectors_idx) pu.plot_surfaceSVM(X[:, 0], X[:, 1], w, w0, ax=ax) plt.show()
def main(): m=350 random.seed(2) X = np.empty([m,2]) X[:,0] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) X[:,1] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) #not separable y = np.empty([m,1]) for i in range(X.shape[0]): y[i] = func2(X[i,:]) #plot data and decision surface ax = pu.plot_data(X,y) pu.plot_surface(X,y, X[:, 0], X[:,1], disc_func=func, ax=ax) plt.show() #train svm #change c to hard/soft margins w,w0, support_vectors_idx = svm.train(X,y,c=99999,eps=0.1) #plot result predicted_labels = svm.classify_all(X,w,w0) print("Accuracy: {}".format(svm.getAccuracy(y,predicted_labels))) ax = pu.plot_data(X,y, support_vectors_idx) pu.plot_surfaceSVM(X[:,0], X[:,1], w,w0, ax=ax) plt.show()
def main(plot_type): per_size = 5 nrow, ncol = len(graphs), len(params) fig = plt.figure(figsize=(ncol * per_size, nrow * per_size)) if plot_type.startswith('dist'): angle = (10, 45) else: angle = (15, 210) for i, gname in enumerate(graphs): for j, param in enumerate(params): idx = i * ncol + j + 1 ax = fig.add_subplot(nrow, ncol, idx, projection='3d') plot_surface(gname, param, plot_type, fig, ax=ax, dirname=dirname, angle=angle, use_colorbar=False) ax.set_title('{}({})'.format(gname, param)) plt.locator_params(axis='y', nbins=5) plt.locator_params(axis='x', nbins=5) fig_dir = 'figs/{}'.format(fig_dirname) if not os.path.exists(fig_dir): os.makedirs(fig_dir) figpath = '{}/{}.pdf'.format(fig_dir, plot_type) print(figpath) fig.savefig(figpath)
def main(): m=100 X = np.empty([m,2]) X[:,0] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) X[:,1] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) # preprocessing.scale(X) #linearly separable y = np.empty([m,1]) for i in range(m): y[i] = func(X[i,]) #plot data and decision surface ax = pu.plot_data(X,y) pu.plot_surface(X,y, X[:, 0], X[:,1], disc_func=func, ax=ax) plt.show() #train svm w,w0, support_vectors_idx = svm.train(X,y,c=999999999999999, eps=10, type='gaussian') # w, w0, support_vectors_idx = svm.train(X, y, c=999999999999999, eps=10, type='polynomial') #plot result predicted_labels = svm.classify_all(X,w,w0) print("Accuracy: {}".format(svm.getAccuracy(y,predicted_labels))) ax = pu.plot_data(X,y, support_vectors_idx) pu.plot_surfaceSVM(X[:,0], X[:,1], w,w0, ax=ax) plt.show()
def main(): x, y = readData( "C:/Users/marro/Repo/CS584/Generative_Learning/Data/banknote/data_banknote_authentication.txt", ",", scale=False) #shuffle p = np.random.permutation(len(x)) x = x[p] y = y[p] x = x[:, (0, 1)] # encode class labels classes, y = np.unique(y, return_inverse=True) print("Confussion matrix: {}".format(getCM(y, classifyAll(x, x, y)))) print("Training accuracy: {}".format( getAccuracy(y, classifyAll(x, x, y), 1))) print("Kfold Accuracy, recall, precission,tp,tn,fp,fn: {}".format( kfoldCrossValidation(x, y, 10, 1))) precision = dict() recall = dict() average_precision = dict() for i in range(0, 1): precision[i], recall[i], _ = precision_recall_curve( y, classifyAll(x, x, y)) average_precision[i] = average_precision_score(y, classifyAll(x, x, y)) # Plot Precision-Recall curve plt.clf() plt.plot(recall[0], precision[0], label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-Recall example: AUC={0:0.2f}'.format( average_precision[0])) plt.legend(loc="lower left") plt.show() # plot data and decision surface ax = plt.gca() cm_bright = ListedColormap(['#FF0000', '#0000FF']) ax.scatter(x[:, 0], x[:, 1], c=(y == 1), cmap=cm_bright) plt.xlabel("X1") plt.ylabel("X2") pu.plot_surface(x, y, x[:, 0], x[:, 1], ax=ax) plt.show()
def main(): x, y = readData("C:/Users/marro/Repo/CS584/Generative_Learning/Data/banknote/data_banknote_authentication.txt",",",scale=False) #shuffle p = np.random.permutation(len(x)) x = x[p] y = y[p] x = x[:,(0,1)] # encode class labels classes, y = np.unique(y, return_inverse=True) print("Confussion matrix: {}".format(getCM(y,classifyAll(x,x,y)))) print("Training accuracy: {}".format(getAccuracy(y,classifyAll(x,x,y),1))) print("Kfold Accuracy, recall, precission,tp,tn,fp,fn: {}".format(kfoldCrossValidation(x,y, 10, 1))) precision = dict() recall = dict() average_precision = dict() for i in range(0,1): precision[i], recall[i], _ = precision_recall_curve(y, classifyAll(x,x,y)) average_precision[i] = average_precision_score(y, classifyAll(x,x,y)) # Plot Precision-Recall curve plt.clf() plt.plot(recall[0], precision[0], label='Precision-Recall curve') plt.xlabel('Recall') plt.ylabel('Precision') plt.ylim([0.0, 1.05]) plt.xlim([0.0, 1.0]) plt.title('Precision-Recall example: AUC={0:0.2f}'.format(average_precision[0])) plt.legend(loc="lower left") plt.show() # plot data and decision surface ax = plt.gca() cm_bright = ListedColormap(['#FF0000', '#0000FF']) ax.scatter(x[:,0], x[:,1], c=(y == 1), cmap=cm_bright) plt.xlabel("X1") plt.ylabel("X2") pu.plot_surface(x,y, x[:, 0], x[:, 1], ax=ax) plt.show()
def main(): m=150 random.seed(2) X = np.empty([m,2]) X[:,0] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) X[:,1] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) preprocessing.scale(X) #linearly separable y = np.empty([m,1]) for i in range(m): y[i] = func(X[i,]) # shuffle p = np.random.permutation(len(X)) X = X[p] y = y[p] #plot data and decision surface ax = pu.plot_data(X,y) pu.plot_surface(X,y, X[:, 0], X[:,1], disc_func=func, ax=ax) plt.show() #train svm w,w0, support_vectors_idx = svm.train(X,y,c=9999, eps=0.000001) #plot result predicted_labels = svm.classify_all(X,w,w0) print("Accuracy: {}".format(svm.getAccuracy(y,predicted_labels))) kfold = svm.kfoldCrossValidation(X,y,10,1,c=999999999,eps=0.000001) print (kfold) ax = pu.plot_data(X,y, support_vectors_idx) pu.plot_surfaceSVM(X[:,0], X[:,1], w,w0, ax=ax) plt.show()
def main(): m = 150 random.seed(2) X = np.empty([m, 2]) X[:, 0] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) X[:, 1] = np.matrix((random.sample(range(-10000, 10000), m))) / float(1000) preprocessing.scale(X) #linearly separable y = np.empty([m, 1]) for i in range(m): y[i] = func(X[i, ]) # shuffle p = np.random.permutation(len(X)) X = X[p] y = y[p] #plot data and decision surface ax = pu.plot_data(X, y) pu.plot_surface(X, y, X[:, 0], X[:, 1], disc_func=func, ax=ax) plt.show() #train svm w, w0, support_vectors_idx = svm.train(X, y, c=9999, eps=0.000001) #plot result predicted_labels = svm.classify_all(X, w, w0) print("Accuracy: {}".format(svm.getAccuracy(y, predicted_labels))) kfold = svm.kfoldCrossValidation(X, y, 10, 1, c=999999999, eps=0.000001) print(kfold) ax = pu.plot_data(X, y, support_vectors_idx) pu.plot_surfaceSVM(X[:, 0], X[:, 1], w, w0, ax=ax) plt.show()
def main(plot_type): nrow, ncol = len(dirnames), len(graphs) per_size = 5 fig = plt.figure(figsize=(per_size * ncol, per_size * nrow)) if plot_type.startswith('dist'): angle = (10, 45) elif plot_type.startswith('rank'): angle = (10, 45) else: angle = (15, 210) for i, (method, dirname) in enumerate(zip(methods, dirnames)): for j, gname in enumerate(graphs): idx = i * ncol + j + 1 ax = fig.add_subplot(nrow, ncol, idx, projection='3d') plot_surface(gname, param, plot_type, fig, ax=ax, dirname=dirname, angle=angle, use_colorbar=False) plt.locator_params(axis='y', nbins=5) plt.locator_params(axis='x', nbins=5) if i == 0: ax.set_title(gname) if j == 0: ax.set_zlabel(method, size='large') fig.tight_layout() fig_dir = 'figs/{}'.format(fig_dirname) if not os.path.exists(fig_dir): os.makedirs(fig_dir) figpath = '{}/{}.pdf'.format(fig_dir, plot_type) print(figpath) fig.savefig(figpath)