# Scikit-learn introduction¶

This lesson will illustrate some of the main features that `scikit-learn`

library provides.

## What is scikit-learn?¶

Scikit-learn, or *sklearn*, is an open source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection and evaluation, and many other utilities.

- Simple and efficient tools for predictive data analysis
- Accessible to everybody, and reusable in various contexts
- Built on NumPy, SciPy, and matplotlib
- Open source, commercially usable - BSD license

### Where did it come from?¶

Scikit-learn was initially developed by David Cournapeau as a Google summer of code project in 2007. Later Matthieu Brucher joined the project and started to use it as apart of his thesis work. In 2010 INRIA got involved and the first public release (v0.1 beta) was published in late January 2010.

## What can I do with scikit-learn?¶

scikit-learn offers various important features for machine learning such as classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to inter-operate with the python numerical and scientific libraries like NumPy and SciPy.

It provides a large list of tools and algorithms, divided in 6 categories:

**Classification**, to identify which category an object belongs to.**Regression**, to predict a continuous-valued attribute associated with an object.**Clustering**, to perform automatic grouping of similar objects into sets.**Dimensionality reduction**, to reduce the number of random variables to consider.**Model selection**, to compare, validate and choose parameters and models.**Preprocessing**, to extract and normalize features.

## Estimator Interface¶

All implemented algorithms in scikit-learn, whether preprocessing, supervised learning, or unsupervised learning algorithms, are implemented as classes.

These classes are called **Estimators** in scikit-learn. To apply an algorithm, you first have to instantiate an object of the particular class.

For example, let's see how can we perform a Linear Regression:

```
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model
```

The estimator class contains the algorithm, and also stores the model that is learned from data using the algorithm.

You should set any parameters of the model when constructing the model object. These parameters include regularization, complexity control, number of clusters to find, etc.

### Fit method¶

`estimator.fit(x_train, [y_train])`

All estimators have a ** fit** method, which is used to build the model.

The `fit`

method always requires as its first argument the data `X`

, represented as a NumPy array or a SciPy sparse matrix, where each row represents a single data point. The data `X`

is always assumed to be a NumPy array or SciPy sparse matrix that has continuous (floating-point) entries.

Supervised algorithms also require a `y`

argument, which is a one-dimensional NumPy array containing target values (labels) for regression or classification (i.e., the known output labels or responses).

```
from sklearn.datasets import make_regression
X, y = make_regression(n_samples=100,
n_features=1,
noise=25)
X.shape, y.shape
```

```
X[0], y[0]
```

```
X[1], y[1]
```

```
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(14,6))
plt.scatter(X, y, color='black')
```

```
model.fit(X, y)
```

### Predict method¶

`estimator.predict(X_test)`

There are two main ways to apply a learned model in scikit-learn.

To create a prediction in the form of a new output like $y$, you use the `predict`

method.

To create a new representation of the input data, you use the `transform`

method.

We will cover this

`transform`

method in detail on upcoming lessons.

```
y_pred = model.predict(X)
y_pred
```

```
X[0], y[0], y_pred[0]
```

```
X[1], y[1], y_pred[1]
```

```
plt.figure(figsize=(14,6))
plt.scatter(X, y, color='black')
plt.plot(X, y_pred, color='red', linewidth=2)
```

### Score method¶

`estimator.score(X_test, y_test)`

Additionally, all supervised models have a `score`

method that allows an evaluation of the model.

```
model.score(X, y)
```

## Choosing the right estimator¶

Often the hardest part of solving a machine learning problem can be finding the right estimator for the job.

Different estimators are better suited for different types of data and different problems.

The flowchart below is designed to give users a bit of a rough guide on how to approach problems with regard to which estimators to try on your data.