elif l == 1 and h == -1: tp += 1 elif l == -1 and h == 1: fn += 1 else: tn += 1 print '-----------------------------' print '\tConfusion Matrix' print '-----------------------------' print '\t\tPredicted' print '\tActual\tNO\tYES' print '-----------------------------' print '\tNO\t', tn, '\t', fp print '-----------------------------' print '\tYES\t', fn, '\t', tp print '-----------------------------' clf = svm_basic(5000) dataArr, labelArr = loadDataSet('linearly_separable.csv') size = int(len(dataArr) * 0.8) train_data = dataArr[:size] train_label = labelArr[:size] test_data = dataArr[size:] test_label = labelArr[size:] weightArray = clf.fit(train_data, train_label) plot_fit(weightArray, train_data, train_label) hyp = clf.predict(test_data) print('Accuracy: ', accuracy(test_label, hyp)) print_confusion_matrix(test_label, hyp)
import pandas as pd import numpy as np from svm_basic import svm_basic data = pd.read_csv('linearly_separable.csv', delimiter=",", header=None) Xin = data.iloc[:, :2] Yin = data.iloc[:, 2] dataArr = Xin.values.tolist() labelArr = Yin.values.tolist() splitpoint = int(len(labelArr) * 0.5) train_x = dataArr[:splitpoint] train_y = labelArr[:splitpoint] test_x = dataArr[splitpoint:] test_y = labelArr[splitpoint:] svm = svm_basic(1, 0.001, 50) svm.fit(train_x, train_y) w0 = svm.weights.item(0) w1 = svm.weights.item(1) b = svm.b.item(0) print("parameters: ") print(w0, w1, b) svm.predict(train_x, train_y) svm.predict(test_x, train_y) n = Xin.shape[0] xcord1 = [] ycord1 = []
y = a * x - b[0,0]/weights[1] ax.fill_between(x, y, y2=min_y - 1, color='red', alpha=0.5) ax.fill_between(x, y, y2=max_y + 1, color='blue', alpha=0.5) plt.tight_layout() ax.axis([min_x - 1, max_x + 1, min_y - 1, max_y + 1]) ax.plot(x, y) plt.xlabel('x1') plt.ylabel('x2') plt.savefig(name) if __name__ == '__main__': svm_classifier = svm_basic(maxIter=100) X, Y = get_data_and_labels('linearly_separable.csv') plot_data(X, Y, name='Linearly Seperable Data') print('Saving initial scatter plot of data as Linearly Seperable Data.png') print('Splitting data') train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size=0.2) weights, b = svm_classifier.fit(train_x, train_y) hyp = svm_classifier.predict(test_x) print('Accuracy: {0:.4f}'.format(accuracy_score(test_y.values, hyp))) print(confusion_matrix(test_y, hyp)) plot_data(X, Y, weights, b, plot_line=True, name='Linearly Separated Data') print('Saving final result as Linearly Separated Data.png')
dataMat = [] labelMat = [] fr = open(fileName) for line in fr.readlines(): lineArr = line.strip().split(',') dataMat.append([float(lineArr[0]), float(lineArr[1])]) labelMat.append(float(lineArr[2])) return dataMat, labelMat X, Y = loadDataSet('linearly_separable.csv') plt.scatter([i[0] for i in X], [i[1] for i in X], c=Y, alpha=0.5) plt.savefig("UsingLabels.png") clf = svm_basic() b, weights = clf.fit(X, Y) hypotheses = clf.predict(np.mat(X)) labels = [] x = np.arange(min([i[0] for i in X]), max([i[0] for i in X]), 0.5) list_x = x.tolist() list_x = np.append(list_x, [0, -float(b) / float(weights[1])]) x = np.array(list_x) y = [] for i in range(0, len(x)): y.append((-float(b) - (float(weights[1]) * list_x[i])) / float(weights[0])) for i in range(0, len(hypotheses)): if hypotheses[i] >= 0:
tp += 1.0 elif l == 1 and h == 0: tn += 1.0 elif l == 0 and h == 0: fn += 1.0 else: tn += 1 print('-----------------------------') print('\tConfusion Matrix') print('-----------------------------') print('\t\tPredicted') print('\tActual\tNO\tYES') print('-----------------------------') print('\tNO\t', tn, '\t', fp) print('-----------------------------') print('\tYES\t', fn, '\t', tp) print('-----------------------------') dataArr, labelArr = get_data('linearly_separable.csv') test = svm_basic(mat(dataArr), mat(labelArr).transpose(), 1, 0.001) b, alphas = test.smoPK(50) w = test.fit() w = asmatrix(w) hypotheses = test.predict(dataArr) print("b =", b) print("weights =\n", w) plot_fit(w, dataArr, labelArr, squeeze(asarray(b))) print('Accuracy:', accuracy(labelArr, hypotheses)) print_confusion_matrix(labelArr, hypotheses)