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 numpy as np | |
class Perceptron: | |
def __init__(self, eta=0.01, n_iter=50, random_state=1): | |
""" | |
eta: 学習率 | |
n_iter: トレーニングデータの学習回数 | |
random_state: 乱数のシード。固定しておく。 | |
""" | |
self.eta = eta |
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
#Irisのデータを取得する | |
import pandas as pd | |
df = pd.read_csv("/Users/rikiyaota/Documents/self-study/machine-learning/python-machine-learning-book-2nd-edition/code/ch02/iris.data", | |
header=None) | |
#データセットに少し手を加える | |
import matplotlib.pyplot as plt | |
#データの先頭から100個だけを抜き出し、クラスラベルだけを抽出する | |
y = df.iloc[0:100, 4].values |
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
#パーセプトロンのインスタンス生成 | |
ppn = Perceptron(eta=0.1, n_iter=10) | |
#学習!! | |
ppn.fit(X, y) | |
#エポック毎の誤分類の数をプロットしてみます。 | |
plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker="o") | |
plt.xlabel("Epoch") | |
plt.ylabel("Number of update") | |
plt.show() |
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 numpy as np | |
#3x3メッシュを作成 | |
xx, yy = np.meshgrid(np.arange(3), np.arange(3)) | |
print(xx) | |
#--------------------- | |
#array([[0, 1, 2], | |
# [0, 1, 2], | |
# [0, 1, 2]]) |
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
from matplotlib.colors import ListedColormap | |
def plot_decision_regions(X, y, classifier, resolution=0.02): | |
""" | |
X: 入力データ X.shape = [n_samples, n_features] | |
y: 教師ラベル | |
classifier: 分類を行うモデルのインスタンス | |
resolution: 領域の解像度 | |
""" | |
#マーカーとカラーアップの準備 |
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
print(xx.ravel()) | |
#---------------------------------------- | |
#array([0, 1, 2, 0, 1, 2, 0, 1, 2]) | |
#---------------------------------------- | |
print(yy.ravel()) | |
#---------------------------------------- | |
#array([0, 0, 0, 1, 1, 1, 2, 2, 2]) | |
#---------------------------------------- |
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
print(np.array([xx.ravel(), yy.ravel()]).T) | |
#--------------------------------------------------- | |
#array([[0, 0], | |
# [1, 0], | |
# [2, 0], | |
# [0, 1], | |
# [1, 1], | |
# [2, 1], | |
# [0, 2], | |
# [1, 2], |
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
#ppnはパーセプトロンのインスタンス | |
plot_decision_regions(X, y, classifier=ppn) | |
#それぞれの軸にラベルをつける | |
plt.xlabel("sepal length [cm]") | |
plt.ylabel("petal length [cm]") | |
#凡例をつける | |
plt.legend(loc="upper left") |
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
/** | |
* 1秒待ってから"First"と表示し、その直後に"Second"と表示したいと思っている | |
* (以下のコードでは逆になってしまいます。うまくない) | |
*/ | |
setTimeout(()=>{ | |
console.log("First"); | |
}, 1000); | |
console.log("Second"); |
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
/** | |
* Promiseの基本的な使い方 | |
* | |
* new Promise((resolve, reject)=>{ | |
* 何かしら非同期処理 | |
* ・・・・ | |
* ・・・・ | |
* | |
* if(成功!) resolve(value); | |
* if(失敗!) reject(error); |
OlderNewer