-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·46 lines (34 loc) · 1.2 KB
/
main.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
#!/usr/bin/env python3
"""
File name : main.py
Author : Ryky Nelson
Created Date : 10/18/2021
Python Version: Python 3.6.9
Main function:
- gets & processes the data
- separates the data into the training and test sets
- calls & feeds training data to the perceptron
- measures the training perceptron against (sparred) test data
"""
import pandas as pd
from perceptron import perceptron
if __name__ == "__main__":
with open("train.csv", 'r') as tr:
data = pd.read_csv( tr )
digit = 1
data = data.sample(frac=1, random_state=69).reset_index(drop=True)
data['label0'] = [ 1 if row == digit else -1 for row in data['label'] ]
ndata = len(data)
ntrain = int(0.80 * ndata)
nfeat = len( data.columns ) - 1
training = data.loc[ [*range(ntrain)] ]
test = data.loc[ [*range(ntrain,ndata)] ]
Xtrain = training[ training.columns[1:nfeat] ]
Ytrain = training[['label0']].values.ravel()
per = perceptron()
per.fit(Xtrain, Ytrain)
Xtest = test[ test.columns[1:nfeat] ]
Ytest = test[['label0']].values.ravel()
Ypred = per.predict(Xtest)
print( 'Accuracy = %3.1f%%' %\
( (Ytest == Ypred).sum() * 100 / len(Ytest) ) )