This lesson is being piloted (Beta version)

Introduction to Scikit Learn

Overview

Teaching: 40 min
Exercises: 0 min
Questions
  • What is Scikit Learn

Objectives
  • Master Scikit Learn for Machine Learning

2.1 What is Scikit-Learn

image

  - data splitting
  - pre-processing
  - feature selection
  - model tuning using resampling
  - variable importance estimation
  as well as other functionality.
  

2.2 Install sklearn

We have installed kernel ML_SKLN which contains the scikit-learn package in Palmetto. However for new conda environment installation, here is the command: $ pip3 install -U scikit-learn

2.3 Pre-processing using sklearn

There are several steps that we will use sklearn for. For preprocessing raw data, we gonna use sklearn in these tasks:

2.3.1 Pre-processing with missing value

knnImpute can also be used to fill in missing value

from sklearn.impute import KNNImputer
imputer = KNNImputer(n_neighbors=2, weights="uniform")
data_knnimpute = pd.DataFrame(imputer.fit_transform(data_df))

Note:

2.3.2 Pre-processing with Transforming data

2.3.2.1 Using Standardization

image

2.3.2.2 Using scaling with predefine range

Transform features by scaling each feature to a given range. This estimator scales and translates each feature individually such that it is in the given range on the training set, e.g. between zero and one. Formulation for this is:

X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + min
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
#By default, it scales for (0, 1) range
data_scaler = pd.DataFrame(scaler.fit_transform(data3))

2.3.2.3 Using Box-Cox Transformation

import matplotlib.pyplot as plt
ax1 = plt.subplot(1,2,1)
ax1.hist(data3["Ozone"])
ax1.set_title("Original probability")
ax1.set_xlabel('Ozone')
ax1.set_ylabel('Count')
ax2 = plt.subplot(1,2,2)
ax2.hist(data_BxCx["Ozone"])
ax2.set_title("Box-Cox Transformation")
ax2.set_xlabel('Ozone')
ax2.set_ylabel('Count')
plt.show()

image

Key Points

  • sklearn