-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbinary_perceptron.py
94 lines (69 loc) · 2.42 KB
/
binary_perceptron.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# encoding=utf-8
# @Author: WenDesi
# @Date: 09-08-16
# @Email: [email protected]
# @Last modified by: WenDesi
# @Last modified time: 08-11-16
import pandas as pd
import numpy as np
import cv2
import random
import time
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score
class Perceptron(object):
def __init__(self):
self.learning_step = 0.00001
self.max_iteration = 5000
def predict_(self, x):
wx = sum([self.w[j] * x[j] for j in xrange(len(self.w))])
return int(wx > 0)
def train(self, features, labels):
self.w = [0.0] * (len(features[0]) + 1)
correct_count = 0
time = 0
while time < self.max_iteration:
index = random.randint(0, len(labels) - 1)
x = list(features[index])
x.append(1.0)
y = 2 * labels[index] - 1
wx = sum([self.w[j] * x[j] for j in xrange(len(self.w))])
if wx * y > 0:
correct_count += 1
if correct_count > self.max_iteration:
break
continue
for i in xrange(len(self.w)):
self.w[i] += self.learning_step * (y * x[i])
def predict(self,features):
labels = []
for feature in features:
x = list(feature)
x.append(1)
labels.append(self.predict_(x))
return labels
if __name__ == '__main__':
print 'Start read data'
time_1 = time.time()
raw_data = pd.read_csv('../data/train_binary.csv', header=0)
data = raw_data.values
imgs = data[0::, 1::]
labels = data[::, 0]
# 选取 2/3 数据作为训练集, 1/3 数据作为测试集
train_features, test_features, train_labels, test_labels = train_test_split(
imgs, labels, test_size=0.33, random_state=23323)
# print train_features.shape
# print train_features.shape
time_2 = time.time()
print 'read data cost ', time_2 - time_1, ' second', '\n'
print 'Start training'
p = Perceptron()
p.train(train_features, train_labels)
time_3 = time.time()
print 'training cost ', time_3 - time_2, ' second', '\n'
print 'Start predicting'
test_predict = p.predict(test_features)
time_4 = time.time()
print 'predicting cost ', time_4 - time_3, ' second', '\n'
score = accuracy_score(test_labels, test_predict)
print "The accruacy socre is ", score