<jupyter_start><jupyter_text>Updates of the Previous Notebook 1. Algorithms like Xgboost can take care of Categorical and    Ordinal features without OneHotEncoding. So as per our features, performing   OneHotEncoding results in more features and Curse of Dimensionality begins !!2. Cross Validation using StratifiedKFold<jupyter_code>import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

from sklearn.ensemble import VotingClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split,cross_val_score,StratifiedKFold
from sklearn import metrics

import warnings
warnings.filterwarnings('ignore')
df_train = pd.read_csv('Dataset/train.csv')
df_test = pd.read_csv('Dataset/test.csv')
df_train.head()
df_train.shape,df_test.shape
labels = df_train.iloc[:,-2:]
id_test = df_test['pet_id']
df_train = df_train.drop(['breed_category','pet_category'],axis=1)
df_all = pd.concat([df_train,df_test],axis=0,ignore_index=True)
def missing_percentage(df):
    total = df.isnull().sum().sort_values(ascending=False)
    percent = total/len(df)*100
    df = pd.concat([total,percent],axis=1,keys=['Total','Percent'])
    df = df[df['Percent']!=0]
    return df.sort_values(by='Percent',ascending=False)
missing_percentage(df_all)
df_all['issue_date'] = df_all['issue_date'].apply(lambda x:x.split(' ')[0])

df_all['isd_date'] = df_all['issue_date'].apply(lambda x:x.split('-')[2])
df_all['isd_month'] = df_all['issue_date'].apply(lambda x:x.split('-')[1])
df_all['isd_year'] = df_all['issue_date'].apply(lambda x:x.split('-')[0])

df_all.drop('issue_date',axis=1,inplace=True)
df_all['list_date'] = df_all['listing_date'].apply(lambda x:x.split(' ')[0])
df_all['list_time'] = df_all['listing_date'].apply(lambda x:x.split(' ')[1])

df_all.drop('listing_date',axis=1,inplace=True)
df_all['lst_date'] = df_all['list_date'].apply(lambda x:x.split('-')[2])
df_all['lst_month'] = df_all['list_date'].apply(lambda x:x.split('-')[1])
df_all['lst_year'] = df_all['list_date'].apply(lambda x:x.split('-')[0])
df_all = df_all.drop('list_date',axis=1)

df_all = df_all.drop(['list_time','pet_id'],axis=1)
sns.set_style('whitegrid')

order1 = labels['breed_category'].value_counts()
order2 = order1.index
plt.figure(figsize=(10,7))
sns.countplot(labels['breed_category'],order=order2)

for i in range(order1.shape[0]):
    count = order1[i]
    strg = '{:0.2f}%'.format(100*count/labels.shape[0])
    plt.text(i,count+100,strg,ha='center')
plt.figure(figsize=(10,7))
sns.countplot(labels['pet_category'])
df_all['isd_date'] = df_all['isd_date'].astype('int64')
df_all['lst_date'] = df_all['lst_date'].astype('int64')
df_all.head()
def value_counts(df):
    a = {}
    for column in df.columns:
        a[column] = len(df[column].value_counts())
    return a
def Categorical(df):
    categories = []
    for column in df.columns:
        if df[column].dtype =='object' or len(df[column].value_counts()) < 32:
            categories.append(column)
    return categories
df_all['isd_month'] = df_all['isd_month'].astype('int')
df_all['lst_month'] = df_all['lst_month'].astype('int')
def month_to_date(n):
    total = 0
    memo = {1:31,2:28,3:31,4:30}
    memo[6] = memo[9] = memo[11] = memo[4]
    memo[5] = memo[7] = memo[8] = memo[10] = memo[12] = memo[1]
    
    for i in range(1,n+1):
        total = total+memo[i]
    return total
df_all['monthindate'] = df_all['isd_month'].apply(month_to_date)
df_all['isd D-M'] = df_all['monthindate']+df_all['isd_date']
df_all.drop('monthindate',axis=1,inplace=True)


df_all['monthindate'] = df_all['lst_month'].apply(month_to_date)
df_all['lst D-M'] = df_all['monthindate']+df_all['lst_date']
df_all.drop('monthindate',axis=1,inplace=True)
df_all['length(cm)'] = df_all['length(m)'].apply(lambda x:x*100)
df_all['area'] = df_all['length(cm)']*df_all['height(cm)']

df_all['X1+X2'] = df_all['X1']+df_all['X2']
df_all.head()
onehot = pd.get_dummies(df_all['color_type'],drop_first=True)
df_all.drop('color_type',axis=1,inplace=True)
df_all = pd.concat([df_all,onehot],axis=1)
df_all.head()
for column in df_all.columns:
    if df_all[column].dtype != 'int64' and df_all[column].dtype != 'float64':
        df_all[column] = df_all[column].astype('int64')
