Introduction to Machine Learning¶
- One of the last stages of the data science life cycle is the Data Modeling. Machine learning algorithms are a set of statistical techniques that build mathematical models from training data. These algorithms come in two types:
Supervised: these algorithms are used when we want to predict a response column.
Unsupervised: these algorithms are used when we want to detect anomalies or when we want to segment the data. No response column is needed.
Supervised Learning¶
- Supervised Learning techniques map an input to an output based on some example dataset. This type of learning consists of two main types:
Regression: The Response is numerical (
Linear Regression,SVM Regression,RF Regression…).Classification: The Response is categorical (
Gradient Boosting,Naive Bayes,Logistic Regression…).
For example, predicting the total charges of a Telco customer using their tenure would be a type of regression. The following code is drawing a linear regression using the TotalCharges as a function of the tenure in the telco churn dataset.
import vastorbit as vo
churn = vo.read_csv("churn.csv")
from vastorbit.machine_learning.vast import LinearRegression
model = LinearRegression()
model.fit(churn, ["tenure"], "TotalCharges")
model.plot()
In contrast, when we have to predict a categorical column, we’re dealing with classification.
In the following example, we use a Linear Support Vector Classification (SVC) to predict the species of a flower based on its petal and sepal lengths.
from vastorbit.datasets import load_iris
iris = load_iris()
iris.one_hot_encode()
123 sepallengthcmDecimal(5,2) | 123 sepalwidthcmDecimal(5,2) | 123 petallengthcmDecimal(5,2) | 123 petalwidthcmDecimal(5,2) | Abc speciesVarchar(30) | 123 species_Iris-setosaBool | 123 species_Iris-versicolorBool | |
|---|---|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | 1 | 0 |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | 1 | 0 |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | 1 | 0 |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | 1 | 0 |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | 1 | 0 |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | Iris-setosa | 1 | 0 |
| 7 | 4.6 | 3.4 | 1.4 | 0.3 | Iris-setosa | 1 | 0 |
| 8 | 5.0 | 3.4 | 1.5 | 0.2 | Iris-setosa | 1 | 0 |
| 9 | 4.4 | 2.9 | 1.4 | 0.2 | Iris-setosa | 1 | 0 |
| 10 | 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa | 1 | 0 |
| 11 | 5.4 | 3.7 | 1.5 | 0.2 | Iris-setosa | 1 | 0 |
| 12 | 4.8 | 3.4 | 1.6 | 0.2 | Iris-setosa | 1 | 0 |
| 13 | 4.8 | 3.0 | 1.4 | 0.1 | Iris-setosa | 1 | 0 |
| 14 | 4.3 | 3.0 | 1.1 | 0.1 | Iris-setosa | 1 | 0 |
| 15 | 5.8 | 4.0 | 1.2 | 0.2 | Iris-setosa | 1 | 0 |
| 16 | 5.7 | 4.4 | 1.5 | 0.4 | Iris-setosa | 1 | 0 |
| 17 | 5.4 | 3.9 | 1.3 | 0.4 | Iris-setosa | 1 | 0 |
| 18 | 5.1 | 3.5 | 1.4 | 0.3 | Iris-setosa | 1 | 0 |
| 19 | 5.7 | 3.8 | 1.7 | 0.3 | Iris-setosa | 1 | 0 |
| 20 | 5.1 | 3.8 | 1.5 | 0.3 | Iris-setosa | 1 | 0 |
from vastorbit.machine_learning.vast import LinearSVC
model = LinearSVC(max_iter = 1000)
model.fit(iris, ["PetalLengthCm", "SepalLengthCm"], "Species_Iris-setosa")
model.plot()
When we have more than two categories, we use the expression Multiclass Classification instead of Classification.
Unsupervised Learning¶
These algorithms are to used to segment the data (KMeans, DBSCAN, etc.) or to detect anomalies (LocalOutlierFactor, Z-Score Techniques…). In particular, they’re useful for finding patterns in data without labels. For example, let’s use a KMeans algorithm to create different clusters on the Iris dataset. Each cluster will represent a flower’s species.
from vastorbit.machine_learning.vast import KMeans
model = KMeans(n_clusters = 3)
model.fit(iris, ["PetalLengthCm", "SepalLengthCm"])
model.plot()
In this section, we went over a few of the many ML algorithms available in vastorbit.
In the next lesson, we’ll go over Time Series