1
$\begingroup$

I have created a random forest classification model in skicit-learn, but I am unsure how to finalise my forecast.

I have built the model and it is showing good results on the testing data. I get a mean accuracy of 85%. Predicting whether the stock price will go up or down. I used data from Yahoo finance consisting of open, high, low, close and volume. From these I worked out some technical indicators such as the RSI, ROC, stochastic oscillators (fast and slow), macd, on balance volume and the 200 day moving average and used these as features (independent variables) in the random forest classifier. I created another column, showing 1 when the price went up and 0 when the price went down. This column was used as the dependent variable. (the thing I want to predict)

The thing I am trying to find out now is how can I run the forecast into the unknown future? For now, I have split my data into training and testing, trained the model on the training dataset, and then used the predict function on the testing dataset. The model performs well and after a little more tweaking it can be used.

But how? I can't seem to find anywhere in the sklearn random forest documentation about how to actually run the forecast for the future (not on the testing data). I hope you understand what I mean. Below is my code.

X_train2, X_test2, y_train2, y_test2 =
train_test_split(data2.drop('prediction',axis=1),data2.prediction,test_size=0.02)

from sklearn.ensemble import RandomForestClassifier
model1 = RandomForestClassifier(random_state=13)
model1.fit(X_train2,y_train2)

predicted = model1.predict(X_test2)
model1.score(X_test2, y_test2)

from sklearn.metrics import roc_auc_score 
probabilities = model1.predict_proba(X_test)
probabilities

roc_auc_score(y_test2, probabilities[:,1])

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test2, predicted 
$\endgroup$
5
  • $\begingroup$ you have to prepare new dataset in the same format as the training dataset and then you just use predict function on that i.e. run model1.predict(new_dataset) $\endgroup$
    – emot
    Jul 9 at 20:42
  • $\begingroup$ @emot Thanks for the comment. What sort of dataset would that be? You mean like an empty dataframe? Because I want to predict the unknown future, e.g the next 10 days, I don't have any future data... Also, if I run model1.predict(new_dataset).. The prediction won't be based on the data the model has been trained on right? $\endgroup$ Jul 9 at 20:51
  • $\begingroup$ I am not sure I know what you mean. To build the model you pick the predicting variables X_train that you know the values of or you can easily calculate them. The model then learns on what was given. If you don't have future variables the model doesn't know how to predict anything. If I understand you properly, then what you have is: $(RSI_t, ROC_t_, ... )$ dataset for day $t$ and the outcome variable is for day $t$ as well. What you should do is use the variables like RSI but like from 10 days ago to predict the outcome. $\endgroup$
    – emot
    Jul 9 at 21:08
  • $\begingroup$ So your training dataset should be like: $(RSI_{t-10}, ROC_{t-10},..)$ and the y_train should be $y_t$ (so y is 10 days ahead). Then you train your model on that dataset and can easily forecast the future, just prepare dataset with current RSI, ROC etc. to forecast y 10 days ahead. $\endgroup$
    – emot
    Jul 9 at 21:09
  • $\begingroup$ @emot it won't be a future forecast since the train test split is random. The testing data is not necessarily the latest data... That could be a potential way, if I arranged the data differently, so that the test data is the latest data. But still that doesn't allow me to specify the number of days. I think there must be another more specific way! :) $\endgroup$ Jul 10 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.