<jupyter_start><jupyter_text>In the previous chapters, we learned about the features (`X`), target (`y`), and the train-test split.As you have seen in countless movies, the hero always trains hard before he faces the competitor. Similarly, we have to train the ML algorithm too. So that when it goes in the real world, it will perform spectacularly. We will use the `X_train` and `y_train` to train the machine learning model. The model training is also referred to as "fitting" the model.After the model is fit, the `X_test` will be used with the trained machine learning model to get the predicted values (`y_pred`). Import Libraries<jupyter_code># For data manipulation
import pandas as pd

# Import sklearn's Random Forest Classifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression<jupyter_output><empty_output><jupyter_text>Read the DataThe target (`y`) and features (`X`) for the `train` and `test` dataset is read from the CSV files. Note that this data was prepared in the previous chapters.<jupyter_code># Define the path for the data files
path = "../data_modules/"

# Read the target and features of the training and testing data
X_train = pd.read_csv(
    path + "JPM_features_training_2017_2019.csv", index_col=0, 
    parse_dates=True)
X_test = pd.read_csv(
    path + "JPM_features_testing_2017_2019.csv", index_col=0, 
    parse_dates=True)
y_train = pd.read_csv(
    path + "JPM_target_training_2017_2019.csv", index_col=0, 
    parse_dates=True)
y_test = pd.read_csv(path + "JPM_target_testing_2017_2019.csv",
                     index_col=0, parse_dates=True)<jupyter_output><empty_output><jupyter_text>Select a Classification ModelFor illustration, we will use the `RandomForestClassifier`. Don't worry if you are unfamiliar with this ML model. It is not important to understand how the random forest classifier works at this time. We can use any other classification model in its place. What is important here is to learn how the `train_data` and `test_data` are used along with the ML model.The `RandomForestClassifier` model from the `sklearn` package is used to create the classification tree model. If you are very new to machine learning, you can skip the interpretation and understanding of these parameters for now.Syntax:```pythonRandomForestClassifier(n_estimators, max_features, max_depth, random_state)```Parameters:1. **n_estimators:** The number of trees in the forest.2. **max_features:** The number of features to consider when looking for the best split.3. **max_depth:** The maximum depth of a tree.4. **random_state:** Seed value for the randomised bootstrapping and feature selection. This is set to replicate results for subsequent runs.Returns:A `RandomForestClassifier` type object that can be fit on the test data, and then used for making forecasts.We have set the values for the parameters. These are for illustration and can be changed.<jupyter_code># Create the machine learning model
rf_model = RandomForestClassifier(
    n_estimators=3, max_features=3, max_depth=2, random_state=4)<jupyter_output><empty_output><jupyter_text>Train the ModelNow it is time for the model to learn from the `X_train` and `y_train`. We call the `fit` function of the model and pass the `X_train` and `y_train` datasets. Syntax:```pythonmodel.fit(X_train, y_train)```Parameters:1. **model:** The model (RandomForestClassifier) object.2. **X_train:** The features from the training dataset.3. **y_train:** The target from the training dataset.Returns:The `fit` function trains the model using the data passed to it. The trained model is stored in the model object where the `fit` function was applied.<jupyter_code># Fit the model on the training data
rf_model.fit(X_train, y_train['signal'])<jupyter_output><empty_output><jupyter_text>--- Forecast DataThe model is now ready to make forecasts. We can now pass the unseen data (`X_test`) to the model, and obtain the model predicted values (`y_pred`). To make a forecast, the `predict` function is called and the unseen data is passed as a parameter.Syntax:```pythonmodel.predict(X_test)```Parameters:1. **model:** The model (RandomForestClassifier) object.2. **X_test:** The features from the testing dataset.Returns:A `numpy` array of the predicted outputs is obtained.Let's make one prediction using the model. For illustration, we are using the first data point in the `X_test`.<jupyter_code># Get a sample day of the data from X_test
unseen_data_single_day = X_test.head(1)

# Preview the data
unseen_data_single_day<jupyter_output><empty_output><jupyter_text>This data is for 28th May, 2019. Let us pass this to the model and get the prediction.<jupyter_code># Get the prediction of a single day
single_day_prediction = rf_model.predict(unseen_data_single_day)

# Preview the prediction
single_day_prediction<jupyter_output><empty_output><jupyter_text>The predicted model output is 0. This means that the model is signalling to take no position on 28th May, 2019.Let's apply the model to all of the testing dataset.<jupyter_code># Use the model and predict the values for the test data
y_pred = rf_model.predict(X_test)

# Display the first five predictions
print("The first five predicted values", y_pred[:5])<jupyter_output>The first five predicted values [0 0 1 0 0]
{'fraction_non_alphanumeric': 0.07551789077212806, 'fraction_numerical': 0.012994350282485875, 'mean_word_length': 5.548705302096177, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '624143', 'license': 'CC0-1.0', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1489, 'n_words': 659, 'path': 'Python Codes/Chapter 5 - Training and Forecasting using Classification Model.ipynb', 'repo_name': 'quantra-go-algo/ml-trading-ebook', 'size': '5310'}