Created
April 6, 2022 00:55
-
-
Save sithu/4722649d23c83440f2067ed429fa434b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from matplotlib import pyplot | |
# Some functions to plot our points and draw the lines | |
def plot_points(features, labels): | |
X = np.array(features) | |
y = np.array(labels) | |
spam = X[np.argwhere(y==1)] | |
ham = X[np.argwhere(y==0)] | |
pyplot.scatter([s[0][0] for s in spam], | |
[s[0][1] for s in spam], | |
s = 35, | |
color = 'cyan', | |
edgecolor = 'k', | |
marker = '^') | |
pyplot.scatter([s[0][0] for s in ham], | |
[s[0][1] for s in ham], | |
s = 25, | |
color = 'red', | |
edgecolor = 'k', | |
marker = 's') | |
pyplot.xlabel('x_1') | |
pyplot.ylabel('x_2') | |
pyplot.legend(['label 1','label 0']) | |
def plot_model(X, y, model): | |
X = np.array(X) | |
y = np.array(y) | |
plot_step = 0.01 | |
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 | |
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 | |
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step), | |
np.arange(y_min, y_max, plot_step)) | |
Z = model.predict(np.c_[xx.ravel(), yy.ravel()]) | |
Z = Z.reshape(xx.shape) | |
pyplot.contour(xx, yy, Z,colors = 'k',linewidths = 3) | |
plot_points(X, y) | |
pyplot.contourf(xx, yy, Z, colors=['red', 'blue'], alpha=0.2, levels=range(-1,2)) | |
pyplot.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment