# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="3yzVZcFVW4J3" colab_type="text"
# # `nnetsauce`'s Ridge2 logistic regression
#
# This notebook demonstrates the use of `nnetsauce`'s Ridge2 logistic regression. It's a quasi-randomized classification model with 2 shrinkage parameters: one on input data, another one on the hidden layer. For more details, report to: https://www.researchgate.net/publication/334706878_Multinomial_logistic_regression_using_quasi-randomized_networks

# + id="I2BYGMHm_YT4" colab_type="code" colab={}
pip install git+https://github.com/thierrymoudiki/nnetsauce.git

# + id="VSpcAAgc_Hyp" colab_type="code" colab={}
import nnetsauce as ns
import numpy as np
from sklearn.datasets import load_digits, load_breast_cancer, load_wine, load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

# + id="7cB4SMlzq-z2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 208} outputId="b4647623-fa50-4d79-e356-3cb43c3c3499"
# dataset no. 1 ----------

breast_cancer = load_breast_cancer()
X = breast_cancer.data
y = breast_cancer.target

# split data into training test and test set
np.random.seed(123)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# create the model with nnetsauce
fit_obj = ns.Ridge2Classifier(lambda1 = 6.90185578e+04, 
                             lambda2 = 3.17392781e+02, 
                             n_hidden_features=95, 
                             n_clusters=2, 
                             row_sample = 4.63427734e-01, 
                             dropout = 3.62817383e-01,
                             type_clust = "gmm")

# fit the model on training set
fit_obj.fit(X_train, y_train)

# get the accuracy on test set
print(fit_obj.score(X_test, y_test))

# get area under the curve on test set (auc)
print(fit_obj.score(X_test, y_test, scoring="roc_auc"))

# classification report
y_pred = fit_obj.predict(X_test)
print(classification_report(y_test, y_pred))

# + id="JnfY88AkrBNi" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 208} outputId="9e023ddb-2165-4273-a6be-f79d36a508e7"
# dataset no. 2 ----------

wine = load_wine()
Z = wine.data
t = wine.target
np.random.seed(123)
Z_train, Z_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)

# create the model with nnetsauce
fit_obj = ns.Ridge2Classifier(lambda1 = 8.64135756e+04, 
                             lambda2 = 8.27514666e+04, 
                             n_hidden_features=109, 
                             n_clusters=3, 
                             row_sample = 5.54907227e-01, 
                             dropout = 1.84484863e-01,
                             type_clust = "gmm")

# fit the model on training set
fit_obj.fit(Z_train, y_train)

# get the accuracy on test set
print(fit_obj.score(Z_test, y_test))

# classification report
y_pred = fit_obj.predict(Z_test)
print(classification_report(y_test, y_pred))

# + id="wnfLXUAerDvs" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 208} outputId="4a7a344a-5b5d-4d40-9c56-9cc897c4505d"
# dataset no. 3 ----------

iris = load_iris()
Z = iris.data
t = iris.target
np.random.seed(123)
Z_train, Z_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)

# create the model with nnetsauce
fit_obj = ns.Ridge2Classifier(lambda1 = 1.87500081e+04, 
                             lambda2 = 3.12500069e+04, 
                             n_hidden_features=47, 
                             n_clusters=3, 
                             row_sample = 7.37500000e-01, 
                             dropout = 1.31250000e-01,
                             type_clust = "gmm")

# fit the model on training set
fit_obj.fit(Z_train, y_train)

# get the accuracy on test set
print(fit_obj.score(Z_test, y_test))

# classification report
y_pred = fit_obj.predict(Z_test)
print(classification_report(y_test, y_pred))

StarcoderdataJupyterScriptsDedupFiltered