-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (47 loc) · 1.42 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
47
48
49
50
import pandas as pd
import createdata
import numpy as np
import bp
import random
import matplotlib.pyplot as plt
if __name__ == '__main__':
path = 'D:\homework\picture\picall'
createdata.createcsv(path)
f = open('labels.txt', 'r')
dataset = f.readlines()
f.close()
for i in range(len(dataset)):
dataset[i] = dataset[i].rstrip().split(',')
for j in range(5):
dataset[i][j] = float(dataset[i][j])
train_dataset = dataset[:210]
test_dataset = dataset[210:]
x_train = []
y_train = []
for data in train_dataset:
x_train.append(data[:-1])
if data[5][:1] == 'g':
y_train.append([1, 0, 0])
if data[5][:1] == 'n':
y_train.append([0, 1, 0])
if data[5][:1] == 'r':
y_train.append([0, 0, 1])
x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = []
y_test = []
for data in train_dataset:
x_test.append(data[:-1])
if data[5][:1] == 'g':
y_test.append([1, 0, 0])
if data[5][:1] == 'n':
y_test.append([0, 1, 0])
if data[5][:1] == 'r':
y_test.append([0, 0, 1])
x_test = np.array(x_test)
y_test = np.array(y_test)
net = bp.NNetWork([5, 19, 3])
net.train(x_train, y_train)
acc = bp.ComputeAccuracy(net, x_test, y_test)
print('Accuracy is:', acc)
plt.show()