<jupyter_start><jupyter_text>Categories of Services Shortage Classification (LGBM Multi-Class)1. Data was first pre-processed and load the final version of la_facility_mua.csv here2. Since feature engineering has been done, a few touch-ups before training:a) **Feature Mapping,**b) **Dropping useless variables,**c) **Datetime conversion,**d) **Days Diff**3. We have **3 main types of Services (Target variable: Category Service)**a) **Health and Mental Health**b) **Social Services**c) **Public Safety**4. Data was split into Train and Validation5. Model: **LGBM**6. 2 Methods:a) Simple train-test-split b) 6-StratifiedKFold<jupyter_code>import os
os.environ['PYTHONHASHSEED']='0'
import pandas as pd
import numpy as np
import random as rn
# Deep Learning Libraries
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten,Dense,Dropout,BatchNormalization
from tensorflow.keras.layers import Conv1D,MaxPool1D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
from tensorflow.keras import callbacks
from tensorflow.keras.models import load_model
import tensorflow as tf
np.random.seed(37)
rn.seed(42)
tf.random.set_seed(42)
from keras import backend as K
import pandas as pd
import numpy as np
import seaborn as sns
import re
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.gridspec as gridspec
from math import sqrt 
from sklearn.metrics import log_loss
import lightgbm as lgb
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import auc,roc_auc_score
from catboost import CatBoostClassifier,Pool, cv
from sklearn.preprocessing import LabelEncoder
from lightgbm import LGBMClassifier
from sklearn.metrics import roc_curve, auc, classification_report, confusion_matrix, accuracy_score
from sklearn.model_selection import train_test_split, cross_val_score, KFold, StratifiedKFold

la_facility_mua = pd.read_csv('../input/la-final-data-2/la_facility_mua.csv')
la_facility_mua.head()
la_facility_mua.rename({'cat1': 'Category Service'}, axis=1, inplace=True)<jupyter_output><empty_output><jupyter_text>We will use 'Service Category' as our target variable<jupyter_code>la_facility_mua['Category Service'].value_counts().sort_values(ascending=False)
la_facility_mua['Category Service']=la_facility_mua['Category Service'].map({
'Health and Mental Health':0,
'Social Services':1,
'Public Safety':2
})
la_facility_mua['Category Service']=la_facility_mua['Category Service'].astype('int32')
la_facility_mua['facility_er_service'] = la_facility_mua['facility_er_service'].map({
'Not Applicable':'Not Applicable',
'None':'Not Applicable',
'Emergency - Basic':'Emergency',
'Emergency - Comprehensive':'Emergency',
'Emergency - Standby':'Emergency'
})
la_facility_mua['MUA_Poverty']=la_facility_mua['MUA_Poverty'].map({
157757.0:2,
129447.0:1,
0.0:0,
})
la_facility_mua['MUA_Designated_Date'] = pd.to_datetime(la_facility_mua['MUA_Designated_Date']) 
la_facility_mua['MUA_Updated_Date'] = pd.to_datetime(la_facility_mua['MUA_Updated_Date']) 
la_facility_mua['Date Difference'] = la_facility_mua['MUA_Updated_Date']-la_facility_mua['MUA_Designated_Date']
def ddiff2days(ddiff):
    if not pd.isnull(ddiff):
        return pd.Timedelta.total_seconds(ddiff)/(24.*3600)
    else:
        return np.NaN
la_facility_mua['Date Difference']=la_facility_mua['Date Difference'].apply(ddiff2days)
la_facility_mua['Date Difference']=la_facility_mua['Date Difference'].astype('int32')
la_facility_mua['design_year'] = (la_facility_mua['MUA_Designated_Date'].dt.year).astype('int32')
la_facility_mua['design_month'] = (la_facility_mua['MUA_Designated_Date'].dt.month).astype('int32')
la_facility_mua['design_week'] = (la_facility_mua['MUA_Designated_Date'].dt.week).astype('int32')
la_facility_mua['design_dow'] = (la_facility_mua['MUA_Designated_Date'].dt.dayofweek).astype('int32')

la_facility_mua['update_year'] = (la_facility_mua['MUA_Updated_Date'].dt.year).astype('int32')
la_facility_mua['update_month'] = (la_facility_mua['MUA_Updated_Date'].dt.month).astype('int32')
la_facility_mua['update_week'] = (la_facility_mua['MUA_Updated_Date'].dt.week).astype('int32')
la_facility_mua['update_dow'] = (la_facility_mua['MUA_Updated_Date'].dt.dayofweek).astype('int32')
la_facility_mua.drop(['MUA_Designated_Date', 'MUA_Updated_Date',                     
 ], axis=1, inplace=True)
cols = [c for c in la_facility_mua.columns if la_facility_mua[c].dtypes=='object']
for c in cols:
    le = LabelEncoder()
    la_facility_mua[c] = (le.fit_transform(la_facility_mua[c])).astype('int32')
X = la_facility_mua.drop(labels=['Category Service'], axis=1)
y = la_facility_mua['Category Service']

from sklearn.model_selection import train_test_split
X_train, X_cv, y_train, y_cv = train_test_split(X, y, test_size=0.25, random_state=tf.random.set_seed(42))
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_cv, label=y_cv)

param = {'num_iterations': 800,
         'num_leaves': 31,
         'max_depth': -1,
         'learning_rate': 0.01, 
         'num_class': 3,
         'objective': 'multiclass',
         'metric': 'multi_logloss',
         'min_data_per_group': 80,
         'feature_fraction': 0.4,
         'bagging_fraction':0.5,
         'bagging_freq': 5,
         'boosting': 'gbdt',
         'is_unbalance':True
         }

clf = lgb.train(params=param, 
                early_stopping_rounds=20,
                verbose_eval=100,
                train_set=train_data,
                valid_sets=[train_data, test_data])

y_pred = clf.predict(X_cv)
log_loss(y_cv, y_pred)
feature_imp = pd.DataFrame(sorted(zip(clf.feature_importance(), X.columns), reverse=True)[:15],
                           columns=['Value','Feature'])
plt.figure(figsize=(10,8))
sns.barplot(x="Value", y="Feature", data=feature_imp.sort_values(by="Value", ascending=False))
plt.title('LightGBM Features')
plt.tight_layout()
plt.show()
err = []
fold = StratifiedKFold(n_splits=6, shuffle=True, random_state=42)
for train_index, test_index in fold.split(X, y):
    
    X_train, X_cv = X.loc[train_index], X.loc[test_index]
    y_train, y_cv = y[train_index], y[test_index]
    
    train_data = lgb.Dataset(X_train, label=y_train)
    val_data = lgb.Dataset(X_cv, label=y_cv)
    
    clf = lgb.train(params=param, 
                     early_stopping_rounds=100,
                     verbose_eval=50,
                     train_set=train_data,
                     valid_sets=[train_data, val_data])

    y_pred = clf.predict(X_cv)
    print("Log Loss: ", (log_loss(y_cv, y_pred)))
    err.append(log_loss(y_cv, y_pred))
np.mean(err,0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12614746816701214, 'fraction_numerical': 0.017175007403020432, 'mean_word_length': 5.622549019607843, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '66264', 'license': 'MIT', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2398, 'n_words': 436, 'path': 'dataAnalysis/LAHealthcareProject/DLmodels_CNN_OLC/CategoryService_LGBM.ipynb', 'repo_name': 'mminamina/311-data', 'size': '6754'}