# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: vocabulary_learning
#     language: python
#     name: vocabulary_learning
# ---

model_name = "logistic_regression"



# +
# %load_ext nb_black
# %load_ext autoreload
# %autoreload 2

import os

print(os.getcwd())


def update_working_directory():
    from pathlib import Path

    p = Path(os.getcwd()).parents[0]
    os.chdir(p)
    print(p)


update_working_directory()
# -

path_dataset_train = "data/raw/20201009/dataset_train.pkl"
path_dataset_valid = "data/raw/20201009/dataset_valid.pkl"

# # Import

# +
import dill
import numpy as np
import pandas as pd

pd.set_option("display.max_columns", None)

from sklearn.linear_model import LogisticRegression

from src.data.make_dataset import get_vardict

import src.models.performance_metrics as performance_metrics
# -

# # Dataset

# +
with open(path_dataset_train, "rb") as input_file:
    dataset_train = dill.load(input_file)

with open(path_dataset_valid, "rb") as input_file:
    dataset_valid = dill.load(input_file)
# -

vardict = get_vardict()

vardict = {
    "target": "result",
    "numerical": [
        "previous_score",
        "difficulty_category",
        # "levenshtein_distance_german_english",
    ],
    "all": [
        "previous_score",
        "difficulty_category",
        # "levenshtein_distance_german_english",
    ],
}

# # 1st model

X_train = dataset_train[vardict["all"]]
y_train = dataset_train[vardict["target"]]

X_train

y_train

model = LogisticRegression(random_state=0)

model.fit(X_train, y_train)

# +
with open(f"data/processed/{model_name}_model.pkl", "wb") as file:
    dill.dump(model, file)

with open(f"data/processed/{model_name}_vardict.pkl", "wb") as file:
    dill.dump(vardict, file)
# -



X_valid = dataset_valid[vardict["all"]]
y_valid = dataset_valid[vardict["target"]]

dataset_valid["y_true"] = y_valid
dataset_valid["y_pred"] = clf.predict(X_valid)
dataset_valid["y_proba"] = [x[1] for x in clf.predict_proba(X_valid)]



# +
binary_classification_results = performance_metrics.get_binary_classification_results(
    dataset_valid, model_name
)

binary_classification_results

# +
regression_results = performance_metrics.get_regression_results(
    dataset_valid, model_name
)

regression_results
# -

performance_metrics.plot_roc_auc_curve(dataset_valid, model_name)

performance_metrics.plot_precision_recall_curve(
    dataset_valid, binary_classification_results, model_name
)



StarcoderdataJupyterScriptsDedupFiltered