In [1]:
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix, classification_report
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import seaborn as sns
In [2]:
# load iris data
# sample:150, features:4, target:3
iris = load_iris()
# get two iris kinds among three iris kinds
idx = np.in1d(iris.target, [0, 2]) # true false incoding ( target 0 or 2 is True )
X = iris.data[idx, 0:2] # set X ( first feature and second feature )
y = iris.target[idx] # set y ( target )
# draw sctter flot
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()
In [3]:
iris.keys()
Out[3]:
In [4]:
# sepal(꽃받침), petal(꽃잎)
iris.feature_names
Out[4]:
In [5]:
iris.data[:,:2][:5], y[:5]
Out[5]:
In [6]:
iris.data[:,:2][-5:], y[-5:]
Out[6]:
In [7]:
def plot_perceptron(n):
model = Perceptron(n_iter=n, eta0=0.1, random_state=1).fit(X, y)
XX_min = X[:, 0].min() - 1; XX_max = X[:, 0].max() + 1;
YY_min = X[:, 1].min() - 1; YY_max = X[:, 1].max() + 1;
XX, YY = np.meshgrid(np.linspace(XX_min, XX_max, 1000), np.linspace(YY_min, YY_max, 1000))
ZZ = model.predict(np.c_[XX.ravel(), YY.ravel()]).reshape(XX.shape)
cmap = mpl.colors.ListedColormap(sns.color_palette("Set3"))
plt.contourf(XX, YY, ZZ, cmap=cmap)
plt.scatter(X[:, 0], X[:, 1], s=50, linewidth=2, edgecolor='k', c=y, cmap=cmap)
plt.xlim(XX_min, XX_max)
plt.ylim(YY_min, YY_max)
plt.grid(False)
return model
In [8]:
for n in range(1, 20):
model = plot_perceptron(n)
y_pred = model.predict(X)
print(n, end=" - ")
print(accuracy_score(y_pred, y))
plt.show()
In [ ]: