<jupyter_start><jupyter_text>dataset_name = "rl"filename = "train_bench.csv"target = "target"ids = [] dataset_name = "amazon"filename = "train_bench.csv"target = "ACTION"ids = [] dataset_name = "santander-customer-satisfaction"filename = "train_bench.csv"target = "TARGET"ids = []<jupyter_code>dataset_name = "road-safety"
filename = "train_bench.csv"
target = "Sex_of_Driver_df_res"
ids = []
dataset_name = "open-payments"
filename = "train_bench.csv"
target = "status"
ids = []
dataset_name = "census-income"
filename = "train_bench.csv"
target = "taxable income amount"
ids = []
dataset_name = "albert"
filename = "train_bench.csv"
target = "target"
ids = []
dataset_name = "cat-in-the-dat-ii"
filename = "train_bench.csv"
target = "target"
ids = ["id"]
dataset_name = "bnp-cardif"
filename = "train_bench.csv"
target = "target"
ids = []
dataset_name = "homesite-quote-conversion"
filename = "train_bench.csv"
target = "QuoteConversion_Flag"
ids = []
dataset_name = "give-me-some-credit"
filename = "train_bench.csv"
target = "SeriousDlqin2yrs"
ids = ["Unamed", "age"]
dataset_name = "bank-marketing"
filename = "train_bench.csv"
target = "y"
ids = []<jupyter_output><empty_output><jupyter_text>dataset_name = "sarcasm"filename = "train_bench.csv"target = "label"ids = [    "date",    "created_utc",    "author",    "subreddit",    "score",    "ups",    "downs",    "parent_comment",]<jupyter_code>out = Path(os.getcwd()) / "data" / dataset_name / filename
train = pd.read_csv(out)
train.shape
train.columns
if "Set" not in train.columns:
    print("Building tailored column")
    train_valid_index, test_index = next(
        StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=SEED).split(
            range(train[target].shape[0]), train[target].values
        )
    )
    train_index, valid_index = next(
        StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=SEED).split(
            train_valid_index, train[target].values[train_valid_index]
        )
    )
    train["Set"] = "train"
    train["Set"][valid_index] = "valid"
    train["Set"][test_index] = "test"
    # train.to_csv((out.parent / "train_bench.csv").as_posix(), index=False)
train_indices = train[train.Set == "train"].index
valid_indices = train[train.Set == "valid"].index
test_indices = train[train.Set == "test"].index
# train["comment"] = train["comment"].fillna("unknown")
# max_len = np.percentile(np.vectorize(len)(train["comment"].values), 90)
# max_len
# train["comment"] = train["comment"].str.slice(0, int(max_len))
# train["parent_comment"] = train["parent_comment"].fillna("unknown")
# max_len = np.percentile(np.vectorize(len)(train["parent_comment"].values), 90)
# max_len
# train["parent_comment"] = train["parent_comment"].str.slice(0, int(max_len))
input_train, params = preproc_dataset(train.loc[train_indices], target, ids + ["Set"])
params
len(train_indices)
input_valid, _ = preproc_dataset(
    train.loc[valid_indices], target, ids + ["Set"], params
)
input_test, _ = preproc_dataset(train.loc[test_indices], target, ids + ["Set"], params)
target_encoder = LabelEncoder()
train[target] = target_encoder.fit_transform(train[target].values.reshape(-1))
y_train = train[target].values[train_indices]
y_valid = train[target].values[valid_indices]
y_test = train[target].values[test_indices]
params
model = build_model(params, lconv_dim=[16], lconv_num_dim=[16],)
model.get_layer("output")._build_input_shape
model.summary()
# !pip install pydot graphviz
plot_model(
    model,
    # to_file="model.png",
    show_shapes=True,
    show_layer_names=True,
    rankdir="TB",
    expand_nested=False,
    dpi=96,
)
#!pip install pydot graphviz
y_train.shape
counts = np.unique(y_train, return_counts=True)[1]
counts = counts.sum() / counts
class_weight = {
    0: counts[0],
    1: counts[1],
}
class_weight<jupyter_output><empty_output><jupyter_text>class_weight = {    0: 1,    1: 1,}class_weight<jupyter_code>%%time
history = model.fit(
    input_train,
    y_train.reshape(-1, 1),
    epochs=2000,
    batch_size=1024,
    validation_data=(input_valid, y_valid.reshape(-1, 1),),
    verbose=1,
    callbacks=[EarlyStopping(monitor="val_loss", patience=20, verbose=1)],
    class_weight=class_weight
)
plot_history(history)
model_auc = roc_auc_score(
    y_true=y_valid, y_score=model.predict(input_valid).reshape(-1),
)
model_auc
# 0.945180869596132 => embeddign
# More cats 0.9499529383532423
# 0.9510084257679181 => 32 => 16
# porto seguro 0.6232119567809998
# homesite-quote-conversion 0.9523864163447144
# cat in dat II 0.7664026194097782
# albert 0.7204053242303463
# census-income 0.937909089697099
# open payments 0.9259053788101843
# give-me-some-credit 0.8579704956990437
# bank-marketing 0.7783336895486428
model_auc = roc_auc_score(y_true=y_test, y_score=model.predict(input_test).reshape(-1),)
model_auc
# 0.9438800811761906 => embeddign
# More cats 0.9501807921285899
# 0.9500337101038149 => 32 => 16
# porto seguro 0.6271530652897266
# homesite-quote-conversion 0.9551354942949817
# cat in dat II 0.759840080492485
# albert 0.7160934658045495
# census-income 0.9349738109864048
# open payments 0.9274364959503505
# give-me-some-credit 0.8559444719192335
# bank-marketing 0.804706871986707
from thc_net.explainable_model.model import predict, encode
probs, explanations = predict(model, input_test)
probs, encoded_output = encode(model, input_test)
y_test.shape
encoded_output.shape
probs[0], explanations[0].shape
import matplotlib.pyplot as plt

# plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt


def explain_plot(importances, columns):
    selection = np.argsort(-np.absolute(importances))[:10]
    # indexes = np.argsort(importances)
    performance = importances[selection]
    #     print(performance.shape)512, 256, 128, 64, 32, 1
    y_pos = np.arange(performance.shape[0])

    plt.barh(y_pos, performance, align="center", alpha=0.5)
    plt.yticks(y_pos, columns[selection])
    # plt.xlabel('Usage')
    plt.title("Feature importance")

    plt.show()
all_cols = np.array(params["bool_cols"] + params["num_cols"] + params["cat_cols"])
all_cols
explain_plot(explanations[0], all_cols)
probs[0].item()
for i in range(20):
    explain_plot(explanations[i], all_cols)
    print(probs[i].item())
from sklearn.metrics import confusion_matrix
np.unique(y_test, return_counts=True)
y_test
target_encoder.classes_
confusion_matrix(
    y_true=y_test,
    y_pred=model.predict(input_test).reshape(-1) >= 0.5,
    # labels=target_encoder.classes_,
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14524028160391797, 'fraction_numerical': 0.06948270584634221, 'mean_word_length': 5.521956087824352, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '427986', 'license': 'MIT', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2372, 'n_words': 400, 'path': 'conv1d-generic-clean.ipynb', 'repo_name': 'Hartorn/thc-net', 'size': '6534'}