Skip to content

Commit

Permalink
[Course] Test script
Browse files Browse the repository at this point in the history
Type : feat.

Abstract:
* Test script
  • Loading branch information
JackFunfia committed May 24, 2023
1 parent eca7da5 commit 4143dc4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
4 changes: 1 addition & 3 deletions ch02/perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ def __init__(self, eta=0.01, n_iter=50, random_state=1):

def fit(self, X, y):
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.Shape[1])
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
self.error_ = []

for _ in range(self.n_iter):
errors = 0
zip_data = zip(X, y)
for xi, target in zip_data:
print(f"{xi}, {target}")
update = self.eta * (target - self.predict(xi))
print(f"update {update}")
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0)
Expand Down
79 changes: 64 additions & 15 deletions ch02/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,71 @@
y = df.iloc[:, 4].values
y = np.where(y == "Iris-setosa", -1, 1)

X = df.iloc[:, [0, 2, 4]].values
setosa_array = X[(X[:, 2] == "Iris-setosa")]
virginica_array = X[(X[:, 2] == "Iris-virginica")]
versicolor_array = X[(X[:, 2] == "Iris-versicolor")]

plt.scatter(setosa_array[:, 0], setosa_array[:, 1],
color="red", marker="o", label="setosa")
plt.scatter(virginica_array[:, 0], virginica_array[:, 1],
color="blue", marker="x", label="virginica")
plt.scatter(versicolor_array[:, 0], versicolor_array[:, 1],
color="green", marker="^", label="versicolor")
X = df.iloc[:100, [0, 2]].values

# temp_X = df.iloc[:, [0, 2, 4]].values
# setosa_array = temp_X[(temp_X[:, 2] == "Iris-setosa")]
# virginica_array = temp_X[(temp_X[:, 2] == "Iris-virginica")]
# versicolor_array = temp_X[(temp_X[:, 2] == "Iris-versicolor")]
#
# plt.scatter(setosa_array[:, 0], setosa_array[:, 1],
# color="red", marker="o", label="setosa")
# plt.scatter(virginica_array[:, 0], virginica_array[:, 1],
# color="blue", marker="x", label="virginica")
# plt.scatter(versicolor_array[:, 0], versicolor_array[:, 1],
# color="green", marker="^", label="versicolor")
#
# plt.xlabel("sepal length [cm]")
# plt.ylabel("petal length [cm]")
# plt.legend(loc="upper left")
# plt.show()

ppn = Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)
# plt.plot(range(1, len(ppn.error_) + 1),
# ppn.error_, marker="o")
# plt.xlabel("Epochs")
# plt.xlabel("Number of updates")
# plt.show()

from matplotlib.colors import ListedColormap

for idx, cl in enumerate(np.unique(y)):
print(f"index : {idx}, cl {cl}")
print(X[y == cl, 0])

def plot_decision_regions(X, y, classifier: Perceptron, resolution=0.02):
markers = ("s", "x", "o", "^", "v")
colors = ("red", "blue", "lightgreen", "gray", "cyan")
cmap = ListedColormap(colors[:len(np.unique(y))])

x1_min = X[:, 0].min() - 1
x1_max = X[:, 0].max() + 1

x2_min = X[:, 1].min() - 1
x2_max = X[:, 1].max() + 1

xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))

Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha=0.3, cmap=cmap)
plt.xlim(xx1.min(), xx1.max())
plt.ylim(xx2.min(), xx2.max())

for idx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y == cl, 0],
y=X[y == cl, 1],
alpha=0.8,
c=cmap[idx],
marker=markers[idx],
llabel=cl,
edgecolors="black")


plot_decision_regions(X, y, classifier=ppn)
plt.xlabel("sepal length [cm]")
plt.ylabel("petal length [cm]")
plt.legend(loc="upper left")
plt.show()

ppn = Perceptron(eta=0.1, n_iter=10)
# ppn.fit(X, y)
plt.show()

0 comments on commit 4143dc4

Please sign in to comment.