Last active
July 17, 2016 02:22
-
-
Save kwatch/be8bdc952b8ca79e3066580e8e8646a7 to your computer and use it in GitHub Desktop.
(obsolete) (ML) Example1
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
# -*- coding: utf-8 -*- | |
#from ch02_1 import Perceptron | |
from perceptron import Perceptron, Perceptron2 | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
if 0: | |
#url = ('https://archive.ics.uci.edu/ml/' | |
# 'machine-learning-databases/iris/iris.data') | |
url = 'iris.data' | |
df = pd.read_csv(url, header=None) | |
#df.tail() | |
# select setosa and versicolor | |
y = df.iloc[0:100, 4].values | |
y = np.where(y == 'Iris-setosa', -1, 1) | |
#print(type(y)) #=> <class 'numpy.ndarray'> | |
#print(y) #=> [-1 -1 -1 ..... 1 1 1] | |
y = [-1] * 50 + [1] * 50 | |
# extract sepal length and petal length | |
X = df.iloc[0:100, [0, 2]].values | |
print(type(X)) #=> <class 'numpy.ndarray'> | |
print(X) | |
if 1: | |
import sys | |
## load csv file | |
import csv | |
filename = 'iris.data' | |
table__ = [] | |
with open(filename) as f: | |
reader = csv.reader(f) | |
for line in reader: | |
v1, v2, v3, v4, v5 = line | |
# [sepal_h, sepal_w, pepal_h, pepal_w, label] | |
row_ = [float(v1), float(v2), float(v3), float(v4), v5] | |
table__.append(row_) | |
if 1: | |
# [sepal_h, pepal_h] | |
input__ = [ [row_[0], row_[2]] for row_ in table__[0:100] ] | |
# | |
def fn(name): | |
if name == 'Iris-setosa': return -1 | |
elif name == 'Iris-versicolor': return 1 | |
else: | |
raise ValueError("%r: unexpected value" % name) | |
expected_ = [ fn(row_[4]) for row_ in table__[0:100] ] | |
#ppn = Perceptron(eta=0.1, n_iter=10) | |
ppn = Perceptron2(eta=0.1, n_iter=10) | |
ppn.fit(input__, expected_) | |
if 1: | |
plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='o') | |
plt.xlabel('Epochs') | |
plt.ylabel('Number of misclassifications') | |
plt.tight_layout() | |
# plt.savefig('./perceptron_1.png', dpi=300) | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment