plt.figure(figsize=(14, 14)) sns.heatmap(corr, annot=True) plt.ylim(corr.shape[1], 0) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(data[column_names[1:9]], data[column_names[10]], test_size=0.2, random_state=5) model = svm.SVC(C=0.1, kernel='poly', degree=1, gamma='scale') model.fit(X_train, y_train) prediction = model.predict(X_test) print('accuracy:', metrics.accuracy_score(prediction, y_test)) mtrx = confusion_matrix(y_test, prediction) np.set_printoptions(precision=2) plot_confusion_matrix(mtrx) #plt.show() endtime = datetime.datetime.now() print(endtime - starttime) # we can make a pretty visualizxation xs = data.loc[:, 'Uniformity of Cell Size'] ys = data.loc[:, 'Uniformity of Cell Shape'] #print(xs,ys) from b import perceptron (w, b) = perceptron(xs, ys) from helperfunctions import visboundary visboundary(w, b, xs, ys)
def test(): def perceptron(xs, ys): """ function w=perceptron(xs,ys); Implementation of a Perceptron classifier Input: xs : n input vectors of d dimensions (nxd) ys : n labels (-1 or +1) Output: w : weight vector (1xd) b : bias term """ assert ( len(xs.shape) == 2 ), "The first input to Perceptron must be a _matrix_ of row input vecdtors." assert ( len(ys.shape) == 1 ), "The second input to Perceptron must be a _vector_ of n labels (try ys.flatten())." n, d = xs.shape # so we have n input vectors, of d dimensions each ## fill in code ... w = [0] * d b = [0] for i in range(n): if ys[i] * (sum(w * xs[i] + b)) <= 0: w += ys[i] * xs[i] #print(w) b += ys[i] #print(b) # if ys[i] * (sum(w*xs[i]+b[i])) <= 0: # w += ys[i]*xs[i] # b[i] += ys[i] #b = np.sign(b) ## ... until here print("updated w is ", w) print("b is ", b) return (w, b) # </GRADED> # number of input vectors N = 100 # generate random (linarly separable) data xs = np.random.rand(N, 2) * 10 - 5 # defining random hyperplane w0 = np.random.rand(2) b0 = rand() * 2 - 1 # assigning labels +1, -1 labels depending on what side of the plane they lie on ys = np.sign(xs.dot(w0) + b0) #print("xs is ", xs) #print("w0 is ", w0) #print("b0 is ", b0) #print("ys is ", ys) # call perceptron to find w from data w, b = perceptron(xs.copy(), ys.copy()) #print(w.shape,b.shape) # test if all points are classified correctly print("whether converge? ", all(np.sign(ys * (xs.dot(w) + b)) == 1.0)) print("\n") assert (all(np.sign(ys * (xs.dot(w) + b)) == 1.0) ) # yw'x should be +1.0 for every input print("Looks like you passed the Perceptron test! :o)") # we can make a pretty visualizxation from helperfunctions import visboundary visboundary(w, b, xs, ys)