<jupyter_start><jupyter_text>For classification...using titanic dataset  Libraries<jupyter_code># basics
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import gc
import json
import time
import datetime
import os
import sys
import pickle
from sklearn.model_selection import train_test_split, StratifiedKFold

# visualize
import matplotlib.pyplot as plt
import matplotlib.style as style
from matplotlib_venn import venn2
import seaborn as sns
from matplotlib import pyplot
from matplotlib.ticker import ScalarFormatter
import plotly.figure_factory as ff
import plotly.express as px
import missingno as msno

sns.set_context("talk")
# sns.set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})
style.use('fivethirtyeight')

# custom EDA functions
mypath = os.getcwd()
sys.path.append(mypath + '/code/') 
from utils import reduce_mem_usage, plot_target
from categorical_encoder import label_encoding
from feature_converter import categorize_features
from catplot import CategoricalVisualizer
from denseplot import DenseVisualizer

# custom modeling functions
sys.path.append(mypath + '/models/') 
from run_models import RunModel
from lgb_param_models import lgb_model
from xgb_param_models import xgb_model
from catb_param_models import catb_model
from lin_param_models import lin_model
from nn_param_models import nn_model

# stats
from scipy import stats
from sklearn.metrics import cohen_kappa_score, mean_squared_error, mean_absolute_error
from sklearn.model_selection import KFold, StratifiedKFold, GroupKFold
from sklearn.model_selection import train_test_split
import scipy as sp
from functools import partial
from collections import Counter

pd.set_option('display.max_rows', None)

import warnings
warnings.filterwarnings('ignore')

print("libraries imported")<jupyter_output>/opt/anaconda3/lib/python3.7/site-packages/lightgbm/__init__.py:48: UserWarning:

Starting from version 2.2.1, the library file in distribution wheels for macOS is built by the Apple Clang (Xcode_8.3.3) compiler.
This means that in case of installing LightGBM from PyPI via the ``pip install lightgbm`` command, you don't need to install the gcc compiler anymore.
Instead of that, you need to install the OpenMP library, which is required for running LightGBM on the system with the Apple Clang compiler.
You can install the OpenMP library by the following command: ``brew install libomp``.

Using TensorFlow backend.<jupyter_text>Load data<jupyter_code>!ls
# check path
datapath = "inputs/"
files = os.listdir(datapath)
print(files)
datapath = datapath + 'titanic'
os.listdir(datapath)
# import data
time0 = time.time()

train = pd.read_csv(datapath + '/train.csv')
test = pd.read_csv(datapath + '/test.csv')
submission = pd.read_csv(datapath + '/gender_submission.csv')

time1 = time.time()
print(time1 - time0, " seconds to load data")
# check train
print(train.shape)
train.head()
# check test
print(test.shape)
test.head()
# check submission file
print(submission.shape)
submission.head()<jupyter_output>(418, 2)<jupyter_text>EDA<jupyter_code>train.info()
train.dtypes
# missing data?
import missingno as msno
msno.matrix(df=train, figsize=(20,14), color=(0.5,0,0))<jupyter_output><empty_output><jupyter_text>Target<jupyter_code># hist
target = "Survived"
plot_target(train, target, objective="classification")
# roughly categorize features
features, dense_features, cat_features = categorize_features(train, target, cat_threshold=12)<jupyter_output>There are 11 features.
There are 3 dense features.
There are 8 categorical features.<jupyter_text>Dense features<jupyter_code>vd = DenseVisualizer(train=train, test=test, features=dense_features, target=target)
chkd = vd.column_checker()
chkd.style.background_gradient()
vd.plot_bars()
vd.plot_vs_target()<jupyter_output><empty_output><jupyter_text>Categorical features<jupyter_code># EDA for categorical features
vc = CategoricalVisualizer(train=train, test=test, features=cat_features, target=target)
chkc = vc.column_checker()
chkc.style.background_gradient()
# remove categorical features which have different values between train and test
cat_features = [c for c in cat_features if chkc.loc[chkc["features"] == c, "overlap"].iloc[0] == 1]
print(len(cat_features))
cat_features
vc = CategoricalVisualizer(train=train, test=test, features=cat_features, target=target)
vc.plot_bars()
vc.plot_venn()<jupyter_output><empty_output><jupyter_text>Fit LGB and Get Feature importance<jupyter_code># label encoding object features
train, test, cat_features = label_encoding(train, test, cat_features)
train[target].isna().sum()
features = dense_features + cat_features
target in features
# fit LGB
lgbm = RunModel(train, test, target, features, categoricals=cat_features, n_splits=5,
                model="lgb", task="binary", cv_method="StratifiedKFold", seed=1220)
# plot feature importance
lgb_fi_df = lgbm.plot_feature_importance(rank_range=[1,30])
# fit CB
lgbm = RunModel(train, test, target, features, categoricals=cat_features, n_splits=5,
                model="catb", task="binary", cv_method="StratifiedKFold", seed=1220)
# fit XGB
lgbm = RunModel(train, test, target, features, categoricals=cat_features, n_splits=5,
                model="xgb", task="binary", cv_method="StratifiedKFold", seed=1220)
# fit Linear
lgbm = RunModel(train, test, target, features, categoricals=cat_features, n_splits=5,
                model="linear", task="binary", cv_method="StratifiedKFold", scaler="Standard", seed=1220)
# fit NN
lgbm = RunModel(train, test, target, features, categoricals=cat_features, n_splits=5,
                model="nn", task="binary", cv_method="StratifiedKFold", scaler="MinMax", seed=1220)<jupyter_output>Train on 712 samples, validate on 179 samples
Epoch 1/80
712/712 [==============================] - ETA: 6s - loss: 0.775 - 2s 2ms/step - loss: 0.7336 - val_loss: 0.6958
Epoch 2/80
712/712 [==============================] - ETA: 0s - loss: 0.719 - 0s 88us/step - loss: 0.7007 - val_loss: 0.6874
Epoch 3/80
712/712 [==============================] - ETA: 0s - loss: 0.715 - 0s 81us/step - loss: 0.7006 - val_loss: 0.6758
Epoch 4/80
712/712 [==============================] - ETA: 0s - loss: 0.747 - 0s 80us/step - loss: 0.6793 - val_loss: 0.6646
Epoch 5/80
712/712 [==============================] - ETA: 0s - loss: 0.708 - 0s 82us/step - loss: 0.6686 - val_loss: 0.6585
Epoch 6/80
712/712 [==============================] - ETA: 0s - loss: 0.652 - 0s 83us/step - loss: 0.6612 - val_loss: 0.6534
Epoch 7/80
712/712 [==============================] - ETA: 0s - loss: 0.636 - 0s 85us/step - loss: 0.6413 - val_loss: 0.6497
Epoch 8/80
712/712 [==============================] - ETA: 0s - loss: 0.581 - 0s[...]
{'fraction_non_alphanumeric': 0.13910332541567696, 'fraction_numerical': 0.04082541567695962, 'mean_word_length': 5.811931243680485, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '100771', 'license': 'MIT', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2227, 'n_words': 608, 'path': 'EDA_titanic.ipynb', 'repo_name': 'katsu1110/DataScienceComp', 'size': '6736'}