def test_time_series_slope(): Y = np.array( generate_df_from_array(np.random.normal(size=10), n_rows=100).iloc[:, 0].tolist()) y = Y[0, :] # Compare with scipy's linear regression function x = np.arange(y.size) + 1 a = linregress(x, y).slope b = time_series_slope(y) np.testing.assert_almost_equal(a, b, decimal=10) # Check computations over axis a = np.apply_along_axis(time_series_slope, 1, Y) b = time_series_slope(Y, axis=1) np.testing.assert_equal(a, b) a = time_series_slope(Y, axis=1)[0] b = time_series_slope(y) np.testing.assert_equal(a, b) # Check linear and constant cases for step in [-1, 0, 1]: y = np.arange(1, 4) * step np.testing.assert_almost_equal(time_series_slope(y), step, decimal=10)
def test_time_series_slope_against_scipy_linregress(trend_order): coefs = np.random.normal(size=(trend_order + 1, 1)) y = generate_polynomial_series(20, order=trend_order, coefs=coefs) # Compare with scipy's linear regression function x = np.arange(y.size) + 1 a = linregress(x, y).slope b = time_series_slope(y) np.testing.assert_almost_equal(a, b, decimal=10)
def _test_results(n_cols, n_obs, n_intervals): x = np.random.normal(size=n_obs) X = generate_df_from_array(x, n_rows=3, n_cols=n_cols) trans = RandomIntervalFeatureExtractor(n_intervals=n_intervals, features=[np.mean, np.std, time_series_slope]) Xt = trans.fit_transform(X) # Check results for c in range(n_cols): for s, e in trans.intervals_[c]: assert np.all(Xt.filter(like=f'_{s}_{e}_mean') == np.mean(x[s:e])) assert np.all(Xt.filter(like=f'_{s}_{e}_std') == np.std(x[s:e])) assert np.all(Xt.filter(like=f'_{s}_{e}_time_series_slope') == time_series_slope(x[s:e]))
def _transform(X, intervals): """ Compute the mean, standard deviation and slope for given intervals of input data X. """ n_instances, _ = X.shape n_intervals, _ = intervals.shape transformed_x = np.empty(shape=(3 * n_intervals, n_instances), dtype=np.float32) for j in range(n_intervals): X_slice = X[:, intervals[j][0]:intervals[j][1]] means = np.mean(X_slice, axis=1) std_dev = np.std(X_slice, axis=1) slope = time_series_slope(X_slice, axis=1) transformed_x[3 * j] = means transformed_x[3 * j + 1] = std_dev transformed_x[3 * j + 2] = slope return transformed_x.T
def __cif_feature(self, X, i, j, a, c22): if self.atts[i][a] == 22: # mean return np.mean(X[:, 0, self.intervals[i][j][0]:self.intervals[i][j][1]], axis=1) elif self.atts[i][a] == 23: # std_dev return np.std(X[:, 0, self.intervals[i][j][0]:self.intervals[i][j][1]], axis=1) elif self.atts[i][a] == 24: # slope return time_series_slope( X[:, 0, self.intervals[i][j][0]:self.intervals[i][j][1]], axis=1) else: return c22._transform_single_feature( X[:, 0, self.intervals[i][j][0]:self.intervals[i][j][1]], feature=a)
def test_results(n_instances, n_timepoints, n_intervals): x = np.random.normal(size=n_timepoints) X = generate_df_from_array(x, n_rows=n_instances, n_cols=1) t = RandomIntervalFeatureExtractor( n_intervals=n_intervals, features=[np.mean, np.std, time_series_slope]) Xt = t.fit_transform(X) # Check results intervals = t.intervals_ for start, end in intervals: expected_mean = np.mean(x[start:end]) expected_std = np.std(x[start:end]) expected_slope = time_series_slope(x[start:end]) actual_means = Xt.filter(like=f'*_{start}_{end}_mean').values actual_stds = Xt.filter(like=f'_{start}_{end}_std').values actual_slopes = Xt.filter( like=f'_{start}_{end}_time_series_slope').values assert np.all(actual_means == expected_mean) assert np.all(actual_stds == expected_std) assert np.all(actual_slopes == expected_slope)
def test_time_series_slope_against_simple_cases(slope): x = np.arange(1, 10) y = x * slope np.testing.assert_almost_equal(time_series_slope(y), slope, decimal=10)