-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
102 lines (76 loc) · 3.56 KB
/
test.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
95
96
97
98
99
100
101
102
import pandas as pd
import numpy as np
import knn
from BaysianClassifier import BaysianClassifier
data = pd.read_csv("segmentation_test.csv")
test = data
df = data.set_index('LABEL')
d = len(df.columns)
def compare(a,b):
if(a == b): return 1
return 0
comp = np.vectorize(compare)
def test(df):
ammount = df.iloc[:,0].groupby('LABEL').count()
ammount.columns = ['count']
result = pd.DataFrame(columns=['knn_shape','knn_color','baysian_shape','baysian_color','majority'])
index = 0
for t in range(30):
print("test:"+str(t))
df_shape = df.iloc[:, 0:9]
df_color = df.iloc[:, 9:20]
#print (df_shape)
#print (df_color)
group_shape = {}
group_color = {}
for i in ammount.index:
group_shape[i] = np.split(df_shape.loc[i], 10)
group_color[i] = np.split(df_color.loc[i], 10)
for j in range(10):
train_shape = pd.DataFrame(columns = df_shape.columns)
test_shape = pd.DataFrame(columns = df_shape.columns)
train_color = pd.DataFrame(columns = df_color.columns)
test_color = pd.DataFrame(columns = df_color.columns)
print("j:" +str(j))
for i in ammount.index:
temp = group_shape[i].pop(0)
test_shape = pd.concat([test_shape, temp])
train_shape = pd.concat([train_shape] + group_shape[i])
group_shape[i].append(temp)
temp = group_color[i].pop(0)
test_color = pd.concat([test_color, temp])
train_color = pd.concat([train_color] + group_color[i])
group_color[i].append(temp)
test_shape.index.name = "LABEL"
test_color.index.name = "LABEL"
train_shape.index.name = "LABEL"
train_color.index.name = "LABEL"
#print(test_shape.columns)
#print(test_shape.index.name)
#print(test_color.columns)
#print(test_color.index.name)
#print("shalala")
knn_shape_ans = knn.knn_lables(test_shape, train_shape, 3)
knn_color_ans = knn.knn_lables(test_color, train_color, 3)
baysian_shape_ans = BaysianClassifier(train_shape).test_df_label(test_shape)
baysian_color_ans = BaysianClassifier(train_color).test_df_label(test_color)
majority = []
for i in range(len(knn_shape_ans)):
ans = []
ans.append(knn_shape_ans[i])
ans.append(knn_color_ans[i])
ans.append(baysian_shape_ans[i])
ans.append(baysian_color_ans[i])
x = pd.DataFrame({'label':ans,'count':[1,1,1,1]})
#print(x)
majority += [x.groupby('label').count().nlargest(1,'count').index[0]]
knn_shape_correct = knn.knn_dataframes(test_shape, train_shape, 3)
knn_color_correct = knn.knn_dataframes(test_color, train_color, 3)
baysian_shape_correct = BaysianClassifier(train_shape).test_df(test_shape)
baysian_color_correct = BaysianClassifier(train_color).test_df(test_color)
majority_correct = sum(comp(majority , test_shape.index))
result.loc[index] = [knn_shape_correct, knn_color_correct, baysian_shape_correct, baysian_color_correct, majority_correct]
index += 1;
result.to_csv("part_2_results.csv")
df = df.sample(frac=1)
test(df)