<commit_before>"""
Testing for the forest module (sklearn.ensemble.forest).
"""

import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_almost_equal
from nose.tools import assert_raises

from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import ExtraTreesRegressor
from sklearn import datasets

# toy sample
X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
y = [-1, -1, -1, 1, 1, 1]
T = [[-1, -1], [2, 2], [3, 2]]
true_result = [-1, 1, 1]

# also load the iris dataset
# and randomly permute it
iris = datasets.load_iris()
np.random.seed([1])
perm = np.random.permutation(iris.target.size)
iris.data = iris.data[perm]
iris.target = iris.target[perm]

# also load the boston dataset
# and randomly permute it
boston = datasets.load_boston()
perm = np.random.permutation(boston.target.size)
boston.data = boston.data[perm]
boston.target = boston.target[perm]

def test_classification_toy_rf():
    """Check classification on a toy dataset (random forest)."""
    clf = RandomForestClassifier(n_trees=10, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    clf = RandomForestClassifier(n_trees=10, max_features=1, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

def test_classification_toy_et():
    """Check classification on a toy dataset (extra-trees)."""
    clf = ExtraTreesClassifier(n_trees=10, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    clf = ExtraTreesClassifier(n_trees=10, max_features=1, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

def test_iris_rf():
    """Check consistency on dataset iris (random forest)."""
    for c in ("gini", "entropy"):
        clf = RandomForestClassifier(n_trees=10, criterion=c, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

        clf = RandomForestClassifier(n_trees=10, criterion=c, max_features=2, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.5, "Failed with criterion %s and score = %f" % (c, score)

def test_iris_et():
    """Check consistency on dataset iris (extra-trees)."""
    for c in ("gini", "entropy"):
        clf = ExtraTreesClassifier(n_trees=10, criterion=c, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

        clf = ExtraTreesClassifier(n_trees=10, criterion=c, max_features=2, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

def test_boston_rf():
    """Check consistency on dataset boston house prices (random forest)."""
    for c in ("mse",):
        clf = RandomForestRegressor(n_trees=10, criterion=c, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

        clf = RandomForestRegressor(n_trees=10, criterion=c, max_features=6, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

def test_boston_et():
    """Check consistency on dataset boston house prices (extra-trees)."""
    for c in ("mse",):
        clf = ExtraTreesRegressor(n_trees=10, criterion=c, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

        clf = ExtraTreesRegressor(n_trees=10, criterion=c, max_features=6, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

def test_probability_rf():
    """Predict probabilities (random forest)."""
    clf = RandomForestClassifier(n_trees=10, random_state=1)
    clf.fit(iris.data, iris.target)
    assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))

def test_probability_et():
    """Predict probabilities (extra-trees)."""
    clf = ExtraTreesClassifier(n_trees=10, random_state=1)
    clf.fit(iris.data, iris.target)
    assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))

def test_error_rf():
    """Check that proper errors are triggered (random forest)."""
    assert_raises(ValueError, RandomForestClassifier(n_trees=-1).fit, X, y)
    assert_raises(ValueError, RandomForestRegressor(n_trees=-1).fit, X, y)

def test_error_et():
    """Check that proper errors are triggered (extra-trees)."""
    assert_raises(ValueError, ExtraTreesClassifier(n_trees=-1).fit, X, y)
    assert_raises(ValueError, ExtraTreesRegressor(n_trees=-1).fit, X, y)<commit_msg>Added some more tests<commit_after>"""
Testing for the forest module (sklearn.ensemble.forest).
"""

import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_array_almost_equal
from numpy.testing import assert_almost_equal
from numpy.testing import assert_equal
from nose.tools import assert_raises

from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.ensemble import ExtraTreesRegressor
from sklearn import datasets

# toy sample
X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]
y = [-1, -1, -1, 1, 1, 1]
T = [[-1, -1], [2, 2], [3, 2]]
true_result = [-1, 1, 1]

# also load the iris dataset
# and randomly permute it
iris = datasets.load_iris()
np.random.seed([1])
perm = np.random.permutation(iris.target.size)
iris.data = iris.data[perm]
iris.target = iris.target[perm]

# also load the boston dataset
# and randomly permute it
boston = datasets.load_boston()
perm = np.random.permutation(boston.target.size)
boston.data = boston.data[perm]
boston.target = boston.target[perm]

def test_classification_toy():
    """Check classification on a toy dataset."""
    # Random forest
    clf = RandomForestClassifier(n_trees=10, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    clf = RandomForestClassifier(n_trees=10, max_features=1, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    # Extra-trees
    clf = ExtraTreesClassifier(n_trees=10, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

    clf = ExtraTreesClassifier(n_trees=10, max_features=1, random_state=1)
    clf.fit(X, y)
    assert_array_equal(clf.predict(T), true_result)

def test_iris():
    """Check consistency on dataset iris."""
    for c in ("gini", "entropy"):
        # Random forest
        clf = RandomForestClassifier(n_trees=10, criterion=c, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

        clf = RandomForestClassifier(n_trees=10, criterion=c, max_features=2, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.5, "Failed with criterion %s and score = %f" % (c, score)

        # Extra-trees
        clf = ExtraTreesClassifier(n_trees=10, criterion=c, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

        clf = ExtraTreesClassifier(n_trees=10, criterion=c, max_features=2, random_state=1)
        clf.fit(iris.data, iris.target)
        score = clf.score(iris.data, iris.target)
        assert score > 0.9, "Failed with criterion %s and score = %f" % (c, score)

def test_boston():
    """Check consistency on dataset boston house prices."""
    for c in ("mse",):
        # Random forest
        clf = RandomForestRegressor(n_trees=10, criterion=c, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

        clf = RandomForestRegressor(n_trees=10, criterion=c, max_features=6, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

        # Extra-trees
        clf = ExtraTreesRegressor(n_trees=10, criterion=c, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

        clf = ExtraTreesRegressor(n_trees=10, criterion=c, max_features=6, random_state=1)
        clf.fit(boston.data, boston.target)
        score = clf.score(boston.data, boston.target)
        assert score < 3, "Failed with max_features=None, criterion %s and score = %f" % (c, score)

def test_probability():
    """Predict probabilities."""
    # Random forest
    clf = RandomForestClassifier(n_trees=10, random_state=1)
    clf.fit(iris.data, iris.target)
    assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))
    assert_array_almost_equal(clf.predict_proba(iris.data), np.exp(clf.predict_log_proba(iris.data)))

    # Extra-trees
    clf = ExtraTreesClassifier(n_trees=10, random_state=1)
    clf.fit(iris.data, iris.target)
    assert_array_almost_equal(np.sum(clf.predict_proba(iris.data), axis=1), np.ones(iris.data.shape[0]))
    assert_array_almost_equal(clf.predict_proba(iris.data), np.exp(clf.predict_log_proba(iris.data)))

def test_error():
    """Check that proper errors are triggered."""
    # Random forest
    assert_raises(ValueError, RandomForestClassifier(n_trees=-1).fit, X, y)
    assert_raises(ValueError, RandomForestRegressor(n_trees=-1).fit, X, y)

    # Extra-trees
    assert_raises(ValueError, ExtraTreesClassifier(n_trees=-1).fit, X, y)
    assert_raises(ValueError, ExtraTreesRegressor(n_trees=-1).fit, X, y)

def test_pickle():
    """Check pickability."""
    import pickle

    # Random forest
    obj = RandomForestClassifier()
    obj.fit(iris.data, iris.target)
    score = obj.score(iris.data, iris.target)
    s = pickle.dumps(obj)

    obj2 = pickle.loads(s)
    assert_equal(type(obj2), obj.__class__)
    score2 = obj2.score(iris.data, iris.target)
    assert score == score2

    obj = RandomForestRegressor()
    obj.fit(boston.data, boston.target)
    score = obj.score(boston.data, boston.target)
    s = pickle.dumps(obj)

    obj2 = pickle.loads(s)
    assert_equal(type(obj2), obj.__class__)
    score2 = obj2.score(boston.data, boston.target)
    assert score == score2

    # Extra-trees
    obj = ExtraTreesClassifier()
    obj.fit(iris.data, iris.target)
    score = obj.score(iris.data, iris.target)
    s = pickle.dumps(obj)

    obj2 = pickle.loads(s)
    assert_equal(type(obj2), obj.__class__)
    score2 = obj2.score(iris.data, iris.target)
    assert score == score2

    obj = ExtraTreesRegressor()
    obj.fit(boston.data, boston.target)
    score = obj.score(boston.data, boston.target)
    s = pickle.dumps(obj)

    obj2 = pickle.loads(s)
    assert_equal(type(obj2), obj.__class__)
    score2 = obj2.score(boston.data, boston.target)
    assert score == score2
{'fraction_non_alphanumeric': 0.13151629587061817, 'fraction_numerical': 0.017404154010343977, 'mean_word_length': 3.8630738522954093, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2217286', 'n_tokens_mistral': 4559, 'n_tokens_neox': 4328, 'n_words': 966}