Skip to content

Instantly share code, notes, and snippets.

@kwatch
Last active July 17, 2016 02:22
Show Gist options
  • Save kwatch/72d73f82196e2339b50c6cd8cb8f76e2 to your computer and use it in GitHub Desktop.
Save kwatch/72d73f82196e2339b50c6cd8cb8f76e2 to your computer and use it in GitHub Desktop.
(obsolete) (ML) Example1
# -*- coding: utf-8 -*-
import sys
import numpy as np
class Perceptron(object):
"""Perceptron classifier.
Parameters
------------
eta : float
Learning rate (between 0.0 and 1.0)
n_iter : int
Passes over the training dataset.
Attributes
-----------
w_ : 1d-array
Weights after fitting.
errors_ : list
Number of misclassifications in every epoch.
"""
def __init__(self, eta=0.01, n_iter=10):
self.eta = eta
self.n_iter = n_iter
def fit(self, X, y):
"""Fit training data.
Parameters
----------
X : {array-like}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
Returns
-------
self : object
"""
if hasattr(X, 'shape'):
n_features = X.shape[1]
else:
n_features = len(X[0])
#print(n_features) #=> 2
self.w_ = np.zeros(1 + n_features)
#print(self.w_) #=> [0.0 0.0 0.0] <numpy.ndarray>
sys.stderr.write("\033[0;31m*** debug: self.w_=%r\033[0m\n" % (self.w_, ))
self.errors_ = []
for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
#print(xi) #=> ex: [5.1 1.4] <numpy.ndarray>
#print(target) #=> -1 or 1 <int>
#print(self.predict(xi)) #=> [1] or [-1] <numpy.ndarray>
#print(target - self.predict(xi)) #=> -2 or 2 or 0 <numpy.int64>
update = self.eta * (target - self.predict(xi))
#print(update) #=> -0.2 or 0.2 or 0.0 <numpy.float64>
#print(update * xi) #=> ex: [-1.02 -0.28] (= -0.2 * [5.1 1.4])
self.w_[1:] += update * xi
self.w_[0] += update
#print(self.w_[0]) #=> ex: -0.2, 0.0, -0.4
#print(update != 0.0) #=> False or True
#print(int(update != 0.0)) #=> 0 or 1
errors += int(update != 0.0)
#print(errors) #=> ex: 2 2 3 2 1 0 0 0 0 0
self.errors_.append(errors)
sys.stderr.write("\033[0;31m*** debug: self.errors_=%r\033[0m\n" % (self.errors_, ))
#print(self.errors_) #=> ex: [2, 2, 3, 2, 1, 0, 0, 0, 0, 0] <list>
#print(self.w_) #=> ex: [-0.4 -0.68 1.82] <numpy.ndarray>
return self
def net_input(self, X):
"""Calculate net input"""
#print(X) #=> ex: [5.1 1.4], [4.9 1.4], [4.7 1.3], ...
#print(self.w_) #=> ex: [0.0 0.0 0.0], [-0.2 -1.02 -0.28], ..., [0.0 0.38 0.66] [-0.2 -0.64 0.38], [0.0 0.76 1.32], [-0.4 -1.18 0.74], ..., [-0.4 -0.68 1.82]
#sys.stderr.write("\033[0;31m*** debug: self.w_[1:]=%r\033[0m\n" % (self.w_[1:], ))
#sys.stderr.write("\033[0;31m*** debug: X=%r\033[0m\n" % (X, ))
#sys.stderr.write("\033[0;31m*** debug: self.w_[1:]=%r\033[0m\n" % (self.w_[1:], ))
#sys.stderr.write("\033[0;31m*** debug: self.w_[0]=%r\033[0m\n" % (self.w_[0], ))
# ex: ret = -5.56 (= [4.9, 1.4] * [-1.02, -0.28] + (-0.2))
ret = np.dot(X, self.w_[1:]) + self.w_[0]
#sys.stderr.write("\033[0;31m*** debug: ret=%r\033[0m\n" % (ret, ))
return np.dot(X, self.w_[1:]) + self.w_[0]
def predict(self, X):
"""Return class label after unit step"""
predict = np.where(self.net_input(X) >= 0.0, 1, -1)
#print(predict) #=> array(1) or array(-1) <numpy.ndarray>
return predict
class Perceptron2(object):
def __init__(self, eta=0.01, n_iter=10):
assert 0.0 < eta <= 1.0
self.eta = eta # 学習率 (0.0 < eta <= 1.0)
self.n_iter = n_iter # 繰り返し回数
def fit(self, input__, expected_):
assert len(input__) == len(expected_)
n_features = len(input__[0]) # ex: 2
weight_ = [0.0] * (n_features + 1) # ex: [0.0, 0.0, 0.0]
errors_ = []
eta = self.eta # 学習率 (0.0 < eta <= 1.0)
for _ in range(self.n_iter):
error_count = 0
for input_, expected in zip(input__, expected_):
z = self.net_input(input_, weight_)
guess = self.predict(z) #=> 1 or -1
if expected != guess:
#print(expected - guess) #=> 2 or -2
update = eta * (expected - guess)
#print(update) #=> 0.2 or -0.2
weight_[0] += update * 1.0
i = 0
for x in input_: # or: for i, x in enumerate(input_, 1)
i += 1
weight_[i] += update * x
error_count += 1
errors_.append(error_count)
#print(errors_) #=> ex: [2, 2, 3, 2, 1, 0, 0, 0, 0, 0] <list>
#print(weight_) #=> ex: [-0.4 -0.68 1.82] <numpy.ndarray>
self.w_ = weight_
self.errors_ = errors_
return self
def net_input(self, input_, weight_):
assert len(input_) + 1 == len(weight_)
#z = 1.0 * weight_[0]
#for x, w in zip(input_, weight_[1:]):
# z += x * w
z = 1.0 * weight_[0] + \
sum( x * w for x, w in zip(input_, weight_[1:]) )
return z
def predict(self, z):
if z >= 0.0:
return 1 # 'Iris-virsicolor'
else:
return -1 # 'Iris-setosa'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment