def test_fit_predict_normalize(): '''Tests with dataset''' D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2) ssm = SSM(False, False, 1, normalize_err=True) Y = ssm.fit_predict(D) assert Y.any()
def test_yes(): D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() tw = TemporalWeight('yes', 2) y = tw.fit_predict(D) for i in xrange(D.shape[0]): val = D[i][-1] assert_equal(val, y[i])
def test_rbf_with_dataset(): '''Tests the RBF model with TimeSeriesDataset''' rbf_model = RidgeRBFModel(10, .5, alpha=.01) D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-6) y = D.np_like_firstn().sum(axis=1) model = rbf_model.fit(D, y) y_pred = model.predict(D) mrse = (((y - y_pred) / y)**2).mean() assert_almost_equal(0, mrse, 4)
def test_pow(): D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() tw = TemporalWeight('pow', 2) y = tw.fit_predict(D) for i in xrange(D.shape[0]): div = sum((np.arange(D.shape[1]) + 1)**2) val = sum(D[i] * ((np.arange(D.shape[1]) + 1)**2) / div) assert_equal(val, y[i])
def test_pow(): D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() tw = TemporalWeight('pow', 2) y = tw.fit_predict(D) for i in xrange(D.shape[0]): div = sum((np.arange(D.shape[1]) + 1) ** 2) val = sum(D[i] * ((np.arange(D.shape[1]) + 1) ** 2) / div) assert_equal(val, y[i])
def test_one_series(): '''Tests with only one time series''' y = tsio.from_id_row_mat(SM_ONE_SEQUENCE, add_eps=1e-2).np_like_firstn() pred = SpikeM(0).fit_predict(y, [24], True) from matplotlib import pyplot as plt x = range(y.shape[1]) plt.plot(x, y[0]) plt.plot(x, pred[0]) plt.show() rmse = np.sqrt((pred[0] - y[0]) ** 2).sum() / len(y[0]) print(rmse) assert rmse < 8
def test_one_series(): '''Tests with only one time series''' y = tsio.from_id_row_mat(SM_ONE_SEQUENCE, add_eps=1e-2).np_like_firstn() pred = SpikeM(0).fit_predict(y, [24], True) from matplotlib import pyplot as plt x = range(y.shape[1]) plt.plot(x, y[0]) plt.plot(x, pred[0]) plt.show() rmse = np.sqrt((pred[0] - y[0])**2).sum() / len(y[0]) print(rmse) assert rmse < 8
def test_fit_predict2(): ''' Tests the fit and predict methods for the state space models. Period Only ''' D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() D = D[: 50] ssm = SSM(False, True, 50) X_train = D[:, :50] X_test = D[:, 50:] Y = ssm.fit_predict(X_train) err = ((X_test - Y) ** 2).mean() assert_equal((50, 50), Y.shape) assert err > 0
def test_fit_predict(): ''' Tests the fit and predict methods for the spike m ''' D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() D = D[:50] sm = SpikeM() X_train = D[:, :50] X_test = D[:, 50:] Y = sm.fit_predict(X_train) err = ((X_test - Y)**2).mean() assert_equal((50, ), Y.shape) assert err > 0
def test_fit_predict(): ''' Tests the fit and predict methods for the spike m ''' D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() D = D[: 50] sm = SpikeM() X_train = D[:, :50] X_test = D[:, 50:] Y = sm.fit_predict(X_train) err = ((X_test - Y) ** 2).mean() assert_equal((50,), Y.shape) assert err > 0
def test_ml_model(): '''Tests the ML model''' ml_model = MLModel() D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-6).np_like_firstn() D += 1e-6 X_train = D[:500, :7] X_test = D[500:, :7] y_train = D.sum(axis=1)[:500] y_test = D.sum(axis=1)[500:] model = ml_model.fit(X_train, y_train) y_pred = model.predict(X_test) mrse = (((y_test - y_pred) / y_test)**2).mean() assert_equal(1, mrse > 0) assert_equal(1, mrse < 1)
def test_rbf_model(): '''Tests the RBF model''' rbf_model = RidgeRBFModel(100, 50, alpha=.00001) D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-6).np_like_firstn() X_train = D[:500, :7] X_test = D[500:, :7] y_train = D.sum(axis=1)[:500] y_test = D.sum(axis=1)[500:] model = rbf_model.fit(X_train, y_train) y_pred = model.predict(X_test) mrse = (((y_test - y_pred) / y_test)**2).mean() print(mrse) assert_equal(1, mrse > 0) assert_equal(1, mrse < 1)
def test_fit_predict(): ''' Tests the fit and predict methods for the state space models. Trend + period ''' D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2).np_like_firstn() D = D[: 50] ssm = SSM(True, True, 50) D = (D.T / D.sum(axis=1)).T X_train = D[:, :50] X_test = D[:, 50:] Y = ssm.fit_predict(X_train) err = ((X_test - Y) ** 2).mean() assert_equal((50, 50), Y.shape) print(err) assert err > 0
def test_from_mat(): dataset = tsio.from_id_row_mat(YOUTUBE_1K) assert_equal(1000, dataset.num_series) for series in dataset: assert_equal(100, len(series))
def test_avg(): D = tsio.from_id_row_mat(YOUTUBE_1K, add_eps=1e-2) tw = TemporalWeight('avg') y = tw.fit_predict(D) assert_array_equal(D.np_like_firstn().mean(axis=1), y)