-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.py
91 lines (70 loc) · 2.17 KB
/
cv.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 25 18:12:00 2016
@author: Sandy Wong
Cross Validation for Project 3 COMP 551
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, datasets
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn import svm
import load
from sklearn.model_selection import KFold
import pickle
import csv
cNames=[]
def printscore(array):
for i in range(len(array)):
print ("\n", cNames[i],"\n")
for j in range(len(array[i])):
print (array[i][j]," ")
#import data TEMP
iris = datasets.load_iris()
data = load.load("X.pkl")
#X = iris.data[:, :2] # we only take the first two features.
#Y = iris.target
Y = []
with open('train_y.csv', newline='') as f_in:
csvreader = csv.reader(f_in)
for row in csvreader:
Y.append(row[1])
X = np.matrix(data)
y = np.matrix(Y)
y = y.transpose()
print ("X: ",X.shape)
print ("y: ", y.shape)
#classifiers
logreg = linear_model.LogisticRegression(C=1e5)
C=1
svc = svm.SVC(kernel='linear', C=C)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C)
poly_svc = svm.SVC(kernel='poly', degree=3, C=C)
lin_svc = svm.LinearSVC(C=C)
#do classification
kf = KFold(n_splits=3)
classifiers = [logreg,svc,rbf_svc,poly_svc,lin_svc]
cNames = ["Logistic Regression", "SVC", "Rbf SVC", "Polynomial SVC", "Linear SVC"]
scores = [[],[],[],[],[]]
scores2 = []
'''
for train_index, test_index in kf:
#print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
for i in range(len(classifiers)):
clf = classifiers[i]
clf.fit(X_train,y_train)
name = cNames[i]
score = clf.score(X_test, y_test)
scores[i].append(score)
'''
count = 0
for clf in classifiers:
print ("cross validating...\n")
score = cross_val_score(clf, X, y, cv=kf, n_jobs=-1)
count = count+1
print("cross validation number: ", count)
scores2.append(score)
printscore(scores2)