-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (37 loc) · 1.13 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Cyrus Burt |JAN 2019| MIT LICENSE
"""
main.py
uses my simple perceptron to classify irises
>>> model.pred([1.4,0.2])
1.0
>>> model.pred([4.5,1.6])
-1.0
"""
import perceptron
import pandas as pd
import matplotlib.pyplot as plt
import random
from sklearn.utils import shuffle
model = perceptron.perceptron(0.8) #initialize a new perceptron with a learning rate of 0.1
data = pd.read_csv("iris.data") #read iris csv
data = shuffle(data)
species = list(data.iloc[0:, 4]) #initialize a new array of the iris species, these will get encoded
petal_length = list(data.iloc[0:, 2])
petal_width = list(data.iloc[0:, 3])
encoded_species = []
for i in species:
if i == "Iris-setosa":
encoded_species.append(1.0)
if i == "Iris-versicolor":
encoded_species.append(-1.0)
for _ in range(100):
for i in range(len(encoded_species)):
model.train([petal_length[i], petal_width[i]], encoded_species[i])
def classify_iris(petal_l, petal_w):
if model.pred([petal_l, petal_w]) == 1:
return "Iris-setosa"
else:
return "Iris-versicolor"
if __name__ == "__main__":
import doctest
doctest.testmod()