-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise02.py
More file actions
55 lines (43 loc) · 1.48 KB
/
Exercise02.py
File metadata and controls
55 lines (43 loc) · 1.48 KB
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
# looking at data
from sklearn import datasets
wine = datasets.load_wine()
print("Number of entries: ", len(wine.data))
for featurename in wine.feature_names:
print(featurename[:10], " \t", end="")
print("Class")
for feature, label in zip(wine.data, wine.target):
for f in feature:
print(f, "\t\t",end="")
print(label)
##################################################
# split data into train and test
from sklearn.model_selection import train_test_split as tts
feats = wine.data
labels = wine.target
train_feats, test_feats, train_labels, test_labels = tts(feats, labels, test_size=0.2)
print("Number of entries: ", len(train_feats))
for featurename in wine.feature_names:
print(featurename[:10], " \t",end="")
print("Class")
for feature, label in zip(train_feats,train_labels):
for f in feature:
print(f, "\t\t",end="")
print(label)
##################################################
# svm classifier
from sklearn import svm
clf = svm.SVC(kernel='linear')
clf.fit(train_feats, train_labels)
predictions = clf.predict(test_feats)
print(predictions)
score = sum(predictions == test_labels)
print("SVM: ", score / len(predictions))
###################################################
# tree classifier
from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=3,max_leaf_nodes=3)
clf.fit(train_feats, train_labels)
predictions = clf.predict(test_feats)
print(predictions)
score = sum(predictions == test_labels)
print("Tree: ", score / len(predictions))