Sktime: [BUG] ModuleNotFoundError because of change of scikit_learn upgrade

Created on 12 Jan 2021  路  2Comments  路  Source: alan-turing-institute/sktime

Describe the bug

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-10-f1e54e367177> in <module>
      1 import time
      2 
----> 3 from sklearn.ensemble.forest import RandomForestClassifier
      4 from sklearn.pipeline import Pipeline
      5 

ModuleNotFoundError: No module named 'sklearn.ensemble.forest'

To Reproduce

import time

from sklearn.ensemble.forest import RandomForestClassifier
from sklearn.pipeline import Pipeline

from sktime.datasets import load_osuleaf

train_x, train_y = load_osuleaf(split="train", return_X_y=True)
test_x, test_y = load_osuleaf(split="test", return_X_y=True)

# example pipeline with 1 minute time limit
pipeline = Pipeline(
    [
        (
            "st",
            ContractedShapeletTransform(
                time_contract_in_mins=time_contract_in_mins,
                num_candidates_to_sample_per_case=10,
                verbose=False,
            ),
        ),
        ("rf", RandomForestClassifier(n_estimators=100)),
    ]
)

start = time.time()
pipeline.fit(train_x, train_y)
end_build = time.time()
preds = pipeline.predict(test_x)
end_test = time.time()

print("Results:")
print("Correct:")
correct = sum(preds == test_y)
print("\t" + str(correct) + "/" + str(len(test_y)))
print("\t" + str(correct / len(test_y)))
print("\nTiming:")
print("\tTo build:   " + str(end_build - start) + " secs")
print("\tTo predict: " + str(end_test - end_build) + " secs")

Expected behavior

1.1210031509399414
2.162216901779175
2.449451446533203
2.5960922241210938
Results:
Correct:
    121/242
    0.5

Timing:
    To build:   68.00058245658875 secs
    To predict: 8.72866439819336 secs

Additional context

I upgrade scikit_learn from 0.23.1 to 0.24.0 and unfortunately failed in this cell.
The reason is that scikit_learn community changed the sub-module name from forest to _forest.

I wonder instead of such fine-grained import, we can avoid such issue by a little more coarse-grained import. For this case:

from sklearn.ensemble import RandomForestClassifier

We already know there cannot be another RandomForestClassifier class and the libs from sklearn.ensemble are not big. Therefore, this seems to a balanced solution for both 0.23.x to 0.24.x version of scikit_learn.

What do you think? If you also agree with this suggestion then I will pull a request to fix that. If you don't like that, I am also willing to hear your explanation.

Versions


System:
machine: Windows-10-10.0.19041-SP0

Python dependencies:
pip: 20.3.3
setuptools: 49.2.1
sklearn: 0.24.0
sktime: 0.5.1
statsmodels: 0.12.1
numpy: 1.19.3
scipy: 1.4.1
Cython: 0.29.17
pandas: 1.1.2
matplotlib: 3.3.2
joblib: 0.17.0
numba: 0.52.0
pmdarima: 1.8.0
tsfresh: 0.17.0

bug

All 2 comments

great spot, thanks, it seems scikit changes structurally quite a bit from .23 to ,24. Your solution looks good to me, put in the PR.

great spot, thanks, it seems scikit changes structurally quite a bit from .23 to ,24. Your solution looks good to me, put in the PR.

Dear prof @TonyBagnall
I've fixed it. Please merge #613 whenever you are free. 鉂わ笍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mloning picture mloning  路  9Comments

big-o picture big-o  路  6Comments

mloning picture mloning  路  10Comments

isma3ilsamir picture isma3ilsamir  路  7Comments

MarkMoretto picture MarkMoretto  路  10Comments