--What is adaline? --Data used this time
There was a very easy-to-understand article, so I will post it. It's an improved version of the so-called Perset Pron. 2.ADALINE
This time, I will use the data of quite famous iris. https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data Please download this.
This time I would like to implement it in python. As part of the lesson, I wrote it without much research, so please understand that there are some parts that are not functionalized.
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)
# Check the initial value of the weight
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
#Update to weight
w[1:] += eta * np.dot(errors,X)
w[0] += eta * errors.sum()
#Calculation of cost function
cost = (errors**2).sum() / 2.0
cost_.append(cost)
# Checking the weight
print(w)
# Plot for the time being
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()
I wrote it almost sequentially, but the graph is plotted well. I am glad that I was able to deepen my understanding of adaline.
How was that? It's not very clean code, but I posted it because I also wrote python. In the future, I would like to post higher-level items.
Recommended Posts