{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## DISCRIM (LDA) - alcools dataset" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#disable warnings\n", "from warnings import simplefilter, filterwarnings\n", "simplefilter(action='ignore', category=FutureWarning)\n", "filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### alcools dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 52 entries, 0 to 51\n", "Data columns (total 9 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 TYPE 52 non-null object \n", " 1 MEOH 52 non-null float64\n", " 2 ACET 52 non-null float64\n", " 3 BU1 52 non-null float64\n", " 4 BU2 52 non-null float64\n", " 5 ISOP 52 non-null int64 \n", " 6 MEPR 52 non-null float64\n", " 7 PRO1 52 non-null float64\n", " 8 ACAL 52 non-null float64\n", "dtypes: float64(7), int64(1), object(1)\n", "memory usage: 3.8+ KB\n", "None\n" ] } ], "source": [ "#vins dataset\n", "from discrimintools.datasets import load_alcools\n", "D = load_alcools(\"train\")\n", "print(D.info())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#split into X and y\n", "y, X = D[\"TYPE\"], D.drop(columns=[\"TYPE\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### instanciation and training" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
DISCRIM(priors='prop')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "DISCRIM(priors='prop')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from discrimintools import DISCRIM\n", "clf = DISCRIM()\n", "clf.fit(X,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Evaluation of prediction on training data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observation Profile:\n", " Read Used\n", "Number of Observations 52 52\n", "\n", "Number of Observations Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 17 0 0 17\n", "MIRAB 0 14 1 15\n", "POIRE 0 2 18 20\n", "Total 17 16 19 52\n", "\n", "Percent Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 100.000000 0.000000 0.000000 100.0\n", "MIRAB 0.000000 93.333333 6.666667 100.0\n", "POIRE 0.000000 10.000000 90.000000 100.0\n", "Total 32.692308 30.769231 36.538462 100.0\n", "Priors 0.326923 0.288462 0.384615 NaN\n", "\n", "Error Count Estimates for TYPE:\n", " KIRSCH MIRAB POIRE Total\n", "Rate 0.000000 0.066667 0.100000 0.057692\n", "Priors 0.326923 0.288462 0.384615 NaN\n", "\n", "Classification Report for TYPE:\n", " precision recall f1-score support\n", "KIRSCH 1.000000 1.000000 1.000000 17.000000\n", "MIRAB 0.875000 0.933333 0.903226 15.000000\n", "POIRE 0.947368 0.900000 0.923077 20.000000\n", "accuracy 0.942308 0.942308 0.942308 0.942308\n", "macro avg 0.940789 0.944444 0.942101 52.000000\n", "weighted avg 0.943699 0.942308 0.942499 52.000000\n" ] } ], "source": [ "#eval_predict function\n", "eval_train = clf.eval_predict(X,y,verbose=True)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy : 94.0%\n" ] } ], "source": [ "#score function\n", "print(\"Accuracy : {}%\".format(100*round(clf.score(X,y),2)))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Error rate : 6.0%\n" ] } ], "source": [ "#error rate\n", "print(\"Error rate : {}%\".format(100-100*round(clf.score(X,y),2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear Discriminant Function" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " KIRSCH MIRAB POIRE\n", "Constant -5.016453 -18.840685 -24.764879\n", "MEOH 0.003428 0.029028 0.033390\n", "ACET 0.006390 0.016413 0.007513\n", "BU1 -0.063681 0.405390 0.318047\n", "BU2 -0.000883 0.071352 0.114993\n", "ISOP 0.023082 0.029763 -0.008486\n", "MEPR 0.037494 -0.128942 0.061780\n", "PRO1 0.001971 -0.005413 -0.008318\n", "ACAL 0.066184 -0.226424 -0.130332\n" ] } ], "source": [ "#Linear Discriminant Function\n", "print(clf.coef_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### summary" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Discriminant Analysis - Results \n", "\n", "Summary Information:\n", " Infos Value DF DF value\n", "0 Total Sample Size 52 DF Total 51\n", "1 Variables 8 DF Within Classes 49\n", "2 Classes 3 DF Between Classes 2\n", "\n", "Class Level Information:\n", " Frequency Proportion Prior Probability\n", "KIRSCH 17 0.3269 0.3269\n", "MIRAB 15 0.2885 0.2885\n", "POIRE 20 0.3846 0.3846\n", "\n", "Pooled Covariance Matrix Information:\n", " Rank Natural Log of the Determinant\n", "Pooled 8 58.3267\n", "\n", "Linear Discriminant Function for TYPE:\n", " KIRSCH MIRAB POIRE\n", "Constant -5.0165 -18.8407 -24.7649\n", "MEOH 0.0034 0.0290 0.0334\n", "ACET 0.0064 0.0164 0.0075\n", "BU1 -0.0637 0.4054 0.3180\n", "BU2 -0.0009 0.0714 0.1150\n", "ISOP 0.0231 0.0298 -0.0085\n", "MEPR 0.0375 -0.1289 0.0618\n", "PRO1 0.0020 -0.0054 -0.0083\n", "ACAL 0.0662 -0.2264 -0.1303\n", "\n", "Classification Summary for Calibration Data:\n", "\n", "Observation Profile:\n", " Read Used\n", "Number of Observations 52 52\n", "\n", "Number of Observations Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 17 0 0 17\n", "MIRAB 0 14 1 15\n", "POIRE 0 2 18 20\n", "Total 17 16 19 52\n", "\n", "Percent Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 100.0000 0.0000 0.0000 100.0\n", "MIRAB 0.0000 93.3333 6.6667 100.0\n", "POIRE 0.0000 10.0000 90.0000 100.0\n", "Total 32.6923 30.7692 36.5385 100.0\n", "Priors 0.3269 0.2885 0.3846 NaN\n", "\n", "Error Count Estimates for TYPE:\n", " KIRSCH MIRAB POIRE Total\n", "Rate 0.0000 0.0667 0.1000 0.0577\n", "Priors 0.3269 0.2885 0.3846 NaN\n", "\n", "Classification Report for TYPE:\n", " precision recall f1-score support\n", "KIRSCH 1.0000 1.0000 1.0000 17.0000\n", "MIRAB 0.8750 0.9333 0.9032 15.0000\n", "POIRE 0.9474 0.9000 0.9231 20.0000\n", "accuracy 0.9423 0.9423 0.9423 0.9423\n", "macro avg 0.9408 0.9444 0.9421 52.0000\n", "weighted avg 0.9437 0.9423 0.9425 52.0000\n" ] } ], "source": [ "from discrimintools import summaryDISCRIM\n", "summaryDISCRIM(clf,detailed=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Evaluation of prediction on testing dataset\n", "\n", "#### Testing data" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 50 entries, 0 to 49\n", "Data columns (total 9 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 TYPE 50 non-null object \n", " 1 MEOH 50 non-null int64 \n", " 2 ACET 50 non-null int64 \n", " 3 BU1 50 non-null float64\n", " 4 BU2 50 non-null float64\n", " 5 ISOP 50 non-null int64 \n", " 6 MEPR 50 non-null int64 \n", " 7 PRO1 50 non-null int64 \n", " 8 ACAL 50 non-null float64\n", "dtypes: float64(3), int64(5), object(1)\n", "memory usage: 3.6+ KB\n" ] } ], "source": [ "#testining data\n", "DTest = load_alcools(\"test\")\n", "DTest.info()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observation Profile:\n", " Read Used\n", "Number of Observations 50 50\n", "\n", "Number of Observations Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 14 0 0 14\n", "MIRAB 0 14 3 17\n", "POIRE 1 5 13 19\n", "Total 15 19 16 50\n", "\n", "Percent Classified into TYPE:\n", "prediction KIRSCH MIRAB POIRE Total\n", "TYPE \n", "KIRSCH 100.000000 0.000000 0.000000 100.0\n", "MIRAB 0.000000 82.352941 17.647059 100.0\n", "POIRE 5.263158 26.315789 68.421053 100.0\n", "Total 30.000000 38.000000 32.000000 100.0\n", "Priors 0.326923 0.288462 0.384615 NaN\n", "\n", "Error Count Estimates for TYPE:\n", " KIRSCH MIRAB POIRE Total\n", "Rate 0.000000 0.176471 0.315789 0.172362\n", "Priors 0.326923 0.288462 0.384615 NaN\n", "\n", "Classification Report for TYPE:\n", " precision recall f1-score support\n", "KIRSCH 0.933333 1.000000 0.965517 14.00\n", "MIRAB 0.736842 0.823529 0.777778 17.00\n", "POIRE 0.812500 0.684211 0.742857 19.00\n", "accuracy 0.820000 0.820000 0.820000 0.82\n", "macro avg 0.827558 0.835913 0.828717 50.00\n", "weighted avg 0.820610 0.820000 0.817075 50.00\n" ] } ], "source": [ "#split into X and y\n", "yTest, XTest = DTest[\"TYPE\"], DTest.drop(columns=[\"TYPE\"])\n", "eval_test = clf.eval_predict(XTest,yTest,verbose=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.11.0" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }