Указание тестовых строк для тестирования на истории и машинного обучения - PullRequest
1 голос
/ 16 июня 2020

Я хочу использовать машинное обучение, чтобы спрогнозировать движение цены актива. пока я получил данные и результаты. Теперь хочу протестировать модель. посылка очень проста: просто покупайте, когда прогнозируемое значение равно 1, и держите. Я хочу применить модель прогнозирования и перебрать тестовые строки снизу вверх до указанного числа, проверить, соответствует ли прогнозируемый результат соответствующей метке (метка здесь -1,1), а затем выполнить некоторые вычисления.

вот код:

def backtest():
    x = df[['open', 'high', 'low', 'close', 'vol']]
    y = df['label']
    z = np.array(df['log_ret'].values)

test_size = 366
rf = RandomForestClassifier(n_estimators = 100)
rf.fit(x[:-test_size],y[:-test_size])

invest_amount = 1000
trade_qty = 0
correct_count = 0

for i in range(1, test_size):
    if rf.predict(x[-i])[0] == y[-i]:
    correct_count += 1

if rf.predict(x[-i])[0] == 1:
    invest_return = invest_amount + (invest_amount * (z[-i]/100))
    trade_qty += 1


print('accuracy:', (correct_count/test_size)*100)
print('total trades:', trade_qty)
print('profits:', invest_return)

backtest()

Пока я застрял на этом:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: -1

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-29-feab89792f26> in <module>
     22 
     23 for i in range(1, test_size):
---> 24     if rf.predict(x[-i])[0] == y[-i]:
     25         correct_count += 1
     26 

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: -1

1 Ответ

1 голос
/ 16 июня 2020

Приведенный ниже код решает проблему с помощью нескольких модификаций:

def backtest():
    x = df[['open', 'high', 'low', 'close', 'vol']]
    y = df['label']
    z = np.array(df['log_ret'].values)

    test_size = 366
    rf = RandomForestClassifier(n_estimators = 100)
    rf.fit(x[:-test_size],y[:-test_size])

    invest_amount = 1000
    trade_qty = 0
    correct_count = 0

    for i in range(1, test_size)[::-1]:
        if rf.predict(x[x.index == i])[0] == y[i]:
            correct_count += 1

        if rf.predict(x[x.index == i])[0] == 1:
            invest_return = invest_amount + (invest_amount * (z[i]/100))
            trade_qty += 1

    print('accuracy:', (correct_count/test_size)*100)
    print('total trades:', trade_qty)
    print('profits:', invest_return)

backtest()

Объяснение изменений:

  1. Доступ к строке фрейма данных путем фильтрации index x[x.index == i];
  2. Изменение отрицательного индекса для обратного диапазона с меньшим количеством адаптаций range(1, test_size)[::-1];

Создание тестового примера:

import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier

data = {'open': np.random.rand(1000), 
        'high': np.random.rand(1000), 
        'low': np.random.rand(1000), 
        'close': np.random.rand(1000), 
        'vol': np.random.rand(1000),
        'log_ret': np.random.rand(1000),
        'label': np.random.choice([-1,1], 1000)}

df = pd.DataFrame(data)

Это дает следующий результат:

>> backtest()
accuracy: 99.72677595628416
total trades: 181
profits: 1006.8351193358026
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...