<jupyter_start><jupyter_text>OHE,Target + LogitIdeas:* Replace missing values with constant* Add number of missing values in row as a feature* Apply StandardScaler to created feature* Apply Target to features that have many unique values* Apply OHE to other features* Logistic regression trained on full train set<jupyter_code>import numpy as np
import pandas as pd

import warnings
warnings.simplefilter('ignore')

import os<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>train = pd.read_csv('../data/train.csv', index_col='id')
test = pd.read_csv('../data/test.csv', index_col='id')
train.head(3).T
def summary(df):
    summary = pd.DataFrame(df.dtypes, columns=['dtypes'])
    summary = summary.reset_index()
    summary['Name'] = summary['index']
    summary = summary[['Name', 'dtypes']]
    summary['Missing'] = df.isnull().sum().values    
    summary['Uniques'] = df.nunique().values
    summary['First Value'] = df.loc[0].values
    summary['Second Value'] = df.loc[1].values
    summary['Third Value'] = df.loc[2].values
    return summary


summary(train)<jupyter_output><empty_output><jupyter_text>Handle missing values Add number of missing values in row as a feature<jupyter_code>train['missing_count'] = train.isnull().sum(axis=1)
test['missing_count'] = test.isnull().sum(axis=1)<jupyter_output><empty_output><jupyter_text>Replace missing values with constants<jupyter_code>missing_number = -99999
missing_string = 'MISSING_STRING'
numerical_features = [
    'bin_0', 'bin_1', 'bin_2',
    'ord_0',
    'day', 'month'
]

string_features = [
    'bin_3', 'bin_4',
    'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5',
    'nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9'
]
def impute(train, test, columns, value):
    for column in columns:
        train[column] = train[column].fillna(value)
        test[column] = test[column].fillna(value)
impute(train, test, numerical_features, missing_number)
impute(train, test, string_features, missing_string)<jupyter_output><empty_output><jupyter_text>Feature engineering Split 'ord_5' preserving missing values<jupyter_code>train['ord_5_1'] = train['ord_5'].str[0]
train['ord_5_2'] = train['ord_5'].str[1]

train.loc[train['ord_5'] == missing_string, 'ord_5_1'] = missing_string
train.loc[train['ord_5'] == missing_string, 'ord_5_2'] = missing_string

train = train.drop('ord_5', axis=1)


test['ord_5_1'] = test['ord_5'].str[0]
test['ord_5_2'] = test['ord_5'].str[1]

test.loc[test['ord_5'] == missing_string, 'ord_5_1'] = missing_string
test.loc[test['ord_5'] == missing_string, 'ord_5_2'] = missing_string

test = test.drop('ord_5', axis=1)
simple_features = [
    'missing_count'
]

ohe_features = [
    'bin_0', 'bin_1', 'bin_2', 'bin_3', 'bin_4',
    'nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4',
    'ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5_1', 'ord_5_2',
    'day', 'month'
]

target_features = [
    'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9'
]<jupyter_output><empty_output><jupyter_text>Extract target variable<jupyter_code>y_train = train['target'].copy()
x_train = train.drop('target', axis=1)
del train

x_test = test.copy()
del test<jupyter_output><empty_output><jupyter_text>Standard scaler<jupyter_code>from sklearn.preprocessing import StandardScaler


scaler = StandardScaler()
simple_x_train = scaler.fit_transform(x_train[simple_features])
simple_x_test = scaler.transform(x_test[simple_features])<jupyter_output><empty_output><jupyter_text>OHE<jupyter_code>from sklearn.preprocessing import OneHotEncoder


ohe = OneHotEncoder(dtype='uint16', handle_unknown="ignore")
ohe_x_train = ohe.fit_transform(x_train[ohe_features])
ohe_x_test = ohe.transform(x_test[ohe_features])
ohe_x_train.shape<jupyter_output><empty_output><jupyter_text>Target encoder<jupyter_code>from category_encoders import TargetEncoder
from sklearn.model_selection import StratifiedKFold
def transform(transformer, x_train, y_train, cv):
    oof = pd.DataFrame(index=x_train.index, columns=x_train.columns)
    for train_idx, valid_idx in cv.split(x_train, y_train):
        x_train_train = x_train.loc[train_idx]
        y_train_train = y_train.loc[train_idx]
        x_train_valid = x_train.loc[valid_idx]
        transformer.fit(x_train_train, y_train_train)
        oof_part = transformer.transform(x_train_valid)
        oof.loc[valid_idx] = oof_part
    return oof
target = TargetEncoder(drop_invariant=True, smoothing=0.2)

cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
target_x_train = transform(target, x_train[target_features], y_train, cv).astype('float')

target.fit(x_train[target_features], y_train)
target_x_test = target.transform(x_test[target_features]).astype('float')<jupyter_output><empty_output><jupyter_text>Merge all<jupyter_code>import scipy


final_x_train = scipy.sparse.hstack([simple_x_train, ohe_x_train, target_x_train]).tocsr()
final_x_test = scipy.sparse.hstack([simple_x_test, ohe_x_test, target_x_test]).tocsr()<jupyter_output><empty_output><jupyter_text>Logistic regression<jupyter_code>from sklearn.linear_model import LogisticRegression


logit = LogisticRegression(C=0.54321, solver='lbfgs', max_iter=10000)
logit.fit(final_x_train, y_train)
y_pred = logit.predict_proba(final_x_test)[:, 1]<jupyter_output><empty_output><jupyter_text>Submit predictions<jupyter_code>submission = pd.read_csv('../input/cat-in-the-dat-ii/sample_submission.csv', index_col='id')
submission['target'] = y_pred
submission.to_csv('logit.csv')
submission.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1444564824846515, 'fraction_numerical': 0.01968219573853377, 'mean_word_length': 6.912857142857143, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '605392', 'license': 'MIT', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2055, 'n_words': 337, 'path': 'eda/cat-in-dat-2-ohe-target-logit.ipynb', 'repo_name': 'chamhoo/cfec2', 'size': '5538'}