Machine Learning and Sklearn: An Introduction

Welcome! This lesson paves your path toward understanding machine learning and the powerful Python library, sklearn. Machine learning, an application of artificial intelligence, enables systems to learn and improve without being explicitly programmed. It plays a key role in various sectors, such as autonomous vehicles, voice recognition systems, and recommendation engines.

Suppose you aim to predict housing prices as an illustration. This scenario constitutes a standard supervised learning problem wherein you train your model using past data. With sklearn, you can import the data, preprocess it, select an algorithm (like linear regression), train the model with the training data, and make predictions. All these steps can be accomplished without manually implementing algorithms.

Importing the Iris Dataset

Datasets form the backbone of machine learning. In this course, we'll use the Iris dataset, which consists of measurements — namely, sepal length, sepal width, petal length, and petal width — for 150 flowers representing three species of iris.

Sklearn provides an easy-to-use load_iris function to import the Iris dataset. Let's see how it works:

from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.target

Here, the load_iris() function loads the dataset and assigns it to the iris variable. We then separate the dataset into X for features and y for the target.

Furthermore, you can print the description of the dataset for more detailed insight using the DESCR attribute as follows:

print(iris.DESCR)

Output:

.. _iris_dataset:

Iris plants dataset
--------------------

**Data Set Characteristics:**

    :Number of Instances: 150 (50 in each of three classes)
    :Number of Attributes: 4 numeric, predictive attributes and the class
    :Attribute Information:
        - sepal length in cm
        - sepal width in cm
        - petal length in cm
        - petal width in cm
        - class:
                - Iris-Setosa
                - Iris-Versicolour
                - Iris-Virginica
                
    :Missing Attribute Values: None
    :Class Distribution: 33.3% for each of 3 classes.
    :Creator: R.A. Fisher
    :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
    :Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

The mean sepal length is 5.843, which is close to the mean of all other
attributes. The petal width varies from 0.1cm to 2.5cm, indicating a large
range.

This is perhaps the best-known database to be found in the
pattern recognition literature.  Fisher's paper is a classic in the field and
is referenced frequently to this day. 

This code prints a detailed description of the dataset and its attributes.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal