<jupyter_start><jupyter_text>Bank marketing  : loading data<jupyter_code>url = "https://archive.ics.uci.edu/ml/machine-learning-databases/00222/bank-additional.zip"
dataset_name = "bank-additional-full"
out_zip = Path(os.getcwd().rsplit("/", 1)[0] + "/data/bank-additional.zip")
out = Path(
    os.getcwd().rsplit("/", 1)[0] + "/data/bank-additional/" + dataset_name + ".csv"
)

download(url, out_zip)
with ZipFile(out_zip, "r") as zipObj:
    zipObj.extractall("/data")

target = "y"
to_remove = []
train = pd.read_csv(out, sep=";", low_memory=False)<jupyter_output><empty_output><jupyter_text>Census income  : loading data<jupyter_code>url = "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
dataset_name = "census-income"
out = Path(os.getcwd().rsplit("/", 1)[0] + "/data/" + dataset_name + ".csv")


download(url, out)
target = " <=50K"
to_remove = []
train = pd.read_csv(out, sep=",", low_memory=False)<jupyter_output><empty_output><jupyter_text>Load data and split<jupyter_code>if "Set" not in train.columns:
    train["Set"] = np.random.choice(
        ["train", "valid", "test"], p=[0.8, 0.1, 0.1], size=(train.shape[0],)
    )

train_indices = train[train.Set == "train"].index
valid_indices = train[train.Set == "valid"].index
test_indices = train[train.Set == "test"].index
Y = train[target].values
X = train.drop(columns=["Set"] + [target])
ratio = 0.005
n_unique = X.nunique()
ratios = (n_unique / X.shape[0]) < ratio
cat_idxs = np.argwhere(
    X.columns.isin(X.columns[ratios | (X.dtypes == "object")])
).ravel()
cat_dims = n_unique[cat_idxs].values + X.isnull().sum()[cat_idxs].values + 1
del n_unique, ratios, train
gc.collect()<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>tg_enc = LabelEncoder()
Y = tg_enc.fit_transform(Y)
X_train = X.values[train_indices]
y_train = Y[train_indices]

X_valid = X.values[valid_indices]
y_valid = Y[valid_indices]

X_test = X.values[test_indices]
y_test = Y[test_indices]
X_train.shape
X_train_prep, encoders = prepare_input_data(X_train, cat_idxs)
X_valid_prep, _ = prepare_input_data(X_valid, cat_idxs, encoders=encoders, fit=False)
X_test_prep, _ = prepare_input_data(X_test, cat_idxs, encoders=encoders, fit=False)<jupyter_output><empty_output><jupyter_text>Network parameters<jupyter_code>metrics = ["AUC"]
%%time

model = ThcNetClassifier(
    n_layer=3,
    mul_input=8, 
    metrics=metrics,
    cat_idxs=cat_idxs,
    cat_emb_dims=cat_dims,
    dropout=0.05,
    normalize=False,
    max_emb=20,
    patience=30
)

history = model.fit(
        X=X_train_prep, 
        y=y_train,
        X_valid=X_valid_prep,
        y_valid=y_valid,
        batch_size=1024,
        epochs=10000,
        verbose=2,
)
%%time

model_snn = ThcNetClassifier(
    n_layer=3,
    mul_input=8, 
    metrics=metrics,
    cat_idxs=cat_idxs,
    cat_emb_dims=cat_dims,
    dropout=0.05,
    normalize=False,
    max_emb=20,
    patience=30,
    use_snn=True,
    noise=None
)

history_snn = model_snn.fit(
        X=X_train_prep, 
        y=y_train,
        X_valid=X_valid_prep,
        y_valid=y_valid,
        batch_size=1024,
        epochs=10000,
        verbose=2,
)
model.network.summary()
plot_model(
    model,
    # to_file="model.png",
    show_shapes=True,
    show_layer_names=True,
    rankdir="TB",
    expand_nested=False,
    dpi=96,
)
model_snn.network.summary()<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>plot_history(history)
plot_history(history_snn)
y_pred = model.predict_proba(X_test_prep)
test_auc = roc_auc_score(y_score=y_pred[:, 1], y_true=y_test)
print(f"FINAL TEST SCORE FOR {dataset_name} : {test_auc}")
y_pred = model_snn.predict_proba(X_test_prep)
test_auc = roc_auc_score(y_score=y_pred[:, 1], y_true=y_test)
print(f"FINAL TEST SCORE FOR {dataset_name} : {test_auc}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15182291666666667, 'fraction_numerical': 0.017708333333333333, 'mean_word_length': 4.819696969696969, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '537395', 'license': 'MIT', 'n_tokens_mistral': 1594, 'n_tokens_neox': 1537, 'n_words': 218, 'path': 'classifier_example.ipynb', 'repo_name': 'Hartorn/thc-net', 'size': '3840'}