.. _user_guide.machine_learning.introduction: ================================= 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 `__. .. code-block:: python 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() .. ipython:: python :suppress: :okwarning: import vastorbit as vo vo.set_option("plotting_lib", "plotly") churn = vo.read_csv("/Users/badr.ouali/Documents/VastOrbit-master/docs/source/_static/website/user_guides/data_exploration/churn.csv") from vastorbit.machine_learning.vast import LinearRegression model = LinearRegression() churn = churn.dropna(columns = ["tenure", "TotalCharges"]) model.fit(churn, ["tenure"], "TotalCharges") fig = model.plot() fig.write_html("/Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_1.html") .. raw:: html :file: /Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_1.html 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. .. code-block:: from vastorbit.datasets import load_iris iris = load_iris() iris.one_hot_encode() .. ipython:: python :suppress: :okwarning: from vastorbit.datasets import load_iris iris = load_iris() res = iris.one_hot_encode() html_file = open("/Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_table_introduction_1.html", "w") html_file.write(res._repr_html_()) html_file.close() .. raw:: html :file: /Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_table_introduction_1.html .. code-block:: python from vastorbit.machine_learning.vast import LinearSVC model = LinearSVC(max_iter = 1000) model.fit(iris, ["PetalLengthCm", "SepalLengthCm"], "Species_Iris-setosa") model.plot() .. ipython:: python :suppress: :okwarning: from vastorbit.machine_learning.vast import LinearSVC model = LinearSVC(max_iter = 1000) model.fit(iris, ["PetalLengthCm", "SepalLengthCm"], "Species_Iris-setosa") fig = model.plot() fig.write_html("/Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_2.html") .. raw:: html :file: /Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_2.html 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 (:py:mod:`~vastorbit.machine_learning.vast.cluster.KMeans`, :py:mod:`~vastorbit.machine_learning.vast.cluster.DBSCAN`, etc.) or to detect anomalies (:py:mod:`~vastorbit.machine_learning.vast.LocalOutlierFactor`, ``Z-Score`` Techniques...). In particular, they're useful for finding patterns in data without labels. For example, let's use a :py:mod:`~vastorbit.machine_learning.vast.cluster.KMeans` algorithm to create different clusters on the Iris dataset. Each cluster will represent a flower's species. .. code-block:: python from vastorbit.machine_learning.vast import KMeans model = KMeans(n_clusters = 3) model.fit(iris, ["PetalLengthCm", "SepalLengthCm"]) model.plot() .. ipython:: python :suppress: :okwarning: from vastorbit.machine_learning.vast import KMeans model = KMeans(n_clusters = 3) model.fit(iris, ["PetalLengthCm", "SepalLengthCm"]) fig = model.plot() fig.write_html("/Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_3.html") .. raw:: html :file: /Users/badr.ouali/Documents/VastOrbit-master/docs/figures/ug_ml_plot_introduction_3.html In this section, we went over a few of the many ML algorithms available in vastorbit. In the next lesson, we'll go over :ref:`user_guide.machine_learning.time_series` .. ipython:: python :suppress: from vastorbit._utils._sql._sys import purge_memory purge_memory()