forked from rushter/MLAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.py
33 lines (25 loc) · 1.02 KB
/
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
import logging
try:
from sklearn.model_selection import train_test_split
except ImportError:
from sklearn.cross_validation import train_test_split
from sklearn.datasets import make_classification
from mla.metrics.metrics import accuracy
from mla.svm.kernerls import Linear, RBF
from mla.svm.svm import SVM
logging.basicConfig(level=logging.DEBUG)
def classification():
# Generate a random binary classification problem.
X, y = make_classification(
n_samples=1200, n_features=10, n_informative=5, random_state=1111, n_classes=2, class_sep=1.75
)
# Convert y to {-1, 1}
y = (y * 2) - 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1111)
for kernel in [RBF(gamma=0.1), Linear()]:
model = SVM(max_iter=500, kernel=kernel, C=0.6)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Classification accuracy (%s): %s" % (kernel, accuracy(y_test, predictions)))
if __name__ == "__main__":
classification()