10 - Generative Models for Classification Code Demo

Author

Dr. Cheng-Han Yu

1 R implementation

Code

Attaching package: 'MASS'
The following object is masked from 'package:ISLR2':

    Boston
Code
library(e1071)
data("Default")
Code
lda_fit <- MASS::lda(default ~ balance, data = Default)
lda_pred <- predict(lda_fit, data = Default)
table(lda_pred$class, Default$default, dnn = c("Predicted", "Actual"))
         Actual
Predicted   No  Yes
      No  9643  257
      Yes   24   76
Code
qda_fit <- MASS::qda(default ~ balance, data = Default)
qda_pred <- predict(qda_fit, data = Default)
table(qda_pred$class, Default$default, dnn = c("Predicted", "Actual"))
         Actual
Predicted   No  Yes
      No  9639  246
      Yes   28   87
Code
nb_fit <- e1071::naiveBayes(default ~ balance, data = Default)
nb_pred <- predict(nb_fit, Default)
(nb_conf <- table(nb_pred, Default$default))
       
nb_pred   No  Yes
    No  9639  246
    Yes   28   87

2 Python implementation

Code
import pandas as pd
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix
Code
Default = pd.read_csv("../data/Default.csv")
X = Default[['balance']]
y = Default['default']
Code
lda = LinearDiscriminantAnalysis()
lda.fit(X, y)
LinearDiscriminantAnalysis()
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.
Code
lda_pred = lda.predict(X)
lda_conf_df = pd.DataFrame(
    confusion_matrix(y_true=y, y_pred=lda_pred), 
    index=[f"Actual: {cls}" for cls in lda.classes_],
    columns=[f"Predicted: {cls}" for cls in lda.classes_]
)
lda_conf_df
             Predicted: No  Predicted: Yes
Actual: No            9643              24
Actual: Yes            257              76
Code
qda = QuadraticDiscriminantAnalysis()
qda.fit(X, y)
QuadraticDiscriminantAnalysis()
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.
Code
qda_pred = qda.predict(X)
qda_conf_df = pd.DataFrame(
    confusion_matrix(y, qda_pred), 
    index=[f"Actual: {cls}" for cls in qda.classes_],
    columns=[f"Predicted: {cls}" for cls in qda.classes_]
)
qda_conf_df
             Predicted: No  Predicted: Yes
Actual: No            9639              28
Actual: Yes            246              87
Code
nb = GaussianNB()
nb.fit(X, y)
GaussianNB()
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.
Code
nb_pred = nb.predict(X)
nb_conf_df = pd.DataFrame(
    confusion_matrix(y, nb_pred),
    index=[f"Actual: {cls}" for cls in nb.classes_],
    columns=[f"Predicted: {cls}" for cls in nb.classes_]
)
nb_conf_df
             Predicted: No  Predicted: Yes
Actual: No            9639              28
Actual: Yes            246              87