Implement predict and score methods & tests

This commit is contained in:
2020-05-13 12:42:09 +02:00
parent c4de782a3f
commit 8f71eeb316
6 changed files with 70 additions and 221 deletions

View File

@@ -2,7 +2,7 @@
__author__ = "Ricardo Montañana Gómez"
__copyright__ = "Copyright 2020, Ricardo Montañana Gómez"
__license__ = "MIT"
__version__ = "1.0"
__version__ = "0.9"
Node of the Stree (binary tree)
'''
@@ -11,10 +11,10 @@ from sklearn.svm import LinearSVC
class Snode:
def __init__(self, model: LinearSVC, X: np.ndarray, y: np.ndarray, title: str):
self._model = model
self._vector = None if model is None else model.coef_
self._interceptor = 0 if model is None else model.intercept_
def __init__(self, clf: LinearSVC, X: np.ndarray, y: np.ndarray, title: str):
self._clf = clf
self._vector = None if clf is None else clf.coef_
self._interceptor = 0 if clf is None else clf.intercept_
self._title = title
self._belief = 0 # belief of the prediction in a leaf node based on samples
self._X = X
@@ -60,6 +60,6 @@ class Snode:
num = max(num, self._y[self._y == i].shape[0])
den = self._y.shape[0]
accuracy = num / den if den != 0 else 1
return f"{self._title} LEAF accuracy={accuracy:.2f}\n"
return f"{self._title} LEAF accuracy={accuracy:.2f}, belief={self._belief:.2f} class={self._class}\n"
else:
return f"{self._title}\n"

View File

@@ -2,8 +2,8 @@
__author__ = "Ricardo Montañana Gómez"
__copyright__ = "Copyright 2020, Ricardo Montañana Gómez"
__license__ = "MIT"
__version__ = "1.0"
Create a oblique tree classifier based on SVM Trees
__version__ = "0.9"
Build an oblique tree classifier based on SVM Trees
Uses LinearSVC
'''
@@ -25,6 +25,7 @@ class Stree:
self._tree = None
self.__folder = 'data/'
self.__use_predictions = use_predictions
self.__trained = False
def _split_data(self, clf: LinearSVC, X: np.ndarray, y: np.ndarray) -> list:
if self.__use_predictions:
@@ -46,10 +47,11 @@ class Stree:
def fit(self, X: np.ndarray, y: np.ndarray, title: str = 'root') -> 'Stree':
self._tree = self.train(X, y, title)
self._predictor()
self._build_predictor()
self.__trained = True
return self
def _predictor(self):
def _build_predictor(self):
"""Process the leaves to make them predictors
"""
def run_tree(node: Snode):
@@ -79,6 +81,28 @@ class Stree:
str(np.unique(y_d, return_counts=True))))
return tree
def predict(self, X: np.array) -> np.array:
def predict_class(xp: np.array, tree: Snode) -> np.array:
if tree.is_leaf():
return tree._class
coef = tree._vector[0, :].reshape(-1, xp.shape[1])
if xp.dot(coef.T) + tree._interceptor[0] > 0:
return predict_class(xp, tree.get_down())
return predict_class(xp, tree.get_up())
y = np.array([], dtype=int)
for xp in X:
y = np.append(y, predict_class(xp.reshape(-1, X.shape[1]), self._tree))
return y
def score(self, X: np.array, y: np.array, print_out=True) -> float:
self.fit(X, y)
yp = self.predict(X)
right = (yp == y).astype(int)
accuracy = sum(right) / len(y)
if print_out:
print(f"Accuracy: {accuracy:.6f}")
return accuracy
def __str__(self):
def print_tree(tree: Snode) -> str:
output = str(tree)