Why isn't ClassifierPipeline working here? #7003
-
|
EDIT: Keeping original post, but just skip to the end. Most of this isn't relevant to reproducing the error. from synthdata import trace_clusters
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sktime.classification.sklearn import RotationForest
from sktime.transformations.panel.shapelet_transform import ShapeletTransform
from sktime.classification.compose import ClassifierPipeline
# The following code generates a synthetic dataset consisting of n_samps examples
# from n_clusters (or classes), with 32 time points each.
n_samps = 9
n_clusters = 2 #clusters or classes
_, traces, targs = trace_clusters(n_samps,
plot=True,mode='flat',sf=0.05,
ns_lvl=0.1,pnts=32,
n_clusters=n_clusters)
# This is a matrix of dimension (18, 1, 32). That's 9 curves drawn from 2
# clusters/classes (9 x 2 = 18), each with 32 time points.
traces3d = traces[:,None,:]
# Split traces3d in to train and test datasets. Just split in half (0.5) for
# simplicity.
x_train, x_test, y_train, y_test = train_test_split(traces3d,targs, test_size=0.5, shuffle=True)
# Get our transformer and do the fit.
stf = ShapeletTransform(max_shapelets_to_store_per_class=6,verbose=1, remove_self_similar=False)
# res is a matrix of dimension (9, 12) where we have 9 total examples and
# 2 classes x 6 shapelets per class = 12.
res = stf.fit_transform(x_train, y_train)
# Now classify using RoationForest. Everything up to here works:
clf = RotationForest(n_estimators=10)
clf.fit(res, y_train)
yp = clf.predict(stf.transform(x_test))
print(classification_report(y_test, yp))
# The following doesn't work: throws AttributeError: 'RotationForest' object has no attribute 'clone' error.
pipeline = ClassifierPipeline(
RotationForest(n_estimators=10), [ShapeletTransform(max_shapelets_to_store_per_class=6,verbose=1, remove_self_similar=False)]
)As noted, my code executes correctly up to the print statement: However, when I try to combine the transformation and classifier operations together with ClassifierPipeline, it throws the error: UPDATE: You don't actually need all the above code. This is sufficient for minimal reproduceability: pipeline = ClassifierPipeline(
RotationForest(n_estimators=10), [ShapeletTransform(max_shapelets_to_store_per_class=6,verbose=1, remove_self_similar=False)]
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Ah, that's because To use the It might be a bit unclear which pipeline to use directly, a better way is to use from sktime.pipeline import make_pipeline
pipeline = make_pipeline(
[ShapeletTransform(max_shapelets_to_store_per_class=6,verbose=1, remove_self_similar=False), RotationForest(n_estimators=10)]
)or dunder notation pipeline = (
ShapeletTransform(max_shapelets_to_store_per_class=6,verbose=1, remove_self_similar=False) * RotationForest(n_estimators=10)
)this will select the correct pipeline automatically. I recognize this is a bit suboptimal that there are multiple pipelines, there is a sequence of PR to make them more polymorphic, e.g., it should be possible to pass both sklearn and TS classifiers after this is done, see |
Beta Was this translation helpful? Give feedback.
oh, I see - the reason is that the
RotationForestis in fact not asklearncompliant classifier - it does not inherit from theClassifierMixin.Once that is fixed, the dunders should work.
Until then, you can use directly the
SklearnClassifierPipeline(KNeighborsClassifier(), [ShapeletTransform()])- sorry for this inconvenience