2020-10-05 03:19:18 +00:00
|
|
|
# Titan Robotics Team 2022: SVM submodule
|
|
|
|
# Written by Arthur Lu
|
|
|
|
# Notes:
|
|
|
|
# this should be imported as a python module using 'from tra_analysis import SVM'
|
|
|
|
# setup:
|
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
__version__ = "1.0.1"
|
2020-10-05 03:19:18 +00:00
|
|
|
|
|
|
|
__changelog__ = """changelog:
|
2021-01-27 03:46:29 +00:00
|
|
|
1.0.1:
|
|
|
|
- removed unessasary self calls
|
|
|
|
- removed classness
|
2020-10-05 03:19:18 +00:00
|
|
|
1.0.0:
|
|
|
|
- ported analysis.SVM() here
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = (
|
|
|
|
"Arthur Lu <learthurgo@gmail.com>",
|
|
|
|
)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
]
|
|
|
|
|
|
|
|
import sklearn
|
|
|
|
from sklearn import svm
|
|
|
|
from . import ClassificationMetric, RegressionMetric
|
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class CustomKernel:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls, C, kernel, degre, gamma, coef0, shrinking, probability, tol, cache_size, class_weight, verbose, max_iter, decision_function_shape, random_state):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(C = C, kernel = kernel, gamma = gamma, coef0 = coef0, shrinking = shrinking, probability = probability, tol = tol, cache_size = cache_size, class_weight = class_weight, verbose = verbose, max_iter = max_iter, decision_function_shape = decision_function_shape, random_state = random_state)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class StandardKernel:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls, kernel, C=1.0, degree=3, gamma='auto_deprecated', coef0=0.0, shrinking=True, probability=False, tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, decision_function_shape='ovr', random_state=None):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(C = C, kernel = kernel, gamma = gamma, coef0 = coef0, shrinking = shrinking, probability = probability, tol = tol, cache_size = cache_size, class_weight = class_weight, verbose = verbose, max_iter = max_iter, decision_function_shape = decision_function_shape, random_state = random_state)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class PrebuiltKernel:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class Linear:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(kernel = 'linear')
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class Polynomial:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls, power, r_bias):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(kernel = 'polynomial', degree = power, coef0 = r_bias)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class RBF:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls, gamma):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(kernel = 'rbf', gamma = gamma)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
class Sigmoid:
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def __new__(cls, r_bias):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return sklearn.svm.SVC(kernel = 'sigmoid', coef0 = r_bias)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def fit(kernel, train_data, train_outputs): # expects *2d data, 1d labels or outputs
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return kernel.fit(train_data, train_outputs)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def eval_classification(kernel, test_data, test_outputs):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
predictions = kernel.predict(test_data)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return ClassificationMetric(predictions, test_outputs)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
def eval_regression(kernel, test_data, test_outputs):
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
predictions = kernel.predict(test_data)
|
2020-10-05 03:19:18 +00:00
|
|
|
|
2021-01-27 03:46:29 +00:00
|
|
|
return RegressionMetric(predictions, test_outputs)
|