{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#

Multiclass Classification

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Link de estudo:\n", "\n", "* [pycaret 3.2.0](https://pypi.org/project/pycaret/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 Iris-setosa\n", "1 4.9 3.0 1.4 0.2 Iris-setosa\n", "2 4.7 3.2 1.3 0.2 Iris-setosa\n", "3 4.6 3.1 1.5 0.2 Iris-setosa\n", "4 5.0 3.6 1.4 0.2 Iris-setosa" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from pycaret.datasets import get_data\n", "\n", "\n", "dataset= get_data(\"iris\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(150, 5)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.shape" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset[\"species\"].unique()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
1456.73.05.22.3Iris-virginica
1466.32.55.01.9Iris-virginica
1476.53.05.22.0Iris-virginica
1486.23.45.42.3Iris-virginica
1495.93.05.11.8Iris-virginica
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "145 6.7 3.0 5.2 2.3 Iris-virginica\n", "146 6.3 2.5 5.0 1.9 Iris-virginica\n", "147 6.5 3.0 5.2 2.0 Iris-virginica\n", "148 6.2 3.4 5.4 2.3 Iris-virginica\n", "149 5.9 3.0 5.1 1.8 Iris-virginica" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Mostra as 5 últimas linhas:\n", "dataset.tail()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_width
count150.000000150.000000150.000000150.000000
mean5.8433333.0540003.7586671.198667
std0.8280660.4335941.7644200.763161
min4.3000002.0000001.0000000.100000
25%5.1000002.8000001.6000000.300000
50%5.8000003.0000004.3500001.300000
75%6.4000003.3000005.1000001.800000
max7.9000004.4000006.9000002.500000
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width\n", "count 150.000000 150.000000 150.000000 150.000000\n", "mean 5.843333 3.054000 3.758667 1.198667\n", "std 0.828066 0.433594 1.764420 0.763161\n", "min 4.300000 2.000000 1.000000 0.100000\n", "25% 5.100000 2.800000 1.600000 0.300000\n", "50% 5.800000 3.000000 4.350000 1.300000\n", "75% 6.400000 3.300000 5.100000 1.800000\n", "max 7.900000 4.400000 6.900000 2.500000" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset.describe()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
736.12.84.71.2Iris-versicolor
185.73.81.70.3Iris-setosa
1187.72.66.92.3Iris-virginica
786.02.94.51.5Iris-versicolor
766.82.84.81.4Iris-versicolor
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "73 6.1 2.8 4.7 1.2 Iris-versicolor\n", "18 5.7 3.8 1.7 0.3 Iris-setosa\n", "118 7.7 2.6 6.9 2.3 Iris-virginica\n", "78 6.0 2.9 4.5 1.5 Iris-versicolor\n", "76 6.8 2.8 4.8 1.4 Iris-versicolor" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Dados de Treinamento:\n", "\n", "data_train = dataset.sample(frac=0.9, random_state=42)\n", "\n", "data_train.head()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(135, 5)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_train.shape" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
145.84.01.20.2Iris-setosa
205.43.41.70.2Iris-setosa
526.93.14.91.5Iris-versicolor
716.12.84.01.3Iris-versicolor
746.42.94.31.3Iris-versicolor
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "14 5.8 4.0 1.2 0.2 Iris-setosa\n", "20 5.4 3.4 1.7 0.2 Iris-setosa\n", "52 6.9 3.1 4.9 1.5 Iris-versicolor\n", "71 6.1 2.8 4.0 1.3 Iris-versicolor\n", "74 6.4 2.9 4.3 1.3 Iris-versicolor" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Dados de Teste:\n", "\n", "data_test = dataset.drop(data_train.index)\n", "\n", "data_test.head()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(15, 5)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_test.shape" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
06.12.84.71.2Iris-versicolor
15.73.81.70.3Iris-setosa
27.72.66.92.3Iris-virginica
36.02.94.51.5Iris-versicolor
46.82.84.81.4Iris-versicolor
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 6.1 2.8 4.7 1.2 Iris-versicolor\n", "1 5.7 3.8 1.7 0.3 Iris-setosa\n", "2 7.7 2.6 6.9 2.3 Iris-virginica\n", "3 6.0 2.9 4.5 1.5 Iris-versicolor\n", "4 6.8 2.8 4.8 1.4 Iris-versicolor" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tratando os índices dos dados de Treinamento:\n", "\n", "data_train.reset_index(drop=True, inplace=True)\n", "\n", "data_train.head()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.84.01.20.2Iris-setosa
15.43.41.70.2Iris-setosa
26.93.14.91.5Iris-versicolor
36.12.84.01.3Iris-versicolor
46.42.94.31.3Iris-versicolor
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.8 4.0 1.2 0.2 Iris-setosa\n", "1 5.4 3.4 1.7 0.2 Iris-setosa\n", "2 6.9 3.1 4.9 1.5 Iris-versicolor\n", "3 6.1 2.8 4.0 1.3 Iris-versicolor\n", "4 6.4 2.9 4.3 1.3 Iris-versicolor" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tratando os índices dos dados de Teste:\n", "\n", "data_test.reset_index(drop=True, inplace=True)\n", "\n", "data_test.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importando nosso modelo de `Classificação`:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 DescriptionValue
0Session id123
1Targetspecies
2Target typeMulticlass
3Target mappingIris-setosa: 0, Iris-versicolor: 1, Iris-virginica: 2
4Original data shape(135, 5)
5Transformed data shape(135, 5)
6Transformed train set shape(94, 5)
7Transformed test set shape(41, 5)
8Numeric features4
9PreprocessTrue
10Imputation typesimple
11Numeric imputationmean
12Categorical imputationmode
13Fold GeneratorStratifiedKFold
14Fold Number10
15CPU Jobs-1
16Use GPUFalse
17Log ExperimentFalse
18Experiment Nameclf-default-name
19USIc071
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from pycaret.classification import *\n", "\n", "\n", "Mult_clf = setup(data=data_train,\n", " target=\"species\",\n", " session_id=123\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "best_Model = compare_models()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NOTA:\n", "\n", "Tive que executar no Google Colab, porque demorou muito para obter os resultados.\n", "\n", "Pedir permissão para meu arquivo compartilhado, [Aqui](https://colab.research.google.com/drive/1bELHHfpLFZ7SyRifMpn_4h34Nir1y37D#scrollTo=NelzMSccoXnI)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv_pycaret", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }