Skip to content

Commit

Permalink
Merge pull request scikit-multiflow#75 from PGijsbers/fix_40
Browse files Browse the repository at this point in the history
Set numpy.ndarray as return type in predict() and predict_proba() methods.
- Fixes 40
  • Loading branch information
jacobmontiel authored Oct 28, 2018
2 parents 2af4d35 + d727a17 commit 5fbefd6
Show file tree
Hide file tree
Showing 25 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/skmultiflow/lazy/sam_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def predict(self, X):
for i in range(r):
distancesSTM = SAMKNN.get_distances(X[i], self._STMSamples)
predictedLabel.append(self.predictFct(X[i], None, distancesSTM))
return predictedLabel
return np.asarray(predictedLabel)

def predict_proba(self, X):
raise NotImplementedError
Expand Down
5 changes: 3 additions & 2 deletions src/skmultiflow/meta/adaptive_random_forests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from copy import deepcopy
import numpy as np
from sklearn.preprocessing import normalize

from skmultiflow.core.base_object import BaseObject
Expand Down Expand Up @@ -230,7 +231,7 @@ def predict(self, X):
Samples for which we want to predict the labels.
Returns
-------
list
numpy.ndarray
Predicted labels for all instances in X.
"""
r, _ = get_dimensions(X)
Expand All @@ -242,7 +243,7 @@ def predict(self, X):
predictions.append(0)
else:
predictions.append(max(votes, key=votes.get))
return predictions
return np.asarray(predictions)

def predict_proba(self, X):
""" Predicts the class probabilities for the X instance(s).
Expand Down
8 changes: 4 additions & 4 deletions src/skmultiflow/meta/leverage_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def predict(self, X):
Returns
-------
list
A list with the label prediction for all the samples in X.
numpy.ndarray
A numpy.ndarray with the label prediction for all the samples in X.
"""
r, c = get_dimensions(X)
Expand All @@ -328,7 +328,7 @@ def predict(self, X):
proba = np.zeros((r, 1))
for i in range(r):
predictions.append(np.argmax(proba[i]))
return predictions
return np.asarray(predictions)

def predict_proba(self, X):
""" predict_proba
Expand Down Expand Up @@ -392,7 +392,7 @@ def predict_proba(self, X):
aux.append([x / sum_proba[i] for x in proba[i]])
else:
aux.append(proba[i])
return aux
return np.asarray(aux)

def predict_binary_proba(self, X):
""" predict_binary_proba
Expand Down
8 changes: 4 additions & 4 deletions src/skmultiflow/meta/oza_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def predict(self, X):
Returns
-------
list
A list with the label prediction for all the samples in X.
numpy.ndarray
A numpy.ndarray with the label prediction for all the samples in X.
"""
r, c = get_dimensions(X)
Expand All @@ -207,7 +207,7 @@ def predict(self, X):
return None
for i in range(r):
predictions.append(np.argmax(proba[i]))
return predictions
return np.asarray(predictions)

def predict_proba(self, X):
""" predict_proba
Expand Down Expand Up @@ -267,7 +267,7 @@ def predict_proba(self, X):
aux.append([x / sum_proba[i] for x in proba[i]])
else:
aux.append(proba[i])
return aux
return np.asarray(aux)

def score(self, X, y):
raise NotImplementedError
Expand Down
7 changes: 4 additions & 3 deletions src/skmultiflow/neural_networks/perceptron.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from skmultiflow.core.base import StreamModel
from sklearn.linear_model.perceptron import Perceptron

Expand Down Expand Up @@ -107,11 +108,11 @@ def predict(self, X):
Returns
-------
list
A list containing the predicted labels for all instances in X.
numpy.ndarray
A numpy.ndarray containing the predicted labels for all instances in X.
"""
return self.classifier.predict(X)
return np.asarray(self.classifier.predict(X))

def predict_proba(self, X):
""" predict_proba
Expand Down
4 changes: 2 additions & 2 deletions src/skmultiflow/trees/regression_hoeffding_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def predict(self, X):
Returns
-------
list
numpy.ndarray
Predicted target values.
"""
Expand Down Expand Up @@ -584,7 +584,7 @@ def predict(self, X):
else:
# Model is empty
predictions.append(0.0)
return predictions
return np.asarray(predictions)

