In [1]:
import numpy as np
import pandas as pd

In [8]:
class Perceptron(object):
    def __init(self):
        super(Perceptron, object).__init(self)
    
    def fit(self, X, Y, eta=0.001, iterations=100):
        self.w_ = np.zeros(1 + X.shape[1])
        
        self.errors_ = []
        for _ in range(iterations):
            errors = 0
            for xi, target in zip(X, y):
                
                update = eta * (target - self.predict(xi))
#                 if iterations % 20 == 0:
#                     print "w: {}, xi: {}, target: {}, update: {}".format(
#                        self.w_, xi, target, update)
                self.w_[1:] += update * xi
                self.w_[0] += update
                errors += float(update != 0.0)
            #print "errors: ", errors
            self.errors_.append(errors)
        return self
    
    def net_input(self, X):
        """Calculate net input"""
#         print "net input:"
#         print X, self.w_[1:]
#         print "end of net input"
        return np.dot(X, self.w_[1:]) + self.w_[0]
    
    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, -1)

    
p = Perceptron()
p.fit(x, y)


Out[8]:
<__main__.Perceptron at 0x1092357d0>

In [36]:
df = pd.read_csv("data/iris.txt", delimiter=" ")
df.sample(5)


Out[36]:
sl sw pl pw c
73 6.1 2.8 4.7 1.2 2
17 5.1 3.5 1.4 0.3 1
83 6.0 2.7 5.1 1.6 2
5 5.4 3.9 1.7 0.4 1
22 4.6 3.6 1.0 0.2 1

In [4]:
y = df.iloc[0:100, 4].values
x = df.iloc[0:100, [2, 3]].values
x, y


Out[4]:
(array([[ 1.4,  0.2],
        [ 1.4,  0.2],
        [ 1.3,  0.2],
        [ 1.5,  0.2],
        [ 1.4,  0.2],
        [ 1.7,  0.4],
        [ 1.4,  0.3],
        [ 1.5,  0.2],
        [ 1.4,  0.2],
        [ 1.5,  0.1],
        [ 1.5,  0.2],
        [ 1.6,  0.2],
        [ 1.4,  0.1],
        [ 1.1,  0.1],
        [ 1.2,  0.2],
        [ 1.5,  0.4],
        [ 1.3,  0.4],
        [ 1.4,  0.3],
        [ 1.7,  0.3],
        [ 1.5,  0.3],
        [ 1.7,  0.2],
        [ 1.5,  0.4],
        [ 1. ,  0.2],
        [ 1.7,  0.5],
        [ 1.9,  0.2],
        [ 1.6,  0.2],
        [ 1.6,  0.4],
        [ 1.5,  0.2],
        [ 1.4,  0.2],
        [ 1.6,  0.2],
        [ 1.6,  0.2],
        [ 1.5,  0.4],
        [ 1.5,  0.1],
        [ 1.4,  0.2],
        [ 1.5,  0.1],
        [ 1.2,  0.2],
        [ 1.3,  0.2],
        [ 1.5,  0.1],
        [ 1.3,  0.2],
        [ 1.5,  0.2],
        [ 1.3,  0.3],
        [ 1.3,  0.3],
        [ 1.3,  0.2],
        [ 1.6,  0.6],
        [ 1.9,  0.4],
        [ 1.4,  0.3],
        [ 1.6,  0.2],
        [ 1.4,  0.2],
        [ 1.5,  0.2],
        [ 1.4,  0.2],
        [ 4.7,  1.4],
        [ 4.5,  1.5],
        [ 4.9,  1.5],
        [ 4. ,  1.3],
        [ 4.6,  1.5],
        [ 4.5,  1.3],
        [ 4.7,  1.6],
        [ 3.3,  1. ],
        [ 4.6,  1.3],
        [ 3.9,  1.4],
        [ 3.5,  1. ],
        [ 4.2,  1.5],
        [ 4. ,  1. ],
        [ 4.7,  1.4],
        [ 3.6,  1.3],
        [ 4.4,  1.4],
        [ 4.5,  1.5],
        [ 4.1,  1. ],
        [ 4.5,  1.5],
        [ 3.9,  1.1],
        [ 4.8,  1.8],
        [ 4. ,  1.3],
        [ 4.9,  1.5],
        [ 4.7,  1.2],
        [ 4.3,  1.3],
        [ 4.4,  1.4],
        [ 4.8,  1.4],
        [ 5. ,  1.7],
        [ 4.5,  1.5],
        [ 3.5,  1. ],
        [ 3.8,  1.1],
        [ 3.7,  1. ],
        [ 3.9,  1.2],
        [ 5.1,  1.6],
        [ 4.5,  1.5],
        [ 4.5,  1.6],
        [ 4.7,  1.5],
        [ 4.4,  1.3],
        [ 4.1,  1.3],
        [ 4. ,  1.3],
        [ 4.4,  1.2],
        [ 4.6,  1.4],
        [ 4. ,  1.2],
        [ 3.3,  1. ],
        [ 4.2,  1.3],
        [ 4.2,  1.2],
        [ 4.2,  1.3],
        [ 4.3,  1.3],
        [ 3. ,  1.1],
        [ 4.1,  1.3]]),
 array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2]))

In [ ]:


In [ ]:


In [ ]: