def shapelet_transform(run_tag): # Shapelet transformation st = ShapeletTransform(n_shapelets=5, window_sizes=[0.1, 0.2, 0.3, 0.4 ], sort=True, verbose=1, n_jobs=1) path = 'data/' + run_tag + '/' + run_tag + '_eval.txt' data = load_ucr(path) X = data[:, 1:] y = data[:, 0] X_new = st.fit_transform(X, y) file = open('shapelet_pos/' + run_tag + '_shapelet_pos.txt', 'w+') for i, index in enumerate(st.indices_): idx, start, end = index file.write(run_tag + ' ') file.write(str(idx) + ' ') file.write(str(start) + ' ') file.write(str(end) + '\n') file.close() # Visualize the most discriminative shapelets plt.figure(figsize=(6, 4)) for i, index in enumerate(st.indices_): idx, start, end = index plt.plot(X[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X[idx, start:end], lw=5, color='C{}'.format(i)) plt.xlabel('Time', fontsize=12) plt.title('The five more discriminative shapelets', fontsize=14) plt.legend(loc='best', fontsize=8) plt.savefig('shapelet_fig/' + run_tag + '_shapelet.pdf')
def test_attributes_shape(params, X, y, fewer_shapelets, attr_expected): """Test the attributes of a ShapaletTransform instance.""" shapelet = ShapeletTransform(**params) shapelet.fit(X, y) # Check 'window_range_' window_sizes_auto = (isinstance(shapelet.window_sizes, str) and shapelet.window_sizes == 'auto') if window_sizes_auto: assert isinstance(shapelet.window_range_, tuple) assert (0 < shapelet.window_range_[0] <= shapelet.window_range_[1] <= 8) else: assert shapelet.window_range_ is None # Check other attributes n_shapelets_actual = len(shapelet.shapelets_) if fewer_shapelets: assert n_shapelets_actual < attr_expected['n_shapelets'] assert shapelet.indices_.shape[0] < attr_expected['indices_shape'][0] assert shapelet.scores_.size < attr_expected['scores_size'] else: assert n_shapelets_actual == attr_expected['n_shapelets'] assert shapelet.indices_.shape == attr_expected['indices_shape'] assert shapelet.scores_.size == attr_expected['scores_size']
def test_fit_transform(params): """Test that 'fit_transform' and 'fit' then 'transform' yield same res.""" shapelet_1 = ShapeletTransform(random_state=42, **params) shapelet_2 = ShapeletTransform(random_state=42, **params) X_fit_transform = shapelet_1.fit_transform(X, y) X_fit_then_transform = shapelet_2.fit(X, y).transform(X) # Test that the transformation are identical np.testing.assert_allclose(X_fit_transform, X_fit_then_transform, atol=1e-5, rtol=0.) # Test that the shapelets are identical for (shap_1, shap_2) in zip(shapelet_1.shapelets_, shapelet_2.shapelets_): np.testing.assert_allclose(shap_1, shap_2, atol=1e-5, rtol=0.) # Test that the remaining attributes are identical np.testing.assert_allclose(shapelet_1.scores_, shapelet_2.scores_, atol=1e-5, rtol=0.) np.testing.assert_array_equal(shapelet_1.indices_, shapelet_2.indices_) shapelets_not_none = ((shapelet_1.window_range_ is not None) and (shapelet_2.window_range_ is not None)) if shapelets_not_none: np.testing.assert_array_equal(shapelet_1.window_range_, shapelet_2.window_range_)
def test_parameter_check(params, error, err_msg): """Test parameter validation.""" shapelet = ShapeletTransform(**params) with pytest.raises(error, match=re.escape(err_msg)): shapelet._check_params(X, y)
This example illustrates the transformation of this algorithm and highlights the most discriminative shapelets that have been selected. It is implemented as :class:`pyts.transformation.ShapeletTransform`. """ import numpy as np import matplotlib.pyplot as plt from pyts.datasets import load_gunpoint from pyts.transformation import ShapeletTransform # Toy dataset X_train, _, y_train, _ = load_gunpoint(return_X_y=True) # Shapelet transformation st = ShapeletTransform(window_sizes=[12, 24, 36, 48], random_state=42, sort=True) X_new = st.fit_transform(X_train, y_train) # Visualize the four most discriminative shapelets plt.figure(figsize=(6, 4)) for i, index in enumerate(st.indices_[:4]): idx, start, end = index plt.plot(X_train[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X_train[idx, start:end], lw=5, color='C{}'.format(i))
def executeShapeletTransform(datasetName): # INPUT: Dataset name # Execution of a ShapeletTransformation algorithm over the dataset: datasetName X_train, y_train, X_test, y_test = UCR_UEA_datasets().load_dataset( datasetName) #RE-SIZE BY FUN X TRAIN dfTrain = computeLoadedDataset(X_train, y_train) y_train = dfTrain['target'].values y_train = y_train.astype(int) del dfTrain['target'] del dfTrain['TsIndex'] # RE-SIZE BY FUN X TEST dfTest = computeLoadedDataset(X_test, y_test) y_test = dfTest['target'].values y_test = y_test.astype(int) del dfTest['target'] del dfTest['TsIndex'] #inizio preprocessing train start_timePreprocessingTrain = time.time() #Shapelet transformation WITH RANDOM STATE #NB: IN ORDER TO MAKE A VALID COMPARISON WITH TSCMP, THE WINDOW SIZE VALUE MUST BE THE SAME OF THE VALUE CHOSEN IN TSCMP st = ShapeletTransform(window_sizes=[20], sort=True) X_new = st.fit_transform(dfTrain, y_train) # fine preprocessing train PreprocessingTrainTime = time.time() - start_timePreprocessingTrain from sklearn.tree import DecisionTreeClassifier clf = DecisionTreeClassifier(criterion='entropy', max_depth=3, min_samples_leaf=20) # inizio train start_timeTrain = time.time() clf.fit(X_new, y_train) # fine train TrainTime = time.time() - start_timeTrain # inizio preprocessing test start_timePreprocessingTest = time.time() X_test_new = st.transform(dfTest) # fine preprocessing test PreprocessingTestTime = time.time() - start_timePreprocessingTest # inizio test start_timeTest = time.time() y_pred = clf.predict(X_test_new) # fine test TestTime = time.time() - start_timeTest print(accuracy_score(y_test, y_pred)) row = [ 'ShapeletTransformation', datasetName, round(accuracy_score(y_test, y_pred), 2), round(PreprocessingTrainTime, 2), round(TrainTime, 2), round(PreprocessingTestTime, 2), round(TestTime, 2) ] WriteCsvShapeletAlgo('Shapelet_Algo_Experiments_29-12.csv', row)
predictors = \ ['rsi_14', 'rsi_14_thresh', 'rsi_21', 'rsi_21_thresh', 'rsi_60', 'rsi_60_thresh', 'macd_12_26', 'macdsignal_12_26', 'macdhist_12_26', 'macd_5_12', 'macdsignal_5_12', 'macdhist_5_12', 'mom', 'roc_20', 'roc_125', 'psar_005', 'psar_02', 'psar_1', 'psar_005_dist', 'psar_02_dist', 'psar_1_dist', 'psar_005_ind', 'psar_02_ind', 'psar_1_ind', 'sma20', 'sma50', 'adx_7', 'adx_14', 'adx_21', 'adx_60', 'cci', 'cci_chg', 'cci_thresh', 'money_flow', 'cross_sma20', 'cross_sma50', 'cross_sma200', 'sma20_above', 'sma50_above', 'cross_stoch', 'cross_macd_12_26', 'cross_macd_5_12', 'roc_20_125', 'adjclose'] X = ticker.indicator_matrix[predictors].dropna(axis=0) X_train = X[predictors].drop(['adjclose'], axis=1).values y_train = X['adjclose'].values st = ShapeletTransform(window_sizes=[3, 5, 10, 14, 21], sort=True) X_new = st.fit_transform(X_train, y_train) plt.figure(figsize=(24, 12)) for i, index in enumerate(st.indices_[:4]): idx, start, end = index plt.plot(X_train[idx], color='C{}'.format(i), label='Sample {}'.format(idx)) plt.plot(np.arange(start, end), X_train[idx, start:end], lw=5, color='C{}'.format(i)) plt.xlabel('Time', fontsize=12) plt.title('The four more discriminative shapelets', fontsize=14)