def predict_proba(self, X):
pass
Expand Down
5 changes: 4 additions & 1 deletion tests/bayes/test_naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ def test_naive_bayes(test_path):
expected_score = 0.751503006012024
assert np.isclose(expected_score, learner.score(X=X_batch[4501:], y=y_batch[4501:]))

assert 'estimator' == learner.get_class_type()
assert 'estimator' == learner.get_class_type()

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
3 changes: 3 additions & 0 deletions tests/lazy/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ def test_knn():
correct_predictions = sum(predictions == y_batch[4501:4550])
expected_correct_predictions = 49
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
3 changes: 3 additions & 0 deletions tests/lazy/test_knn_adwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ def test_knn_adwin():
correct_predictions = sum(np.array(predictions) == y[951:])
expected_correct_predictions = 47
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
3 changes: 3 additions & 0 deletions tests/lazy/test_sam_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_sam_knn(package_path):

assert np.alltrue(predictions == expected_predictions)

assert type(learner.predict(X)) == np.ndarray
# assert type(learner.predict_proba(X)) == np.ndarray predict_proba not implemented.


def test_sam_knn_coverage(package_path):

Expand Down
4 changes: 4 additions & 0 deletions tests/meta/test_adaptive_random_forests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def test_adaptive_random_forests():
# Temporary disable as pre-3.6 give different predictions than 3.6+
assert np.alltrue(predictions == last_version_predictions)

assert type(learner.predict(X)) == np.ndarray


def test_adaptive_random_forests_labels_given():
stream = RandomTreeGenerator(tree_random_state=112, sample_random_state=112, n_classes=2)
Expand Down Expand Up @@ -128,3 +130,5 @@ def test_adaptive_random_forests_batch_predict_proba():
if sys.version_info.major == 3 and sys.version_info.minor >= 6:
# Temporary disable as pre-3.6 give different predictions than 3.6+
assert np.alltrue(all_predictions.argmax(axis=1) == last_version_predictions)

assert type(learner.predict_proba(X)) == np.ndarray
2 changes: 2 additions & 0 deletions tests/meta/test_batch_incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ def test_batch_incremental():
assert np.alltrue(predictions == expected_predictions)
assert np.isclose(expected_performance, performance)
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
3 changes: 3 additions & 0 deletions tests/meta/test_classifier_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def test_classifier_chains():
assert np.alltrue(np.array_equal(predictions, expected_predictions))
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
# assert type(learner.predict_proba(X)) == np.ndarray Not available because default loss is set to 'hinge'


def test_classifier_chains_all():
seed = 1
Expand Down
3 changes: 3 additions & 0 deletions tests/meta/test_leverage_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ def test_leverage_bagging():
assert np.alltrue(predictions == expected_predictions)
assert np.isclose(expected_performance, performance)
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
3 changes: 2 additions & 1 deletion tests/meta/test_multi_output_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ def test_multi_output_learner():
assert np.isclose(expected_performance, perf)
assert correct_predictions == expected_correct_predictions


assert type(classifier.predict(X)) == np.ndarray
assert type(classifier.predict_proba(X)) == np.ndarray
2 changes: 2 additions & 0 deletions tests/meta/test_oza_bagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ def test_oza_bagging():
assert np.isclose(expected_performance, performance)
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
2 changes: 2 additions & 0 deletions tests/meta/test_oza_bagging_adwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ def test_oza_bagging_adwin():
assert np.isclose(expected_performance, performance)
assert correct_predictions == expected_correct_predictions

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
3 changes: 2 additions & 1 deletion tests/meta/test_regressor_chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def test_regressor_chains():
[-113.86249490223707, 2634310697909.643, 1.580428629322546e+23],
[-35.92856878407447, -5410985463428.589, 2.522168862637753e+23]]


print(predictions)
assert np.allclose(np.array(predictions).all(), np.array(expected_predictions).all())
assert type(learner.predict(X)) == np.ndarray


