Kaggle competition: PetFinder.my Adoption Prediction
<SOS> metric: QuadraticWeightedKappa Kaggle data source: petfinder.my-adoption-prediction<categorify>
warnings.filterwarnings('ignore')
 sns.set(style="darkgrid" )
for f in df_train.drop('isFraud', axis=1 ).columns:
if df_train[f].dtype=='object' or df_test[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(df_train[f].values)+ list(df_test[f].values))
df_train[f] = lbl.transform(list(df_train[f].values))
df_test[f] = lbl.transform(list(df_test[f].values))<feature_engineering>
train = pd.read_csv(".. /input/train/train.csv")
 test = pd.read_csv(".. /input/test/test.csv" )
df_train['Trans_min_mean'] = df_train['TransactionAmt'] - df_train['TransactionAmt'].mean()
df_train['Trans_min_std'] = df_train['Trans_min_mean'] / df_train['TransactionAmt'].std()
df_test['Trans_min_mean'] = df_test['TransactionAmt'] - df_test['TransactionAmt'].mean()
df_test['Trans_min_std'] = df_test['Trans_min_mean'] / df_test['TransactionAmt'].std()<categorify>
def generate_groupBY_data(col_name):
l = train.groupby(['AdoptionSpeed', col_name])[['PetID']].count().reset_index().rename(
 columns={'PetID':'count'})
 count_pets = train.groupby(['AdoptionSpeed', col_name])[['PetID']].count().reset_index().groupby(
 [col_name] ).sum() [['PetID']].reset_index()
new_col_name = 'total_pets_by' + col_name
count_pets.rename(columns={'PetID': new_col_name}, inplace=True)
 temp = l.merge(count_pets, on=[col_name], how='left')
 temp['fraction'] = temp['count'] * 100 / temp[new_col_name]
temp = temp.pivot("AdoptionSpeed", col_name, "fraction")
 return temp
df_train['TransactionAmt_to_mean_card1'] = df_train['TransactionAmt'] / df_train.groupby(['card1'])['TransactionAmt'].transform('mean')
 df_train['TransactionAmt_to_mean_card4'] = df_train['TransactionAmt'] / df_train.groupby(['card4'])['TransactionAmt'].transform('mean')
 df_train['TransactionAmt_to_std_card1'] = df_train['TransactionAmt'] / df_train.groupby(['card1'])['TransactionAmt'].transform('std')
 df_train['TransactionAmt_to_std_card4'] = df_train['TransactionAmt'] / df_train.groupby(['card4'])['TransactionAmt'].transform('std')
 df_test['TransactionAmt_to_mean_card1'] = df_test['TransactionAmt'] / df_test.groupby(['card1'])['TransactionAmt'].transform('mean')
 df_test['TransactionAmt_to_mean_card4'] = df_test['TransactionAmt'] / df_test.groupby(['card4'])['TransactionAmt'].transform('mean')
 df_test['TransactionAmt_to_std_card1'] = df_test['TransactionAmt'] / df_test.groupby(['card1'])['TransactionAmt'].transform('std')
 df_test['TransactionAmt_to_std_card4'] = df_test['TransactionAmt'] / df_test.groupby(['card4'])['TransactionAmt'].transform('std' )<feature_engineering>
train['Name'].fillna('None', inplace=True)
 train['Description'].fillna('None', inplace=True)
 test['Name_absent'] = test['Name'].isnull()
test['Name_absent'] = test['Name_absent'].astype(int)
 test['Description_None'] = test['Description'].isnull()
test['Description_None'] = test['Description_None'].astype(int)
 test['Name'].fillna('None', inplace=True)
 test['Description'].fillna('None', inplace=True )
df_train['TransactionAmt'] = np.log(df_train['TransactionAmt'])
 df_test['TransactionAmt'] = np.log(df_test['TransactionAmt'] )<concatenate>
