Scikit Learn Tutorial – Part III


Visit: QuantInsti

Get up-to-speed with Part I and Part II.

Splitting the data into training and test sets 

It is always a good idea to split the data into training and test sets before we begin the modelling process to avoid the problem of overfitting. Overfitting occurs when the model has high predictive power for datapoints on which it is trained but generalizes poorly on out of sample or new data.

When we train a model using only the training set, we still have the test set to check the performance of our model. Thus, splitting of data helps us to evaluate the out of sample accuracy of our data as the test dataset is new or unseen by the model.

We can easily implement this split using the train_test_split() function from the model selection submodule of scikit learn:

In [9]:

#importing the train_test_split function from the model selection submodule of scikit learn library
from sklearn.model_selection import train_test_split
#Splitting X into Xtrain and Xtest while splitting y into ytrain and ytest
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.25, random_state=123, stratify=y)

We have set the value of test_size parameter in this function to 0.25. This means that we are keeping 25% of the data in the test set and will use the remaining 75% as the training set on which the model will be trained. We have also set an arbitrary “random state” (or seed) so that we can reproduce our results later on. Finally, we have stratified the sample by the target variable. This ensures our training set looks similar to the test set, making evaluation metrics more reliable.

Next, we will be using the datasets we created to illustrate the steps in model development in scikit learn.

Stay tuned for the next installment in which the author will discuss Steps for building a classification model with Scikit learn.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.