def test_segmentxyforecast(): Nt = 100 width = 5 vars = 5 # lets do a forecast test seg = transform.SegmentXYForecast(width=width, forecast=5) Xt = [ np.random.rand(Nt, vars), np.random.rand(2 * Nt, vars), np.random.rand(Nt, vars) ] Xc = np.random.rand(3, 4) y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] X = make_ts_data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, vars) assert Xsc.shape == (N, 4) # univariate X vars = 1 seg = transform.SegmentXYForecast(width=width, forecast=5) X = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] y = [np.random.rand(Nt), np.random.rand(2 * Nt), np.random.rand(Nt)] seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width)
def test_util(): df = load_watch() data = util.make_ts_data(df['X'], df['side']) util.get_ts_data_parts(data) util.check_ts_data(data, df['y']) util.check_ts_data(df['X'], df['y']) util.ts_stats(df['X'], df['y'], fs=1., class_labels=df['y_labels'])
def test_feature_rep(): # multivariate ts frep = transform.FeatureRep(features=all_features()) X = np.random.rand(100, 10, 5) y = np.ones(100) frep.fit(X, y) Xt = frep.transform(X) assert Xt.shape[0] == len(X) assert len(frep.f_labels) == Xt.shape[1] # univariate ts X = np.random.rand(100, 10) y = np.ones(100) frep.fit(X, y) Xt = frep.transform(X) assert Xt.shape[0] == len(X) assert len(frep.f_labels) == Xt.shape[1] # single feature frep = transform.FeatureRep(features={'mean': mean}) frep.fit(X, y) Xt = frep.transform(X) assert Xt.shape[0] == len(X) assert len(frep.f_labels) == Xt.shape[1] assert Xt.shape[1] == 1 # ts with multivariate contextual data frep = transform.FeatureRep(features=all_features()) X = make_ts_data(np.random.rand(100, 10, 5), np.random.rand(100, 3)) y = np.ones(100) frep.fit(X, y) Xt = frep.transform(X) assert Xt.shape[0] == len(X) assert len(frep.f_labels) == Xt.shape[1] # ts with univariate contextual data X = make_ts_data(np.random.rand(100, 10, 5), np.random.rand(100)) y = np.ones(100) frep.fit(X, y) Xt = frep.transform(X) assert Xt.shape[0] == len(X) assert len(frep.f_labels) == Xt.shape[1]
def test_pipe_regression(): # no context data, single time series X = [np.random.rand(1000, 10)] y = [np.random.rand(1000)] est = Pipeline([('ftr', FeatureRep()), ('ridge', Ridge())]) pipe = SegPipe(est, segmenter=SegmentXY()) pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # context data, single time seres Xt = [np.random.rand(1000, 10)] Xc = [np.random.rand(3)] X = make_ts_data(Xt, Xc) y = [np.random.rand(1000)] pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # multiple time seres Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = make_ts_data(Xt, Xc) y = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # cross val Xt = np.array([np.random.rand(1000, 10) for i in range(5)]) Xc = np.random.rand(5, 3) X = make_ts_data(Xt, Xc) y = np.array([np.random.rand(1000) for i in range(5)]) cross_validate(pipe, X, y) # transform pipe est = Pipeline([('ftr', FeatureRep()), ('scaler', StandardScaler())]) pipe = SegPipe(est, segmenter=SegmentXY()) Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = make_ts_data(Xt, Xc) y = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] pipe.fit(X, y) pipe.transform(X, y) pipe.fit_transform(X, y)
def test_pipe_classification(): # no context data, single time series X = [np.random.rand(1000, 10)] y = [5] est = Pipeline([('ftr', FeatureRep()), ('ridge', RandomForestClassifier())]) pipe = SegPipe(est, segmenter=SegmentX()) pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # context data, single time seres Xt = [np.random.rand(1000, 10)] Xc = [np.random.rand(3)] X = make_ts_data(Xt, Xc) y = [5] pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # multiple time series Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = make_ts_data(Xt, Xc) y = [1, 2, 3] pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # univariate data Xt = [np.random.rand(1000), np.random.rand(100), np.random.rand(500)] Xc = np.random.rand(3) X = make_ts_data(Xt, Xc) y = [1, 2, 3] pipe.fit(X, y) pipe.predict(X, y) pipe.score(X, y) # transform pipe est = Pipeline([('ftr', FeatureRep()), ('scaler', StandardScaler())]) pipe = SegPipe(est, segmenter=SegmentX()) Xt = [ np.random.rand(1000, 10), np.random.rand(100, 10), np.random.rand(500, 10) ] Xc = np.random.rand(3, 3) X = make_ts_data(Xt, Xc) y = [1, 2, 3] pipe.fit(X, y) pipe.transform(X, y) pipe.fit_transform(X, y)
def test_segmentx(): width = 5 vars = 5 seg = transform.SegmentX(width=width) # multivariate ts data without context data Xt = [ np.random.rand(100, vars), np.random.rand(100, vars), np.random.rand(100, vars) ] y = np.random.rand(3) X = make_ts_data(Xt) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width, vars) # univariate ts data without context Xt = [np.random.rand(100), np.random.rand(100), np.random.rand(100)] y = np.random.rand(3) X = make_ts_data(Xt) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) N = len(ys) assert Xs.shape == (N, width) # multivariate ts data with context data Xt = [ np.random.rand(100, vars), np.random.rand(200, vars), np.random.rand(50, vars) ] Xc = np.random.rand(3, 4) y = np.random.rand(3) X = make_ts_data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, vars) assert Xsc.shape == (N, 4) # ts data with univariate context data Xt = [np.random.rand(100), np.random.rand(200), np.random.rand(50)] Xc = np.random.rand(3) y = np.random.rand(3) X = make_ts_data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width) assert Xsc.shape == (N, 1) # same number as context vars and time vars # this would cause broadcasting failure before implementation of TS_Data class Xt = [ np.random.rand(100, vars), np.random.rand(200, vars), np.random.rand(50, vars) ] Xc = np.random.rand(3, vars) y = np.random.rand(3) X = make_ts_data(Xt, Xc) seg.fit(X, y) Xs, ys, _ = seg.transform(X, y) Xst, Xsc = get_ts_data_parts(Xs) N = len(ys) assert Xst.shape == (N, width, vars) assert Xsc.shape == (N, 5)