-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (45 loc) · 1.4 KB
/
main.py
File metadata and controls
74 lines (45 loc) · 1.4 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# coding: utf-8
# In[77]:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import numpy as np
from SVM import *
df = pd.read_csv('iris.csv')
sepalLength = df['SepalLengthCm'][:100].values
sepalWidth = df['SepalWidthCm'][:100].values
species = df['Species'][:100].values
feature = []
for i in range(100):
temp = []
temp = [sepalLength[i]] + [sepalWidth[i]]
feature.append(temp)
#Iris-setosa is 1 Iris-versicolor is -1
label = [1]*50 + [-1]*50
#shuffle
feature, label = shuffle(feature,label)
#split dataset into 90% for training and 10% validation
feature_train, feature_test, label_train, label_test = train_test_split(feature, label, test_size=0.3)
feature_train = np.array(feature_train)
label_train = np.array(label_train)
feature_test = np.array(feature_test)
label_test = np.array(label_test)
# In[208]:
a = 0.0001
epoch = 400
svm = SVM()
svm.train(feature_train, label_train, epoch, a, verbose = 1)
res,acc = svm.predict(feature_test,label_test)
print(acc)
left = max(sepalLength)
right = min(sepalLength)
w1 = svm.weight[0]
w2 = svm.weight[1]
xx = np.linspace(left, right)
yy = (-1*w1)*xx / w2
plt.scatter(sepalLength[:50],sepalWidth[:50],color='green')
plt.scatter(sepalLength[50:],sepalWidth[50:], color='red')
plt.plot(xx, yy)
plt.show()