-
Notifications
You must be signed in to change notification settings - Fork 0
/
Perceptron_classification.py
79 lines (65 loc) · 2.07 KB
/
Perceptron_classification.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
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 9 13:42:52 2019
@author: Amirhossein Forouzani
"""
import csv
import numpy as np
from sklearn.metrics import accuracy_score
import pandas as pd
from sklearn.neighbors import NearestCentroid
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist
import numpy as np
from mlxtend.plotting import plot_decision_regions
#defining a class for OOP
class Perceptron(object):
def __init__(self, eta=0.01, epochs=50):
self.eta = eta
self.epochs = epochs
def train(self, X, y):
self.w_ = np.zeros(1 + X.shape[1])
self.errors_ = []
for _ in range(self.epochs):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self
def net_input(self, X):
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)
#Defining the main function for file import
dataset_name = "synthetic2"
#dataset_label = "label"
df_train = pd.read_csv(dataset_name + "_train.csv")
df_test = pd.read_csv(dataset_name + "_test.csv")
#df_train_label =pd.read_csv(dataset_label + "_train.csv")
#df_test_label =pd.read_csv(dataset_label + "_test.csv")
df_train.columns = ['f1','f2','labels']
df_test.columns = ['f1','f2','labels']
X = df_test.iloc[:, [0,1]].values
y = df_test.iloc[:, 2].values
#print (y)
y = np.where(y == 1, -1, 1)
y1 = y
#print(y)
ppn = Perceptron(epochs=20, eta=0.1)
ppn.train(X, y)
error_train = 1 - accuracy_score(y, y1)
print (error_train)
#error_test = 1 - accuracy_score(y_test, y_pred_test)
print('Weights: %s' % ppn.w_)
plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron')
plt.xlabel('X1')
plt.ylabel('X2')
plt.show()
plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Misclassifications')
plt.show()