-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPCA&SVM.py
52 lines (45 loc) · 1.54 KB
/
PCA&SVM.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
import numpy
from sklearn.decomposition import PCA
from sklearn.svm import SVC
COMPONENT_NUM = 2
print('Read training data...')
with open('Dataset/temphumidity.csv', 'r') as reader:
reader.readline()
train_label = []
train_data = []
for line in reader.readlines():
data = list(map(float, line.rstrip().split(',')))
train_label.append(data[0])
train_data.append(data[1:])
print('Loaded ' + str(len(train_label)))
print('Reduction...')
train_label = numpy.array(train_label)
train_data = numpy.array(train_data)
pca = PCA(n_components=COMPONENT_NUM, whiten=True)
pca.fit(train_data)
train_data = pca.transform(train_data)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(train_data,train_label,test_size=0.2,random_state=32)
print('Train SVM...')
svc = SVC(C=10.0,kernel="rbf",gamma=0.1)
svc.fit(X_train, y_train)
#print('Read testing data...')
#with open('../input/test.csv', 'r') as reader:
# reader.readline()
# test_data = []
# for line in reader.readlines():
# pixels = list(map(int, line.rstrip().split(',')))
# test_data.append(pixels)
#print('Loaded ' + str(len(test_data)))
print('Predicting...')
from sklearn.metrics import accuracy_score
predict = svc.predict(X_test)
acc=accuracy_score(predict,y_test)
print ("Accuracy % : ",acc)
#print('Saving...')
#with open('predict.csv', 'w') as writer:
# writer.write('"ImageId","Label"\n')
# count = 0
# for p in predict:
# count += 1
# writer.write(str(count) + ',"' + str(p) + '"\n')#