LoginSignup
3
0

More than 3 years have passed since last update.

ドラゴンボールをadalineで分類してみた

Last updated at Posted at 2020-05-14

はじめに

  • adalineとは
  • 今回使用するデータ
  • 実装

adalineとは

とてもわかりやすい記事があったので貼らせてもらいます。
いわゆるパーセトプロンの改良版ですね。
2.ADALINE

今回使用するデータ

今回は結構有名なirisのデータを使用します。
https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
こちらをダウンロードしておいてください。

実装

今回はpythonで実装したいと思います。授業の一環で自分であまり調べずに書いたので、関数化していなかったりする部分がありますがご了承ください。

adaline.py
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

df = pd.read_csv(iris-data.csv,header=None)
df_new = df.drop(columns=[1,3])
df_new = df_new.replace(Iris-setosa,0)
df_new = df_new.replace(Iris-versicolor,1)
df_new

eta = 0.001
epoch = 100
cost_=[]
t = np.array(df_new[4])
X = np.array([df_new[0],df_new[2]]).T
w = np.random.normal(0.0, 0.01, size=X.shape[1] + 1)

# 重みの初期値の確認
print(w)

for I in range(epoch):
        input_ = np.dot(X,w[1:])+w[0]
        predict = np.where(input_>= 0, 1, 0)
        errors = t - predict

        # 重みに更新
        w[1:] += eta * np.dot(errors,X)
        w[0] += eta * errors.sum()

        # コスト関数の計算
        cost = (errors**2).sum() / 2.0
        cost_.append(cost)

# 重みの確認
print(w)


# とりあえずプロット        
x_a = range(4,8)
y_a = [-(w[1]/w[2])*xi-(w[0]/w[2]) for xi in x_a]
plt.scatter(df_new.iloc[:50,0],df_new.iloc[:50,1],label = Iris-versicolor)  
plt.scatter(df_new.iloc[50:,0],df_new.iloc[50:,1],label = Iris-setosa)
plt.ylabel(petal length[cm])
plt.xlabel(sepal length[cm])
plt.plot(x_a,y_a)
plt.legend()
plt.show()

image.png

ほとんど逐次処理で書いていますが、うまくグラフがプロットされています。自分的にもadalineの理解を深めることができたのでよかったです。

最後に

いかがだったでしょうか?あまりきれいなコードではありませんが、pythonも書いているということで載せてみました。今後はもっとレベルの高いものを載せていきたいと思います。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0