Loading...

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
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
123
species_Iris-setosa
Bool
100%
123
species_Iris-versicolor
Bool
100%
15.13.51.40.2Iris-setosa10
24.93.01.40.2Iris-setosa10
34.73.21.30.2Iris-setosa10
44.63.11.50.2Iris-setosa10
55.03.61.40.2Iris-setosa10
65.43.91.70.4Iris-setosa10
74.63.41.40.3Iris-setosa10
85.03.41.50.2Iris-setosa10
94.42.91.40.2Iris-setosa10
104.93.11.50.1Iris-setosa10
115.43.71.50.2Iris-setosa10
124.83.41.60.2Iris-setosa10
134.83.01.40.1Iris-setosa10
144.33.01.10.1Iris-setosa10
155.84.01.20.2Iris-setosa10
165.74.41.50.4Iris-setosa10
175.43.91.30.4Iris-setosa10
185.13.51.40.3Iris-setosa10
195.73.81.70.3Iris-setosa10
205.13.81.50.3Iris-setosa10
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