{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## DISCRIM (QDA) - 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": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Since the Chi-Square value is significant at the 0.1 level, the within covariance matrices will be used in the discriminant function.\n", "Reference: Morrison, D.F. (1976) Multivariate Statistical Methods p252.\n" ] }, { "data": { "text/html": [ "
DISCRIM(method='quad', 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(method='quad', priors='prop')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from discrimintools import DISCRIM\n", "clf = DISCRIM(method=\"quad\") #warning can be disable using warn_message\n", "clf.fit(X,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Evaluation 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 15 0 15\n", "POIRE 0 0 20 20\n", "Total 17 15 20 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 100.000000 0.000000 100.0\n", "POIRE 0.000000 0.000000 100.000000 100.0\n", "Total 32.692308 28.846154 38.461538 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.000000 0.000000 0.0\n", "Priors 0.326923 0.288462 0.384615 NaN\n", "\n", "Classification Report for TYPE:\n", " precision recall f1-score support\n", "KIRSCH 1.0 1.0 1.0 17.0\n", "MIRAB 1.0 1.0 1.0 15.0\n", "POIRE 1.0 1.0 1.0 20.0\n", "accuracy 1.0 1.0 1.0 1.0\n", "macro avg 1.0 1.0 1.0 52.0\n", "weighted avg 1.0 1.0 1.0 52.0\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 : 100.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 : 0.0%\n" ] } ], "source": [ "#error rate\n", "print(\"Error rate : {}%\".format(100-100*round(clf.score(X,y),2)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### summary" ] }, { "cell_type": "code", "execution_count": 8, "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", "Within Covariance Matrix Information:\n", " Rank Natural Log of the Determinant\n", "Pooled 8 58.3267\n", "KIRSCH 8 49.0021\n", "MIRAB 8 48.9038\n", "POIRE 8 54.6744\n", "\n", "Test of Homogeneity of Within Covariance Matrices:\n", " Bartlett Value Num DF Den DF F value Pr>F Chi Sq. Value Pr>Chi2\n", "Box's M 350.5115 72 6010 3.679 0.0 269.0859 0.0\n", "\n", "Since the Chi-Square value is significant at the 0.1 level, the within covariance matrices has been used in the discriminant function.\n", "Reference: Morrison, D.F. (1976) Multivariate Statistical Methods p252.\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 15 0 15\n", "POIRE 0 0 20 20\n", "Total 17 15 20 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 100.0000 0.0000 100.0\n", "POIRE 0.0000 0.0000 100.0000 100.0\n", "Total 32.6923 28.8462 38.4615 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.0000 0.0000 0.0\n", "Priors 0.3269 0.2885 0.3846 NaN\n", "\n", "Classification Report for TYPE:\n", " precision recall f1-score support\n", "KIRSCH 1.0 1.0 1.0 17.0\n", "MIRAB 1.0 1.0 1.0 15.0\n", "POIRE 1.0 1.0 1.0 20.0\n", "accuracy 1.0 1.0 1.0 1.0\n", "macro avg 1.0 1.0 1.0 52.0\n", "weighted avg 1.0 1.0 1.0 52.0\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": 9, "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": 10, "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 12 5 17\n", "POIRE 0 2 17 19\n", "Total 14 14 22 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 70.588235 29.411765 100.0\n", "POIRE 0.000000 10.526316 89.473684 100.0\n", "Total 28.000000 28.000000 44.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.294118 0.105263 0.125327\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 14.00\n", "MIRAB 0.857143 0.705882 0.774194 17.00\n", "POIRE 0.772727 0.894737 0.829268 19.00\n", "accuracy 0.860000 0.860000 0.860000 0.86\n", "macro avg 0.876623 0.866873 0.867821 50.00\n", "weighted avg 0.865065 0.860000 0.858348 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 }