diff --git a/ch02/perceptron.py b/ch02/perceptron.py new file mode 100644 index 00000000..2eb03990 --- /dev/null +++ b/ch02/perceptron.py @@ -0,0 +1,37 @@ +### +# page 025 +## +import numpy as np + + +class Perceptron: + def __init__(self, eta=0.01, n_iter=50, random_state=1): + self.error_ = None + self.w_ = None + self.eta = max(0.0, min(eta, 1.0)) + self.n_iter = n_iter + self.random_state = random_state + + 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.error_ = [] + + for _ in range(self.n_iter): + errors = 0 + zip_data = zip(X, y) + for xi, target in zip_data: + update = self.eta * (target - self.predict(xi)) + self.w_[1:] += update * xi + self.w_[0] += update + errors += int(update != 0) + self.error_.append(errors) + return self + + def net_input(self, X): + res = np.dot(X, self.w_[1:]) + self.w_[0] + return res + + def predict(self, X): + res = np.where(self.net_input(X) >= 0.0, 1, -1) + return res diff --git a/ch02/test.py b/ch02/test.py new file mode 100644 index 00000000..63e4f3ea --- /dev/null +++ b/ch02/test.py @@ -0,0 +1,99 @@ +import matplotlib.pyplot as plt +import pandas as pd +import requests +import io +import os +from perceptron import Perceptron +PATH = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" + +with requests.get(PATH) as response: + raw_data = response.text + +# s = os.path.join("https://archive.ics.uci.edu", "ml", +# "machine-learning-databases", +# "iris", "iris.data") +s = os.path.join(PATH) + +df = pd.read_csv(s) +# print(df.tail()) +# string_data = io.StringIO(raw_data) +# +# df = pd.read_csv(string_data, header=None, encoding="utf-8") +# +# print(df.tail()) + +import matplotlib.pyplot as plot +import numpy as np + +y = df.iloc[:100, 4].values +y = np.where(y == "Iris-setosa", -1, 1) + +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") + unique_len = len(np.unique(y)) + cmap = ListedColormap(colors[:unique_len]) + + 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.colors[idx], + marker=markers[idx], + label=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() \ No newline at end of file diff --git a/ch03/scikit_learb_test.py b/ch03/scikit_learb_test.py new file mode 100644 index 00000000..75eb985d --- /dev/null +++ b/ch03/scikit_learb_test.py @@ -0,0 +1,16 @@ +from sklearn import datasets +from sklearn.model_selection import train_test_split +from sklearn.preprocessing import StandardScaler +import numpy as np + +iris = datasets.load_iris() +X = iris.data[:, [2, 3]] +y = iris.target +print(f"Class labels : {np.unique(y)}") + +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y) + +sc = StandardScaler() +sc.fit(X_train) +X_train_std = sc.transform(X_train) +X_test_std = sc.transform(X_test) diff --git a/ch03/sigmoid_test.py b/ch03/sigmoid_test.py new file mode 100644 index 00000000..d59021ed --- /dev/null +++ b/ch03/sigmoid_test.py @@ -0,0 +1,21 @@ +import matplotlib.pyplot as plt +import numpy as np + + +def sigmoid(z): + return 1.0 / (1.0 + np.exp(-z)) + + +z = np.arange(-7, 7, 0.1) +phi_z = sigmoid(z) +plt.plot(z, phi_z) +plt.axvline(0.0, color="k") +plt.ylim(-0.1, 1.1) +plt.xlabel("z") +plt.ylabel("$\phi (z)$") +plt.yticks([0.0, 0.5, 1.0]) +ax = plt.gca() +ax.yaxis.grid(True) +plt.tight_layout() + +plt.show()