def test_linear_regression_persistence(self): model = LinearRegression(['a', 'b', 'c'], fit_intercept=False) model.fit(self.train_x, self.train_y) desc = model.save() new_model = LinearRegression.load(desc) calculated_y = new_model.predict(self.predict_x) expected_y = model.predict(self.predict_x) np.testing.assert_array_almost_equal(calculated_y, expected_y)
def test_linear_regression(self): model = LinearRegression(['a', 'b', 'c'], fit_intercept=False) model.fit(self.train_x, self.train_y) calculated_y = model.predict(self.predict_x) expected_model = LinearRegression2(fit_intercept=False) expected_model.fit(self.train_x, self.train_y) expected_y = expected_model.predict(self.predict_x) np.testing.assert_array_almost_equal(calculated_y, expected_y)
def test_const_linear_model_score(self): model = LinearRegression(['a', 'b', 'c'], fit_intercept=False) model.fit(self.train_x, self.train_y) expected_score = model.score(self.train_x, self.train_y) const_model = ConstLinearModel(features=['a', 'b', 'c'], weights=dict(zip(model.features, model.weights))) calculated_score = const_model.score(self.train_x, self.train_y) self.assertAlmostEqual(expected_score, calculated_score)
def test_load_model(self): model = LinearRegression(['a', 'b', 'c']) model.fit(self.trained_x, self.trained_y) model_desc = model.save() new_model = load_model(model_desc) np.testing.assert_array_almost_equal(model.predict(self.predict_x), new_model.predict(self.predict_x)) self.assertEqual(model.features, new_model.features) self.assertEqual(model.trained_time, new_model.trained_time)
import sqlalchemy as sa import arrow import numpy as np import pandas as pd from alphamind.api import * from alphamind.data.dbmodel.models import Models from alphamind.model.linearmodel import LinearRegression engine = SqlEngine( 'postgresql+psycopg2://postgres:[email protected]/alpha') x = np.random.randn(1000, 3) y = np.random.randn(1000) model = LinearRegression(['a', 'b', 'c']) model.fit(x, y) model_desc = model.save() df = pd.DataFrame() new_row = dict(trade_date='2017-09-05', portfolio_name='test', model_type='LinearRegression', version=1, model_desc=model_desc, update_time=arrow.now().format()) df = df.append([new_row]) df.to_sql(Models.__table__.name,