def _make_transform_args(estimator, random_state=None): if is_series_as_features_transformer(estimator): return make_classification_problem(random_state=random_state) elif is_single_series_transformer(estimator) or is_forecaster(estimator): y = make_forecasting_problem(random_state=random_state) return (y,) else: raise ValueError(f"Estimator type: {type(estimator)} not supported")
def _make_fit_args(estimator, random_state=None, **kwargs): if is_forecaster(estimator): y = make_forecasting_problem(random_state=random_state, **kwargs) fh = 1 return y, fh elif is_classifier(estimator): return make_classification_problem(random_state=random_state, **kwargs) elif is_regressor(estimator): return make_regression_problem(random_state=random_state, **kwargs) elif is_series_as_features_transformer(estimator): return make_classification_problem(random_state=random_state, **kwargs) elif is_single_series_transformer(estimator): y = make_forecasting_problem(random_state=random_state, **kwargs) return (y,) else: raise ValueError(f"Estimator type: {type(estimator)} not supported")
def test_tsfresh_extractor(default_fc_parameters): X, y = make_classification_problem() X_train, X_test, y_train, y_test = train_test_split(X, y) transformer = TSFreshFeatureExtractor( default_fc_parameters=default_fc_parameters, disable_progressbar=True) Xt = transformer.fit_transform(X_train, y_train) actual = Xt.filter(like="__mean", axis=1).values.ravel() expected = tabularize(X_train).mean(axis=1).values assert expected[0] == X_train.iloc[0, 0].mean() np.testing.assert_allclose(actual, expected)
def _make_predict_args(estimator, *args, **kwargs): if is_forecaster(estimator): fh = 1 return (fh,) elif is_classifier(estimator): X, y = make_classification_problem(*args, **kwargs) return (X,) elif is_regressor(estimator): X, y = make_regression_problem(*args, **kwargs) return (X,) else: raise ValueError(f"Estimator type: {type(estimator)} not supported")
from benchmarks.utils import np_3d_arr def _slice(X): return X[10:20, 5:15, 50:60] def _nested_slice(X): x = X.iloc[10:20, 5:15] return np.asarray( [[x.iloc[i, j].iloc[50:60].to_numpy() for j in range(x.shape[1])] for i in range(x.shape[0])]) X, _ = make_classification_problem(n_instances=100, n_timepoints=100, n_columns=20) expected = _slice(np_3d_arr(X)) def test_ak_3d_slice(benchmark): x = ak_3d_arr(X) actual = benchmark(_slice, x) np.testing.assert_array_equal(actual, expected) def test_ak_record_slice(benchmark): x = ak_record_arr(X) actual = benchmark(_slice, x) np.testing.assert_array_equal(actual["value"], expected)
from sklearn.pipeline import FeatureUnion from sklearn.pipeline import Pipeline from sklearn.preprocessing import FunctionTransformer from sklearn.tree import DecisionTreeClassifier from sktime.classification.compose import TimeSeriesForestClassifier from sktime.datasets import load_gunpoint from sktime.transformers.series_as_features.compose import RowTransformer from sktime.transformers.series_as_features.segment import \ RandomIntervalSegmenter from sktime.transformers.series_as_features.summarize import \ RandomIntervalFeatureExtractor from sktime.utils._testing.series_as_features import \ make_classification_problem from sktime.utils.time_series import time_series_slope X, y = make_classification_problem() n_classes = len(np.unique(y)) # Check simple cases. def test_predict_proba(): clf = TimeSeriesForestClassifier(n_estimators=2) clf.fit(X, y) proba = clf.predict_proba(X) assert proba.shape == (X.shape[0], n_classes) np.testing.assert_array_equal(np.ones(X.shape[0]), np.sum(proba, axis=1)) # test single row input y_proba = clf.predict_proba(X.iloc[[0], :]) assert y_proba.shape == (1, n_classes)
from sklearn.base import clone from sklearn.pipeline import Pipeline from sklearn.pipeline import FeatureUnion from sklearn.tree import DecisionTreeClassifier from sklearn.preprocessing import FunctionTransformer from sktime.utils.time_series import time_series_slope from sktime.transformers.series_as_features.segment import IntervalSegmenter from sktime.transformers.series_as_features.compose import RowTransformer from sktime.transformers.series_as_features.summarize._extract import \ RandomIntervalFeatureExtractor from sktime.classification.compose._ensemble import TimeSeriesForestClassifier from sktime.utils._testing.series_as_features import \ make_classification_problem X_train, y_train = make_classification_problem() # Check results of a simple case of single estimator, single feature and # single interval from different but equivalent implementations def test_feature_importances_single_feature_interval_and_estimator(): random_state = 1234 # Compute using default method features = [np.mean] steps = [('transform', RandomIntervalFeatureExtractor(n_intervals=1, features=features, random_state=random_state)), ('clf', DecisionTreeClassifier())] base_estimator = Pipeline(steps)