-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cat4.py
152 lines (127 loc) · 3.95 KB
/
test_cat4.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#python 3 implementation
import random
import csv
split = 0.80
#with open('cat3.csv') as csvfile:
#lines = csv.reader(csvfile)
#dataset = list(lines)
import pandas as pd
data=pd.read_csv('cat4.csv')
data=data.drop(['id','galex_objid','sdss_objid','spectrometric_redshift','pred'],axis=1)
from sklearn.utils import resample
class0= data[data['class']==0]#minority
class1= data[data['class']==1]#majority
class0_upsampled = resample(class0,
replace=True, # sample with replacement
n_samples=len(class1), # match number in majority class
random_state=123) # reproducible results
upsampled = pd.concat([class1, class0_upsampled])
#data.head()
dataset=upsampled.values.tolist()
random.shuffle(dataset)
div = int(split * len(dataset))
train = dataset [:div]
test = dataset [div:]
#print(train[0:5])
#print(test[0:5])
import math
# square root of the sum of the squared differences between the two arrays of numbers
def euclideanDistance(instance1, instance2, length):
distance = 0
for x in range(length):
#print(instance1[x])
#print(instance2[x])
if(x!=11):
#print(instance1[x])
#print(instance2[x])
distance += pow((float(instance1[x]) - float(instance2[x])), 2)
return math.sqrt(distance)
import operator
#distances = []
def getNeighbors(trainingSet, testInstance, k):
distances = []
length = len(testInstance)
for x in range(len(trainingSet)):
dist = euclideanDistance(testInstance, trainingSet[x], length)
distances.append((trainingSet[x], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
def getResponse(neighbors):
classVotes = {}
classVotes['0']=0
classVotes['1']=0
for x in range(len(neighbors)):
response = neighbors[x][11]
#print(response)
if response == 0.0:
classVotes['0'] += 1
else:
classVotes['1'] += 1
sortedVotes = sorted(classVotes.items(), key=operator.itemgetter(1), reverse=True)
#print(sortedVotes)
return float(sortedVotes[0][0])
def confusion_matrix(act,pred):
tp = 0
fp = 0
tn = 0
fn = 0
l0 = 0
l1 = 0
for i in range(len(act)):
if(act[i]==0):
l0 += 1
elif(act[i]==1):
l1 += 1
if(act[i]==1 and pred[i]==1):
tp += 1
elif(act[i]==1 and pred[i]==0):
fn += 1
elif(act[i]==0 and pred[i]==1):
fp += 1
elif(act[i]==0 and pred[i]==0):
tn += 1
print("class 1 accuracy:",(tp/l1))
print("class 0 accuracy:",(tn/l0))
accuracy = (tp+tn)/(tp+tn+fp+fn)
recall = (tp)/(tp+fn)
precision = (tp) / (tp+fp)
a=2*(recall)*(precision)
b=(recall+precision)
f_score = a / b
error_rate = 1-accuracy
print("accuracy:" + repr(accuracy))
print("recall:" + repr(recall))
print("precision:" + repr(precision))
print("f-score:" + repr(f_score))
print("error rate:" + repr(error_rate))
def getAccuracy(testSet, predictions):
correct = 0
for x in range(len(testSet)):
#print(predictions[x])
#print(testSet[x][14])
if testSet[x][11] == float(predictions[x]):
correct += 1
return (correct/float(len(testSet))) * 100.0
predictions=[]
k = 7
for x in range(len(test)):
#print(len(test[x]))
neighbors = getNeighbors(train, test[x], k)
#print("N",neighbors)
result = getResponse(neighbors)
#print("R",result)
predictions.append(result)
#print(predictions)
#print('> predicted=' + repr(result) + ', actual=' + repr(test[x][11]))
accuracy = getAccuracy(test, predictions)
print('Accuracy: ' + repr(accuracy) + '%')
actual = []
for i in range(len(test)):
actual.append(test[i][11])
results = confusion_matrix(actual, predictions)