Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Scikit Learn Tutorial – Part II

Scikit Learn Tutorial – Part II

Posted October 30, 2019 at 10:02 am
Ashutosh Dave

See the requirements for working with data in scikit in Part I.

The Iris dataset

Scikit learn comes with a few standard datasets. One of those is the famous Iris dataset, which was first introduced by Statistician Sir R. A. Fisher in 1936.
This dataset is used to address a simple classification problem where we have to predict the species (Setosa, Versicolor or Virginica) of an iris flower, given a set of measurements (sepal length, sepal width, petal length and petal width) in centimetres.

The Iris dataset has 150 instances of Iris flowers for each of which we have the above four measurements (features) and the species code (response).
The response is in the form of a species code (0,1 and 2 for Setosa, Versicolor and Virginica respectively). This makes it easy for us to use it in scikit learn, as according to the above requirements both feature and response data should be numeric.

Let us get the Iris dataset from the “datasets” submodule of scikit learn library and save it in an object called “iris” using the following commands:

In [6]:

from sklearn import datasets
iris= datasets.load_iris()

The “iris” object belongs to the class Bunch i.e. it is a collection of various objects bunched together in a dictionary-like format. These objects include the feature matrix “data” and the target vector “target”.We will save these in objects X and y respectively:

In [7]:

#storing feature matrix in “X”
#storing target vector in “y”

Let us now check the type and shape of these two objects:

In [8]:

#Printing the type of X and y to check if they meet the NumPy
array requirement
print(” type of X:”,type(X),”\n”,”type of y:”,type(y))
#Printing the shape of X and y to check if their sizes are compatible
print(” shape of X:”,X.shape,”\n”,”shape of y:”,y.shape)

type of X: <class ‘numpy.ndarray’>
type of y: <class ‘numpy.ndarray’>
shape of X: (150, 4)
shape of y: (150,)

We see that X and y are of the type numpy ndarray, where X has 150 instances with four features and y is a one-dimensional array with 150 values.

Great! We see that all the three requirements for using X and y in scikit learn as the feature matrix and response vector are satisfied.

Stay tuned for the next installment in which the author will discuss splitting the data into training and test sets.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.