-
Notifications
You must be signed in to change notification settings - Fork 857
/
_classifier.py
102 lines (85 loc) · 3.23 KB
/
_classifier.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Sebastian Raschka 2014-2019
# mlxtend Machine Learning Library Extensions
#
# Base Clusteer (Clutering Parent Class)
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
import numpy as np
from time import time
class _Classifier(object):
def __init__(self):
pass
def _check_target_array(self, y, allowed=None):
if not isinstance(y[0], (int, np.integer)):
raise AttributeError('y must be an integer array.\nFound %s'
% y.dtype)
found_labels = np.unique(y)
if (found_labels < 0).any():
raise AttributeError('y array must not contain negative labels.'
'\nFound %s' % found_labels)
if allowed is not None:
found_labels = tuple(found_labels)
if found_labels not in allowed:
raise AttributeError('Labels not in %s.\nFound %s'
% (allowed, found_labels))
def score(self, X, y):
""" Compute the prediction accuracy
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values (true class labels).
Returns
---------
acc : float
The prediction accuracy as a float
between 0.0 and 1.0 (perfect score).
"""
y_pred = self.predict(X)
acc = np.sum(y == y_pred, axis=0) / float(X.shape[0])
return acc
def fit(self, X, y, init_params=True):
"""Learn model from training data.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
init_params : bool (default: True)
Re-initializes model parameters prior to fitting.
Set False to continue training with weights from
a previous model fitting.
Returns
-------
self : object
"""
self._is_fitted = False
self._check_arrays(X=X, y=y)
self._check_target_array(y)
if hasattr(self, 'self.random_seed') and self.random_seed:
self._rgen = np.random.RandomState(self.random_seed)
self._init_time = time()
self._fit(X=X, y=y, init_params=init_params)
self._is_fitted = True
return self
def predict(self, X):
"""Predict targets from X.
Parameters
----------
X : {array-like, sparse matrix}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
Returns
----------
target_values : array-like, shape = [n_samples]
Predicted target values.
"""
self._check_arrays(X=X)
if not self._is_fitted:
raise AttributeError('Model is not fitted, yet.')
return self._predict(X)