Skip to content

Commit

Permalink
add fit_predict
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao062 committed Aug 5, 2019
1 parent 9712d60 commit 309b3a4
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 1 deletion.
1 change: 1 addition & 0 deletions combo/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def fit(self, X, y=None):
"""
pass

# todo: make sure fit then predict is equivalent to fit_predict
@abstractmethod
def fit_predict(self, X, y=None):
"""Fit estimator and predict on X. y is optional for unsupervised
Expand Down
19 changes: 19 additions & 0 deletions combo/models/classifier_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ def predict_proba(self, X):
Warning('average method is invoked for predict_proba as'
'probability is not continuous')
return np.mean(all_scores * self.weights, axis=2)

def fit_predict(self, X, y):
"""Fit estimator and predict on X
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X, y)
return self.predict(X)
19 changes: 19 additions & 0 deletions combo/models/classifier_dcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,22 @@ def _predict_internal(self, X, predict_proba):
test_sample)

return y_predicted

def fit_predict(self, X, y):
"""Fit estimator and predict on X
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X, y)
return self.predict(X)
19 changes: 19 additions & 0 deletions combo/models/classifier_des.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,22 @@ def _predict_internal(self, X, predict_proba):
return score_to_proba(y_predicted)
else:
return y_predicted

def fit_predict(self, X, y):
"""Fit estimator and predict on X
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X, y)
return self.predict(X)
19 changes: 19 additions & 0 deletions combo/models/classifier_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,22 @@ def predict_proba(self, X):
"""
X_new_comb = self._process_data(X)
return self.meta_clf.predict_proba(X_new_comb)

def fit_predict(self, X, y):
"""Fit estimator and predict on X
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X, y)
return self.predict(X)
20 changes: 20 additions & 0 deletions combo/models/cluster_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ def predict_proba(self, X):
raise NotImplemented("predict_proba function is currently disabled for"
"clustering due to inconsistent behaviours.")

def fit_predict(self, X, y=None):
"""Fit estimator and predict on X. y is optional for unsupervised
methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Cluster labels for each data sample.
"""
self.fit(X)
return self.labels_


def clusterer_ensemble_scores(original_labels, n_estimators, n_clusters,
weights=None, return_results=False,
Expand Down
20 changes: 20 additions & 0 deletions combo/models/detector_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,23 @@ def predict_proba(self, X, proba_method='linear'):
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
X = check_array(X)
return self._detector_predict_proba(X, proba_method)

def fit_predict(self, X, y=None):
"""Fit estimator and predict on X. y is optional for unsupervised
methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X)
return self.predict(X)
20 changes: 20 additions & 0 deletions combo/models/detector_lscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,23 @@ def predict_proba(self, X, proba_method='linear'):
check_is_fitted(self, ['decision_scores_', 'threshold_', 'labels_'])
X = check_array(X)
return self._detector_predict_proba(X, proba_method)

def fit_predict(self, X, y=None):
"""Fit estimator and predict on X. y is optional for unsupervised
methods.
Parameters
----------
X : numpy array of shape (n_samples, n_features)
The input samples.
y : numpy array of shape (n_samples,), optional (default=None)
The ground truth of the input samples (labels).
Returns
-------
labels : numpy array of shape (n_samples,)
Class labels for each data sample.
"""
self.fit(X)
return self.predict(X)
28 changes: 28 additions & 0 deletions combo/test/test_classifier_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@
from combo.utils.data import evaluate_print


class TestFitPredict(unittest.TestCase):
def setUp(self):
self.roc_floor = 0.9
self.accuracy_floor = 0.9

random_state = 42
X, y = load_breast_cancer(return_X_y=True)

self.X_train, self.X_test, self.y_train, self.y_test = \
train_test_split(X, y, test_size=0.4, random_state=random_state)

classifiers = [DecisionTreeClassifier(random_state=random_state),
LogisticRegression(random_state=random_state),
KNeighborsClassifier(),
RandomForestClassifier(random_state=random_state),
GradientBoostingClassifier(random_state=random_state)]

self.clf = SimpleClassifierAggregator(classifiers, method='average')

def test_fit_predict(self):
y_train_predicted = self.clf.fit_predict(self.X_train, self.y_train)
assert_equal(len(y_train_predicted), self.X_train.shape[0])

# check performance
assert_greater(accuracy_score(self.y_train, y_train_predicted),
self.accuracy_floor)


class TestAverage(unittest.TestCase):
def setUp(self):
self.roc_floor = 0.9
Expand Down
9 changes: 9 additions & 0 deletions combo/test/test_classifier_dcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def test_prediction_proba(self):
assert_allclose(np.ones([self.X_test.shape[0], ]),
y_test_predicted_sum)

def test_fit_predict(self):
y_train_predict = self.clf.fit_predict(self.X_train, self.y_train)

assert_equal(len(y_train_predict), self.X_train.shape[0])

# check performance
assert_greater(accuracy_score(self.y_train, y_train_predict),
self.accuracy_floor)

def tearDown(self):
pass

Expand Down
9 changes: 9 additions & 0 deletions combo/test/test_classifier_des.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def test_prediction_proba(self):
assert_allclose(np.ones([self.X_test.shape[0], ]),
y_test_predicted_sum)

def test_fit_predict(self):
y_train_predict = self.clf.fit_predict(self.X_train, self.y_train)

assert_equal(len(y_train_predict), self.X_train.shape[0])

# check performance
assert_greater(accuracy_score(self.y_train, y_train_predict),
self.accuracy_floor)

def tearDown(self):
pass

Expand Down
9 changes: 9 additions & 0 deletions combo/test/test_classifier_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def test_prediction_proba(self):
assert_allclose(np.ones([self.X_test.shape[0], ]),
y_test_predicted_sum)

def test_fit_predict(self):
y_train_predict = self.clf.fit_predict(self.X_train, self.y_train)

assert_equal(len(y_train_predict), self.X_train.shape[0])

# check performance
assert_greater(accuracy_score(self.y_train, y_train_predict),
self.accuracy_floor)

def tearDown(self):
pass

Expand Down
6 changes: 5 additions & 1 deletion combo/test/test_cluster_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def setUp(self):
self.X, self.y = load_breast_cancer(return_X_y=True)

n_clusters = 5
n_estimators = 3

# Initialize a set of estimators
estimators = [KMeans(n_clusters=n_clusters),
Expand All @@ -57,6 +56,11 @@ def test_scores(self):
predicted_labels = self.estimator.labels_
assert_equal(predicted_labels.shape[0], self.X.shape[0])

def test_fit_predict(self):
predicted_labels = self.estimator.fit_predict(self.X)
assert_equal(predicted_labels.shape[0], self.X.shape[0])


def tearDown(self):
pass

Expand Down
4 changes: 4 additions & 0 deletions combo/test/test_detector_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def test_prediction_proba_parameter(self):
with assert_raises(ValueError):
self.clf.predict_proba(self.X_test, proba_method='something')

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def tearDown(self):
pass

Expand Down
4 changes: 4 additions & 0 deletions combo/test/test_detector_lscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ def test_prediction_proba_parameter(self):
with assert_raises(ValueError):
self.clf.predict_proba(self.X_test, proba_method='something')

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def tearDown(self):
pass

Expand Down

0 comments on commit 309b3a4

Please sign in to comment.