train = df_all.iloc[0:len(df_train),:]
test = df_all.iloc[len(df_train):,:]
X_train, X_test, y_train, y_test = train_test_split(train, labels,test_size=0.2,stratify=labels,random_state=0)
def total_score(f1,f2):
    score = 100*(f1+f2)/2
    return score
y_clf1 = y_train.iloc[:,0] 
y_clf2 = y_train.iloc[:,1]

X_clf2 = np.concatenate([X_train,y_clf1.values.reshape(-1,1)],axis=1)

def clf2_predict(X):
    return np.concatenate([X_test,X.reshape(-1,1)],axis=1)
xgb1 = XGBClassifier()
xgb1.fit(X_train,y_clf1)

y_pred_xgb1 = xgb1.predict(X_test)
f1 = metrics.f1_score(y_test.iloc[:,0],y_pred_xgb1,average='weighted')


xgb2 = XGBClassifier()
xgb2.fit(X_clf2,y_clf2)

y_pred_xgb2 = xgb2.predict(clf2_predict(y_pred_xgb1))
f2 = metrics.f1_score(y_test.iloc[:,1],y_pred_xgb2,average='weighted')
print(f1,f2)
print(total_score(f1,f2))
lgb1 = LGBMClassifier()
lgb1.fit(X_train,y_clf1)

y_pred_lgb1 = lgb1.predict(X_test)
lgb_f1 = metrics.f1_score(y_test.iloc[:,0],y_pred_lgb1,average='weighted')


lgb2 = LGBMClassifier()
lgb2.fit(X_clf2,y_clf2)

y_pred_lgb2 = lgb2.predict(clf2_predict(y_pred_lgb1))
lgb_f2 = metrics.f1_score(y_test.iloc[:,1],y_pred_lgb2,average='weighted')
print(lgb_f1,lgb_f2)
print(total_score(lgb_f1,lgb_f2))
cv = StratifiedKFold(n_splits=4)
f1 = cross_val_score(lgb1,X_train,y_clf1,scoring='f1_weighted',cv=cv)
f2 = cross_val_score(lgb2,X_clf2,y_clf2,scoring='f1_weighted',cv=cv)
print(f1,f2)
print(f1.mean(),f1.std())
print(f2.mean(),f2.std())
total_score(f1.mean(),f2.mean())
cb1 = CatBoostClassifier()
cb1.fit(X_train,y_clf1)

y_pred_cb1 = cb1.predict(X_test)
cb_f1 = metrics.f1_score(y_test.iloc[:,0],y_pred_cb1,average='weighted')


cb2 = CatBoostClassifier()
cb2.fit(X_clf2,y_clf2)

y_pred_cb2 = cb2.predict(clf2_predict(y_pred_cb1))
cb_f2 = metrics.f1_score(y_test.iloc[:,1],y_pred_cb2,average='weighted')
print(cb_f1,cb_f2)
print(total_score(cb_f1,cb_f2))
vot_clf = VotingClassifier(estimators=[('xgboost',xgb1),
                                       ('lgb',lgb1),
                                       ('cb',cb1)], voting='soft')
vot_clf.fit(X_train,y_clf1)

y_pred_vot1 = vot_clf.predict(X_test)
vot_f1 = metrics.f1_score(y_test.iloc[:,0],y_pred_vot1,average='weighted')


vot_clf2 = VotingClassifier(estimators=[('xgboost',xgb2),
                                        ('lgb',lgb2),
                                        ('cb',cb1)], voting='soft')
vot_clf2.fit(X_clf2,y_clf2)

y_pred_vot2 = vot_clf2.predict(clf2_predict(y_pred_vot1))
vot_f2 = metrics.f1_score(y_test.iloc[:,1],y_pred_vot2,average='weighted')
total_score(vot_f1,vot_f2)
predictions1 = vot_clf.predict(test)
predictions2 = vot_clf2.predict(np.concatenate([test,predictions1.reshape(-1,1)],axis=1))
sub = pd.DataFrame({
                    "pet_id":id_test,
                    "breed_category":predictions1,
                    "pet_category":predictions2
})

sub.head()
sub['breed_category'] = sub['breed_category'].astype('int64')
sub.to_csv('Submission.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1732915872583719, 'fraction_numerical': 0.03416825483256194, 'mean_word_length': 6.112294288480155, 'pattern_counts': {'":': 3, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '280889', 'license': 'Apache-2.0', 'n_tokens_mistral': 3153, 'n_tokens_neox': 3082, 'n_words': 381, 'path': 'Adopt a buddy v2.ipynb', 'repo_name': 'Dheeraj2002/Hackerearth-Pet-Adoption-Challenge', 'size': '7346'}