test_regressor_chains()
3 changes: 3 additions & 0 deletions tests/neural_networks/test_perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ def test_perceptron(test_path):
# assert np.isclose(expected_accuracy, accuracy) # Removed due to npn-replicable error in Travis build

assert 'estimator' == learner.get_class_type()

assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
7 changes: 7 additions & 0 deletions tests/trees/test_hoeffding_adaptive_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def test_hat_mc(test_path):
or (learner.get_model_description() == expected_model_4) \


assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray

stream.restart()
X, y = stream.next_sample(5000)

Expand Down Expand Up @@ -108,6 +111,8 @@ def test_hat_nb(test_path):
' - nominal_attributes: [] - '

assert learner.get_info() == expected_info
assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray


def test_hat_nba(test_path):
Expand Down Expand Up @@ -150,3 +155,5 @@ def test_hat_nba(test_path):
' - nominal_attributes: [] - '

assert learner.get_info() == expected_info
assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
2 changes: 2 additions & 0 deletions tests/trees/test_hoeffding_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def test_hoeffding_tree(test_path):
expected_model_2 = 'Leaf = Class 1.0 | {1.0: 1745.0, 2.0: 978.0, 0.0: 1423.0, 3.0: 854.0}\n'
assert (learner.get_model_description() == expected_model_1) \
or (learner.get_model_description() == expected_model_2)
assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray


def test_hoeffding_tree_coverage():
Expand Down
5 changes: 3 additions & 2 deletions tests/trees/test_lc_hoeffding_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
from skmultiflow.trees import LCHT
from skmultiflow.data import MultilabelGenerator
import os

def test_lc_hoeffding_tree(test_path):
stream = MultilabelGenerator(n_samples=10000, n_features=15, n_targets=3, n_labels=4, random_state=112)
Expand Down Expand Up @@ -34,4 +33,6 @@ def test_lc_hoeffding_tree(test_path):
[1, 0, 1], [0, 1, 1], [1, 1, 1], [1, 1, 1], [0, 1, 0], [0, 1, 0], [1, 1, 1], [1, 1, 1],
[1, 1, 1]]

assert np.alltrue(predictions == expected_predictions)
assert np.alltrue(predictions == expected_predictions)
assert type(learner.predict(X)) == np.ndarray
assert type(learner.predict_proba(X)) == np.ndarray
1 change: 1 addition & 0 deletions tests/trees/test_multi_target_regression_hoeffding_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_multi_target_regression_hoeffding_tree_mean(test_path):
'nominal_attributes: [] - '
assert learner.get_info() == expected_info
assert isinstance(learner.get_model_description(), type(''))
assert type(learner.predict(X)) == np.ndarray


def test_multi_target_regression_hoeffding_tree_perceptron(test_path):
Expand Down
2 changes: 2 additions & 0 deletions tests/trees/test_regression_hoeffding_adaptive_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_hoeffding_tree():
assert learner.get_info() == expected_info

assert isinstance(learner.get_model_description(), type(''))
assert type(learner.predict(X)) == np.ndarray


def test_hoeffding_tree_perceptron():
Expand Down Expand Up @@ -103,3 +104,4 @@ def test_hoeffding_tree_perceptron():
assert learner.get_info() == expected_info

assert isinstance(learner.get_model_description(), type(''))
assert type(learner.predict(X)) == np.ndarray
2 changes: 2 additions & 0 deletions tests/trees/test_regression_hoeffding_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_hoeffding_tree():
assert learner.get_info() == expected_info

assert isinstance(learner.get_model_description(), type(''))
assert type(learner.predict(X)) == np.ndarray


def test_hoeffding_tree_perceptron():
Expand Down Expand Up @@ -104,6 +105,7 @@ def test_hoeffding_tree_perceptron():
assert learner.get_info() == expected_info

assert isinstance(learner.get_model_description(), type(''))
assert type(learner.predict(X)) == np.ndarray


def test_hoeffding_tree_coverage(test_path):
Expand Down

0 comments on commit 5fbefd6

Please sign in to comment.