Python SciKit Learn Tutorial With Examples

Scikit Learn

Scikit-learn is a machine learning library for Python. It features several regression, classification and clustering algorithms including SVMs, gradient boosting, k-means, random forests and DBSCAN. It is designed to work with Python Numpy and SciPy.

The scikit-learn project kicked off as a Google Summer of Code (also known as GSoC) project by David Cournapeau as scikits.learn. It gets its name from “Scikit”, a separate third-party extension to SciPy.

Python Scikit-learn

Scikit is written in Python (most of it) and some of its core algorithms are written in Cython for even better performance.

Scikit-learn is used to build models and it is not recommended to use it for reading, manipulating and summarizing data as there are better frameworks available for the purpose.

It is open source and released under BSD license.

Install Scikit Learn

Scikit assumes you have a running Python 2.7 or above platform with NumPY (1.8.2 and above) and SciPY (0.13.3 and above) packages on your device. Once we have these packages installed we can proceed with the installation.

For pip installation, run the following command in the terminal:

If you like conda, you can also use the conda for package installation, run the following command:

Using Scikit-Learn

Once you are done with the installation, you can use scikit-learn easily in your Python code by importing it as:

Scikit Learn Loading Dataset

Let’s start with loading a dataset to play with. Let’s load a simple dataset named Iris. It is a dataset of a flower, it contains 150 observations about different measurements of the flower. Let’s see how to load the dataset using scikit-learn.

We are printing shape of data for ease, you can also print whole data if you wish so, running the codes gives an output like this:

Python SciKit Learn Tutorial

Scikit Learn SVM – Learning and Predicting

Now we have loaded data, let’s try learning from it and predict on new data. For this purpose we have to create an estimator and then call its fit method.

Here is what we get when we run this script:

python-scikit linear regression

Scikit Learn Linear Regression

Creating various models is rather simple using scikit-learn. Let’s start with a simple example of regression.

Running the model should return a point that can be plotted on the same line:

python-scikit linear regression

k-Nearest neighbour classifier

Let’s try a simple classification algorithm. This classifier uses an algorithm based on ball trees to represent the training samples.

Let’s run the classifier and check results, the classifier should return 0. Let’s try the example:

Python SciKit Learn Tutorial

K-means clustering

This is the simplest clustering algorithm. The set is divided into ‘k’ clusters and each observation is assigned to a cluster. This is done iteratively until the clusters converge.

We will create one such clustering model in the following program:

On running the program we’ll see separate clusters in the list. Here is the output for above code snippet:

Python SciKit Learn Tutorial


In this tutorial, we have seen that Scikit-Learn makes it easy to work with several machine learning algorithms. We have seen examples of Regression, Classification and Clustering.

Scikit-Learn is still in development phase and being developed and maintained by volunteers but is very popular in community. Go and try your own examples.

By admin

Leave a Reply

%d bloggers like this: