<jupyter_start><jupyter_text>load train/test<jupyter_code>df_application_train = load_train_test(
    filename=train_filename,
    path_to_train_folder=path_to_data,
)
df_application_test = load_train_test(
    filename=test_filename,
    path_to_train_folder=path_to_data,
)<jupyter_output><empty_output><jupyter_text>select features<jupyter_code># 23 features and y
features = [
    'TARGET',
    'NAME_CONTRACT_TYPE',
    'CODE_GENDER',
    'FLAG_OWN_CAR',
    'FLAG_OWN_REALTY',
    'CNT_CHILDREN',
    'AMT_INCOME_TOTAL',
    'AMT_CREDIT',
    'AMT_ANNUITY',
    'AMT_GOODS_PRICE',
    'NAME_TYPE_SUITE',
    'NAME_INCOME_TYPE',
    'NAME_FAMILY_STATUS',
    'DAYS_BIRTH',
    'DAYS_EMPLOYED',
    'DAYS_REGISTRATION',
    'DAYS_ID_PUBLISH',
    'FLAG_MOBIL',
    'FLAG_EMP_PHONE',
    'FLAG_WORK_PHONE',
    'FLAG_CONT_MOBILE',
    'FLAG_PHONE',
    'FLAG_EMAIL',
    'CNT_FAM_MEMBERS',
]
df_train = df_application_train[features]
df_test = df_application_test[features[1:]]
df_train.isna().sum()
df_test.isna().sum()
df_train = df_train.dropna()<jupyter_output><empty_output><jupyter_text>processing<jupyter_code>df_train = df_train[df_train['CODE_GENDER'].isin(['M', 'F'])]
df_test = df_test[df_test['CODE_GENDER'].isin(['M', 'F'])]
# label encoding
label_encoding_columns = [
    'NAME_CONTRACT_TYPE',
    'CODE_GENDER',
    'FLAG_OWN_CAR',
    'FLAG_OWN_REALTY',
    'NAME_TYPE_SUITE',
    'NAME_INCOME_TYPE',
    'NAME_FAMILY_STATUS',
]

label_encoding_dict = {}

for column in label_encoding_columns:
    label_encoding_dict[column] = LabelEncoder()
    df_train[column] = label_encoding_dict[column].fit_transform(df_train[column])
# make positive
for column in ['DAYS_BIRTH', 'DAYS_EMPLOYED', 'DAYS_REGISTRATION', 'DAYS_ID_PUBLISH']:
    df_train[column] = -1 * df_train[column]
    df_test[column] = -1 * df_test[column]
# drop DAYS_EMPLOYED == 350K
df_train = df_train[df_train['DAYS_EMPLOYED'] >= 0]<jupyter_output><empty_output><jupyter_text>modeling<jupyter_code>X = df_train.drop(['TARGET'], axis=1)
y = df_train['TARGET']
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.33, random_state=42, stratify=y
)
clf = LogisticRegression(class_weight='balanced')
clf.fit(X=X_train, y=y_train)
print(
    classification_report(
        y_pred=clf.predict(X_test),
        y_true=y_test,
    )
)<jupyter_output>precision    recall  f1-score   support

           0       0.93      0.57      0.71     75654
           1       0.11      0.58      0.19      7185

    accuracy                           0.57     82839
   macro avg       0.52      0.58      0.45     82839
weighted avg       0.86      0.57      0.67     82839<jupyter_text>precision/recall<jupyter_code>precision, recall, thresholds = precision_recall_curve(
    y_true=y_test,
    probas_pred=clf.predict_proba(X_test)[:, 0],
)
plt.figure(figsize=(10, 5))

###
plt.subplot(121)

plt.step(recall, precision)

plt.xlabel('recall')
plt.ylabel('precision')

plt.grid()

###
plt.subplot(122)

plt.plot(thresholds, precision[:-1], label='precision')
plt.plot(thresholds, recall[:-1], label='recall')
plt.plot(thresholds, 2*precision[:-1]*recall[:-1]/(precision[:-1]+recall[:-1]), label='f1')

plt.xlabel('thresholds')
plt.ylabel('precision / recall/ f1')

plt.legend()
plt.grid()
# correlation reduction: 7 features left
df_train = df_train.drop([
    'AMT_GOODS_PRICE',
    'CNT_FAM_MEMBERS',
    'AMT_ANNUITY',
],
    axis=1,
)

df_test = df_test.drop([
    'AMT_GOODS_PRICE',
    'CNT_FAM_MEMBERS',
    'AMT_ANNUITY',
],
    axis=1,
)<jupyter_output><empty_output><jupyter_text>fir model<jupyter_code>X_train_sample, y_train_sample = RandomUnderSampler().fit_resample(
    X=X_train,
    y=y_train,
)
X_train_sample_normed = StandardScaler().fit_transform(X_train_sample)
clf = LogisticRegression()
cv = StratifiedKFold(n_splits=5)
for score in ['accuracy', 'precision', 'recall', 'f1']:
    result = cross_val_score(
        estimator=clf,
        X=X_train_sample_normed,
        y=y_train_sample,
        scoring=score,
        cv=cv,
    )
    
    print(f"{score}: {round(result.mean(), 3)}")<jupyter_output>accuracy: 0.611
precision: 0.604
recall: 0.647
f1: 0.625
{'fraction_non_alphanumeric': 0.14014878809695225, 'fraction_numerical': 0.029517638588912886, 'mean_word_length': 4.275949367088607, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '261078', 'license': 'MIT', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1684, 'n_words': 227, 'path': 'thesis/modeling.ipynb', 'repo_name': 'dayyass/bachelor_diploma', 'size': '4167'}