# ---
# 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 Multitask classifier
#
# This notebook demonstrates the use of `nnetsauce`'s Ridge2 Multitask classifier. It's a quasi-randomized network model for classification with 2 shrinkage parameters (one on input data, another one on the hidden layer). In this model, a multi-class classification problem is turned into multiple two-class problems.

# + 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_breast_cancer, load_wine, load_iris, load_digits, make_classification
from sklearn.model_selection import train_test_split
from sklearn import metrics
from time import time

# + id="7cB4SMlzq-z2" colab_type="code" outputId="a69936aa-b29c-4c52-88b8-c34b13b1646f" colab={"base_uri": "https://localhost:8080/", "height": 260}
# dataset no. 1 ---------- 

breast_cancer = load_breast_cancer()
Z = breast_cancer.data
t = breast_cancer.target
np.random.seed(123)
X_train, X_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)

print(Z.shape)
 
fit_obj = ns.Ridge2MultitaskClassifier(n_hidden_features=np.int(9.83730469e+01), 
                                   dropout=4.31054687e-01, 
                                   n_clusters=np.int(1.71484375e+00),
                                   lambda1=1.24023438e+01, lambda2=7.30263672e+03)

start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)

print(fit_obj.score(X_test, y_test))
print(fit_obj.score(X_test, y_test, scoring="roc_auc"))


start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))


# + id="JnfY88AkrBNi" colab_type="code" outputId="72aac767-04be-4e69-ff9a-4872ba11c8bd" colab={"base_uri": "https://localhost:8080/", "height": 225}
# 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)

fit_obj = ns.Ridge2MultitaskClassifier(n_hidden_features=15,
                                  dropout=0.1, n_clusters=3, 
                                  type_clust="gmm")

start = time()
fit_obj.fit(Z_train, y_train)
print(time() - start)

print(fit_obj.score(Z_test, y_test))

preds = fit_obj.predict(Z_test)
print(metrics.classification_report(preds, y_test))

# + id="wnfLXUAerDvs" colab_type="code" outputId="e63e159b-e028-4079-8ab0-5734b6429a0f" colab={"base_uri": "https://localhost:8080/", "height": 225}
# 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)


fit_obj = ns.Ridge2MultitaskClassifier(n_hidden_features=10,
                                  dropout=0.1, n_clusters=2)

start = time()
fit_obj.fit(Z_train, y_train)
print(time() - start)

print(fit_obj.score(Z_test, y_test))

preds = fit_obj.predict(Z_test)
print(metrics.classification_report(preds, y_test))

# + id="opUwVh8FOoAv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 364} outputId="7766faa7-cf92-436a-bf9a-341d268cfdc9"
# dataset no. 4 ----------

digits = load_digits()
X = digits.data
y = digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
                                                    random_state=123)

fit_obj = ns.Ridge2MultitaskClassifier(n_hidden_features=25,
                                  dropout=0.1, n_clusters=3, 
                                  type_clust="gmm")

start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)
print(fit_obj.score(X_test, y_test))

start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))


StarcoderdataJupyterScriptsDedupFiltered