Sktime: KNN Classifier: 'function' object has no attribute '__wrapped__'

Created on 17 Jun 2020  路  12Comments  路  Source: alan-turing-institute/sktime

Describe the bug

I am using the following the demo for univariate time series classification
Calling the first example KNeighborsTimeSeriesClassifier gives the error: 'function' object has no attribute '__wrapped__'

To Reproduce

import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer
from sklearn.tree import DecisionTreeClassifier
from sktime.classification.compose import TimeSeriesForestClassifier
from sktime.classification.distance_based import KNeighborsTimeSeriesClassifier
from sktime.datasets import load_gunpoint
from sktime.series_as_features.compose import FeatureUnion
from sktime.transformers.series_as_features.compose import RowTransformer
from sktime.transformers.series_as_features.reduce import Tabularizer
from sktime.transformers.series_as_features.segment import \
    RandomIntervalSegmenter
from sktime.utils.time_series import time_series_slope
from statsmodels.tsa.ar_model import AutoReg
from statsmodels.tsa.stattools import acf

X, y = load_gunpoint(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)

knn = KNeighborsTimeSeriesClassifier(metric='dtw')
knn.fit(X_train, y_train)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-5eb9acd6a688> in <module>
      1 knn = KNeighborsTimeSeriesClassifier(metric='dtw')
----> 2 knn.fit(X_train, y_train)

~/anaconda3/lib/python3.7/site-packages/sktime/classification/distance_based/_time_series_neighbors.py in fit(self, X, y)
    221             self._y = self._y.ravel()
    222 
--> 223         temp = check_array.__wrapped__.__code__
    224         check_array.__wrapped__.__code__ = _check_array_ts.__code__
    225         fx = self._fit(X)

AttributeError: 'function' object has no attribute '__wrapped__'

Expected behavior
I expect the code to run :-)

Versions

Linux-4.9.0-12-amd64-x86_64-with-debian-9.12
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]
NumPy 1.18.1
SciPy 1.4.1
Pandas 0.25.3
sktime 0.4.0

bug

All 12 comments

Thanks for raising the issue! We'll look into this!

I am interested to have a look, seems like it got something to do with the wrapper decorator.

@Quaterion may I know which version of Sci-Kit learn you are using?

I suspect that this error will occur if using sci-kit learn 0.22 or below. The decorator for check_array was added in 27th April 2020 so was released in 0.23.0rc1 and after

@Cheukting Thanks for offering to work on this! KNN needs some refactoring I believe, the current solution isn't very clean. But check if any changes have been made already in #287. I'd really appreciate a clean up of KNN!

I can have a look, it's quite easy to solve this problem to make it compatible, just need to add check if this function is wrapped.

@mloning seems the problem code is still there in #287
https://github.com/alan-turing-institute/sktime/blob/bd050d986e2c38c193fd30056060f33c7cdca2e3/sktime/classification/distance_based/_time_series_neighbors.py#L224
How can I contribute? Do you want me to make a PR? if so, to where?

Creating a PR into our dev branch would be great!

Ideally, I'd prefer a solution in which we do not temporarily overwrite the check_array function which seems really hacky to me. But that might require more refactoring to make it work with sklearn's classes.

@Quaterion may I know which version of Sci-Kit learn you are using?

It is version '0.22.1'

Creating a PR into our dev branch would be great!

Ideally, I'd prefer a solution in which we do not temporarily overwrite the check_array function which seems really hacky to me. But that might require more refactoring to make it work with sklearn's classes.

I see the whole issue begins with overriding sci-kit learn's check_array. Give me some time tomorrow and let me see if I can find a solution to that.

I have made the above PR to patch it. Unfortunately, check_array is used all over the places in sci-kit learn and there is no easy way to remove check_array otherwise. I think having a discussion with the sci-kit learn team to see if adding an extra option to bypass the check would be the best solution.

Closed by #310

Was this page helpful?
0 / 5 - 0 ratings

Related issues

big-o picture big-o  路  6Comments

mloning picture mloning  路  9Comments

ninfueng picture ninfueng  路  9Comments

aiwalter picture aiwalter  路  8Comments

mloning picture mloning  路  10Comments