|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | __author__ = 'maxim' |
4 | 4 |
|
5 | | - |
6 | | -import os |
7 | | -import numpy as np |
8 | | - |
9 | 5 | import poloniex |
10 | 6 | from predict import * |
11 | 7 | from train import * |
12 | | -from train.evaluator import Evaluator |
13 | 8 | from util import * |
14 | 9 |
|
15 | | - |
16 | | -def try_model(path, data_dir='_data', zoo_dir='_zoo'): |
17 | | - model_info = get_model_info(path) |
18 | | - run_params = model_info.run_params |
19 | | - job = JobInfo(data_dir, zoo_dir, run_params['name'], run_params['target']) |
20 | | - raw_df = read_df(job.get_source_name()) |
21 | | - changes_df = to_changes(raw_df) |
22 | | - data_set = to_dataset(changes_df, run_params['k'], run_params['target'], model_info.model_class.DATA_WITH_BIAS) |
23 | | - |
24 | | - model = model_info.model_class(**model_info.model_params) |
25 | | - evaluator = Evaluator() |
26 | | - |
27 | | - with model.session(): |
28 | | - model.restore(model_info.path) |
29 | | - test_eval, test_stats = evaluator.eval(model, data_set) |
30 | | - info('Result:\n%sEval=%.6f\n' % (evaluator.stats_str(test_stats), test_eval)) |
31 | | - |
32 | | - |
33 | | -def predict_model(changes_df, path): |
34 | | - model_info = get_model_info(path) |
35 | | - run_params = model_info.run_params |
36 | | - model = model_info.model_class(**model_info.model_params) |
37 | | - x = to_dataset_for_prediction(changes_df[:-1], run_params['k'], model_info.model_class.DATA_WITH_BIAS) |
38 | | - x = x[-1:] |
39 | | - |
40 | | - with model.session(): |
41 | | - model.restore(model_info.path) |
42 | | - predicted = float(model.predict(x)) |
43 | | - info('Predicted change=%.5f' % predicted) |
44 | | - return predicted |
45 | | - |
46 | | - |
47 | | -def predict_all_models(changes_df, name, accept): |
48 | | - home_dir = '_zoo/%s' % name |
49 | | - models = [dir for dir in os.listdir(home_dir) if accept(dir)] |
50 | | - if not models: |
51 | | - info('No models found for %s' % name) |
52 | | - return |
53 | | - |
54 | | - predictions = [] |
55 | | - for model in models: |
56 | | - try: |
57 | | - value = predict_model(changes_df, os.path.join(home_dir, model)) |
58 | | - predictions.append(value) |
59 | | - except ModelNotAvailable as e: |
60 | | - warn('Cannot use model from "%s": class "%s" is not available not this system' % (model, e.model_class)) |
61 | | - warn('Most probable reason is that model dependencies are not met') |
62 | | - info() |
63 | | - info('Mean predicted value for %s: %.5f' % (name, np.mean(predictions))) |
64 | | - info() |
65 | | - |
66 | | - |
67 | 10 | def main(): |
68 | | - tickers, periods, targets = parse_command_line(default_tickers=[], |
| 11 | + tickers, periods, targets = parse_command_line(default_tickers=['BTC_ETH'], |
69 | 12 | default_periods=['day'], |
70 | 13 | default_targets=['high']) |
71 | 14 |
|
72 | 15 | for ticker in tickers: |
73 | 16 | for period in periods: |
74 | 17 | for target in targets: |
| 18 | + job = JobInfo('_data', '_zoo', name='%s_%s' % (ticker, period), target=target) |
75 | 19 | raw_df = poloniex.get_latest_data(ticker, period=period, depth=100) |
76 | | - changes_df = to_changes(raw_df) |
77 | | - predict_all_models(changes_df, '%s_%s' % (ticker, period), lambda name: name.startswith('%s_' % target)) |
| 20 | + result_df = predict_multiple(job, raw_df=raw_df, rows_to_predict=1) |
| 21 | + |
| 22 | + raw_df.set_index('date', inplace=True) |
| 23 | + result_df.rename(columns={"True": "Current-Truth"}, inplace=True) |
78 | 24 |
|
| 25 | + info('Latest chart info:', raw_df.tail(2), '', sep='\n') |
| 26 | + info('Prediction for "%s":' % target, result_df, '', sep='\n') |
79 | 27 |
|
80 | 28 | if __name__ == '__main__': |
81 | 29 | main() |
0 commit comments