tfidf_vectorizer = TfidfVectorizer(max_features=100, stop_words='english', use_idf=True)
 tfidf_matrix = tfidf_vectorizer.fit_transform(list(train['Description'].values)+
list(test['Description'].values))
cols = tfidf_vectorizer.get_feature_names()
tfidf_train = pd.DataFrame(tfidf_matrix.toarray() [0:train.shape[0],], columns=cols)
 tfidf_test = pd.DataFrame(tfidf_matrix.toarray() [train.shape[0]:,], columns=cols )
df_test['isFraud'] = 'test'
df = pd.concat([df_train, df_test], axis=0, sort=False)
 df = df.reset_index()
df = df.drop('index', axis=1 )<drop_column>
train = pd.concat([train, tfidf_train], axis=1)
 test = pd.concat([test, tfidf_test], axis=1 )
def PCA_change(df, cols, n_components, prefix='PCA_', rand_seed=4):
pca = PCA(n_components=n_components, random_state=rand_seed)
 principalComponents = pca.fit_transform(df[cols])
 principalDf = pd.DataFrame(principalComponents)
 df.drop(cols, axis=1, inplace=True)
 principalDf.rename(columns=lambda x: str(prefix)+str(x), inplace=True)
 df = pd.concat([df, principalDf], axis=1)
 return df<feature_engineering>
categorical_cols = ['Type', 'Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3', 'MaturitySize', 'FurLength',
'Vaccinated', 'Dewormed', 'Sterilized', 'Health', 'State', 'Name_absent', 'Description_None']
categorical_features_lgbm = []
for col in categorical_cols:
categorical_features_lgbm.append('name:' + col)
 train[col] = train[col].astype('category')
 test[col] = test[col].astype('category' )
for col in mas_v:
df[col] = df[col].fillna(( df[col].min() - 2))
df[col] =(minmax_scale(df[col], feature_range=(0,1)))
 df = PCA_change(df, mas_v, prefix='PCA_V_', n_components=30 )<drop_column>
X_train = train.drop(['Name', 'RescuerID', 'Description', 'PetID', 'AdoptionSpeed'], axis=1)
 y_train = train['AdoptionSpeed']
X_test = test.drop(['Name', 'RescuerID', 'Description'], axis=1 )
df = reduce_mem_usage(df )<drop_column>
param = {'num_leaves': 38,
'min_data_in_leaf': 146,
'objective':'multiclass',
'num_class': 5,
'max_depth': 4,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9980062052116254,
"bagging_freq": 1,
"bagging_fraction": 0.844212672233457,
"bagging_seed": 11,
"metric": 'multi_logloss',
"lambda_l1": 0.12757257166471625,
"random_state": 133,
"verbosity": -1}
folds = KFold(n_splits=5, shuffle=True, random_state=15)
 oof = np.zeros(len(X_train))
start = time.time()
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X_train.values, y_train.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(X_train.iloc[trn_idx],
label=y_train.iloc[trn_idx] 
)
 val_data = lgb.Dataset(X_train.iloc[val_idx],
label=y_train.iloc[val_idx] 
)
 num_round = 10000
clf = lgb.train(param,
trn_data,
10000,
valid_sets = [trn_data, val_data],
verbose_eval=500,
early_stopping_rounds = 200)
 oof[val_idx] =(pd.DataFrame(clf.predict(X_train.iloc[val_idx],
num_iteration=clf.best_iteration)).idxmax(axis=1))
print("CV score: {:<8.5f}".format(cohen_kappa_score(oof, y_train, weights="quadratic")) )
<drop_column><EOS> 
p =(pd.DataFrame(clf.predict(X_test.drop(['PetID'], axis=1),
num_iteration=clf.best_iteration)).idxmax(axis=1))
test['AdoptionSpeed'] = p
test[['PetID', 'AdoptionSpeed']].to_csv("submission_lgbm_bayes_optimization.csv", index=False )