predictive modelling using Random Forest
predictive modelling using Random Forest
Loading saved threads...
PlatinumMaths · External communityPost link
External question — Data Science Stack Exchange
Author: PlatinumMaths
Original post: https://datascience.stackexchange.com/questions/97638
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I have created a
RandomForestClassifier
model in
skicit-learn
, but I am unsure how to finalize 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 there I worked out some technical indicators such as the
RSI
,
ROC
, stochastic oscillators (fast and slow), Moving average convergence/divergence (
macd
), on balance volume, and the 200 days moving average and used these as features (independent variables) in the
RandomForestClassifier
.
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), say for example for the next 10 days from the last day of data. I hope you understand what I mean.
Below is my code:
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
X_train2, X_test2, y_train2, y_test2 = train_test_split(data2.drop('prediction',axis=1),
data2.prediction,
test_size=0.02)
model1 = RandomForestClassifier(random_state=13)
model1.fit(X_train2,y_train2)
predicted = model1.predict(X_test2)
model1.score(X_test2, y_test2)
probabilities = model1.predict_proba(X_test)
probabilities
roc_auc_score(y_test2, probabilities[:,1])
confusion_matrix(y_test2, predicted)
Quote
Report
Brian Spiering · External communityPost link
External answer — Data Science Stack Exchange
Author: Brian Spiering
Original post: https://datascience.stackexchange.com/a/97710
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You have completed the training phase. The next phase is commonly called prediction / inference. That is when already trained model predicts labels for data.
Since you are using scikit-learn, you should the call
.predict
method. In your code, it will be
model1.predict(X)
where
X
is the numpy-like array that contains the data features. The result will be a predicted class (0 or 1) for each entry in
X
.
Quote
Report
Aaron Elliot · External communityPost link
External answer — Data Science Stack Exchange
Author: Aaron Elliot
Original post: https://datascience.stackexchange.com/a/97715
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
short answer, you cannot run your current model as described into the future. However, there is hope.
When building a forecasting model, you're typically using an "autoregressive" model, which is predicting, for example, the price in the future based on the price in the past. The reason this works is you are both predicting the next value, and generating the next input for your model. Let's say you have an autoregressive model
$F(X) \rightarrow Y$
that predict the price of the stock tomorrow (
$Y$
) based on the price today (
$X$
). You can run this into the future by recursive calls to your model.
$Y_{10}$
=
$F(Y_9)$
=
$F(F(Y_8))$
...
$= F(...F(X)$
.
So let's take a look at your case. Your model takes as input a variety of technical indicators
$\color{grey}{\text{RSI, ROC, stochastic oscillators (fast and slow), macd, on balance volume and the 200 day moving average}}$
So with your current model, in order to project into the future 10 days, we need an estimate of these features 9 days into the future.
Here are a couple solutions.
You could train a model to predicts 10 days into the future. So instead of training
$F(X_i) \rightarrow Y_{i+1}$
you train
$F(X_i) \rightarrow Y_{i+10}$
.
You could build a model to predict your features into the future.
$F(X_i) \rightarrow X_{i+1}$
, and then use these new estimated features with your current model. This is easier if
$\color{grey}{\text{RSI, ROC, stochastic oscillators, ...}}$
are derived from the price. In which case, we can estimate the price and then derive these values. Although, I'm not familiar with the finance to say if you can derive them from price.
Although, as a word of warning, you should think deeply about your model performance, and if it's fooling you. You may consider the following: Have you compared performance to any simple baseline models? Is there any "target leakage"? If you look at interpreting your model, do the patterns it found make sense?
Quote
Report
Malo · External communityPost link
External answer — Data Science Stack Exchange
Author: Malo
Original post: https://datascience.stackexchange.com/a/97902
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
In your case
X
is not the future data.
X
is today's data here as you try to predict tomorrow's increase or decrease of value
1
or
0
.
So
model1.predict(X)
with
X
being today's data, will give you the prediction
0
or
1
.
And this is it with your model
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Data Science Stack Exchange Author: Aaron Elliot Source score (net votes, not local likes): 0 Original post: https://datascience.stackexchange.com/a/97715 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. short answer, you cannot run your current model as described into the future. However, there is hope. When building a forecasting model, you're typically using an "autoregressive" model, which is predicting, for example, the price in the future based on the price in the past. The reason this works is you are both predicting the next value, and generating the next input for your model. Let's say you have an autoregressive model $F(X) \rightarrow Y$ that predict the price of the stock tomorrow ( $Y$ ) based on the price today ( $X$ ). You can run this into the future by recursive calls to your model. $Y_{10}$ = $F(Y_9)$ = $F(F(Y_8))$ ... $= F(...F(X)$ . So let's take a look at your case. Your model takes as input a variety of technical indicators $\color{grey}{\text{RSI, ROC, stochastic oscillators (fast and slow), macd, on balance volume and the 200 day moving average}}$ So with your current model, in order to project into the future 10 days, we need an estimate of these features 9 days into the future. Here are a couple solutions. You could train a model to predicts 10 days into the future. So instead of training $F(X_i) \rightarrow Y_{i+1}$ you train $F(X_i) \rightarrow Y_{i+10}$ . You could build a model to predict your features into the future. $F(X_i) \rightarrow X_{i+1}$ , and then use these new estimated features with your current model. This is easier if $\color{grey}{\text{RSI, ROC, stochastic oscillators, ...}}$ are derived from the price. In which case, we can estimate the price and then derive these values. Although, I'm not familiar with the finance to say if you can derive them from price. Although, as a word of warning, you should think deeply about your model performance, and if it's fooling you. You may consider the following: Have you compared performance to any simple baseline models? Is there any "target leakage"? If you look at interpreting your model, do the patterns it found make sense?
Checking account access…