Kaggle competition: Seleksi Calon Asisten GAIB
<SOS> metric: meanfscore Kaggle data source: seleksi-calon-asisten-gaib<categorify>
def get_null(train):
columns = train.columns
values = []
features = []
for column in columns:
size = train[train[column].isnull() ].shape[0]
if(size != 0):
features.append(column)
 values.append(size)
 return pd.DataFrame({
'features': features,
'values': values
} )
label = LabelEncoder()
ship['Title_Code'] = label.fit_transform(ship['Title'])
 print('Fit Transform function has been run.')
 ship['Title_Code'].head()<drop_column>
def range_quantile(column):
q1 = train[column].quantile(0.25)
 q2 = train[column].quantile(0.5)
 q3 = train[column].quantile(0.75)
 print(train[train[column] == q1]['isChurned'].value_counts())
 print(train[(train[column] > q1)&(train[column] <= q2)]['isChurned'].value_counts())
 print(train[(train[column] > q2)&(train[column] <= q3)]['isChurned'].value_counts())
 print(train[train[column] > q3]['isChurned'].value_counts())
 return q1, q2, q3
ship.drop(['Name'],axis=1,inplace=True)
 print('Name column has been dropped.' )<count_values>
warnings.filterwarnings('ignore')
title_data = {}
title_data['Mr - Survived'] = ship['Title_Code'].loc[(ship['Title_Code'] == 3)&(ship['Survived'] == 1)].count()
title_data['Mr - Deceased'] = ship['Title_Code'].loc[(ship['Title_Code'] == 3)&(ship['Survived'] == 0)].count()
title_data['Miss - Survived'] = ship['Title_Code'].loc[(ship['Title_Code'] == 2)&(ship['Survived'] == 1)].count()
title_data['Miss - Deceased'] = ship['Title_Code'].loc[(ship['Title_Code'] == 2)&(ship['Survived'] == 0)].count()
title_data['Mrs - Survived'] = ship['Title_Code'].loc[(ship['Title_Code'] == 4)&(ship['Survived'] == 1)].count()
title_data['Mrs - Deceased'] = ship['Title_Code'].loc[(ship['Title_Code'] == 4)&(ship['Survived'] == 0)].count()
title_data['Master - Survived'] = ship['Title_Code'].loc[(ship['Title_Code'] == 0)&(ship['Survived'] == 1)].count()
title_data['Master - Deceased'] = ship['Title_Code'].loc[(ship['Title_Code'] == 0)&(ship['Survived'] == 0)].count()
title_data['Misc - Survived'] = ship['Title_Code'].loc[(ship['Title_Code'] == 1)&(ship['Survived'] == 1)].count()
title_data['Misc - Deceased'] = ship['Title_Code'].loc[(ship['Title_Code'] == 1)&(ship['Survived'] == 0)].count()
plt.figure(figsize=(16,4))
plt.bar(list(title_data.keys()),(list(title_data.values())))
 plt.show()
title_data<categorify>
train = pd.read_csv('.. /input/seleksidukungaib/train.csv')
 test = pd.read_csv('.. /input/seleksidukungaib/test.csv')
 print(train.shape)
 print(test.shape )
title_dummies_titanic = pd.get_dummies(ship['Title'])
 title_dummies_titanic.drop(['Misc', 'Mr'], axis=1, inplace=True)
 ship.drop(['Title', 'Title_Code'],axis=1,inplace=True)
 ship = ship.join(title_dummies_titanic)
 print('Title column has been dropped."Master", "Mrs", and "Miss" has been added.' )<filter>
train['isChurned'].value_counts()
print('Number of people who survived with a Fare > 50: ',
len(ship['Survived'].loc[(ship['Fare'] > 50)&(ship['Survived'] == 1)]))
print('Number of people who died with a Fare > 50: ',
len(ship['Survived'].loc[(ship['Fare'] > 50)&(ship['Survived'] == 0)])) 
<feature_engineering>
train.drop(columns=['idx'], inplace=True)
 test.drop(columns=['idx'], inplace=True )
ship['High_Fare'] = [1 if x > 50 else 0 for x in ship['Fare']]
print('High Fare column created.' )<drop_column>
print(train[train['userId'].duplicated() ]['userId'].unique().shape[0])
 print(test[test['userId'].duplicated() ]['userId'].unique().shape[0] )
ship.drop(['Age_was_missing'],axis=1,inplace=True)
 print('Dropped "Age_was_missing" column from dataset.' )<drop_column>
ddc = train[['date_collected', 'date']]
ship.drop(['Age'],axis=1,inplace=True )<import_modules>
print(ddc.shape[0])
 print(ddc[ddc.duplicated() ].shape[0] )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier<filter>
train.drop(columns=['date'], inplace=True)
 test.drop(columns=['date'], inplace=True )
df_tr = ship[ship.Survived.notnull() ]
df_tr.head()<drop_column>
train['userId'].value_counts()
df_te = ship[ship.Survived.isnull() ]
df_te.drop(['Survived'], axis=1, inplace=True)
 df_te = df_te.reset_index(drop=True)
 df_te.head()<prepare_x_and_y>
duplicate = train[(train.duplicated(subset=['userId', 'date_collected'], keep=False)) ]
null_row = duplicate[(duplicate['max_recharge_trx'].isnull())|(duplicate['average_topup_trx'].isnull())|(duplicate['total_transaction'].isnull())]
count = 0
for i, el in null_row.iterrows() :
temp_df = duplicate[(duplicate['userId'] == el['userId'])&(duplicate['date_collected'] == el['date_collected'])]
idx_recharge, idx_topup, idx_transaction = -1, -1, -1
for j, row in temp_df.iterrows() :
if(~np.isnan(row['max_recharge_trx'])) :
idx_recharge = j
if(~np.isnan(row['average_topup_trx'])) :
idx_topup = j
if(~np.isnan(row['total_transaction'])) :
idx_transaction = j
if(idx_recharge != -1):
train.at[i, 'max_recharge_trx'] = train.at[idx_recharge, 'max_recharge_trx']
if(idx_topup != -1):
train.at[i, 'average_topup_trx'] = train.at[idx_topup, 'average_topup_trx']
if(idx_transaction != -1):
train.at[i, 'total_transaction'] = train.at[idx_transaction, 'total_transaction']
count += 1
if(count % 1000 == 0):
print(count )
x_train = df_tr.drop("Survived",axis=1)
 y_train = df_tr["Survived"]
x_test = df_te.copy()
print('Test and Train ML variables are ready.' )<choose_model_class>
def update_max_recharge_trx(row):
if(np.isnan(row['max_recharge_trx'])) :
row['max_recharge_trx'] = row['average_recharge_trx'] * 2 - row['min_recharge_trx']
return row
train = train.apply(update_max_recharge_trx, axis=1 )
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_test)
 logreg.score(x_train, y_train )<predict_on_test>
def update_average_topup_trx(row):
if(np.isnan(row['average_topup_trx'])) :
if(row['num_topup_trx'] < 2):
row['average_topup_trx'] = row['min_topup_trx']
else:
row['average_topup_trx'] =(row['min_topup_trx'] + row['max_topup_trx'])/2
return row
train = train.apply(update_average_topup_trx, axis=1 )
random_forest = RandomForestClassifier(n_estimators=300)
 random_forest.fit(x_train, y_train)
 y_pred = random_forest.predict(x_test ).astype(int)
 random_forest.score(x_train, y_train )<save_to_csv>
def update_num_transfer(x):
return x['num_transaction'] - x['num_topup_trx'] - x['num_recharge_trx']
df_test['Survived'] = y_pred.astype(int)
 df_test.to_csv('SurvivedList.csv')
 df_test<define_variables>
train['num_transfer_trx'] = train.apply(update_num_transfer, axis=1)
 test['num_transfer_trx'] = test.apply(update_num_transfer, axis=1 )
submission = df_test[["PassengerId", "Survived"]]<save_to_csv>
def update_average_trx(x):
if(x['num_transfer_trx'] == 0):
return 0
if(np.isnan(x['total_transaction'])) :
return np.nan
topup = x['average_topup_trx'] * x['num_topup_trx']
recharge = x['average_recharge_trx'] * x['num_recharge_trx']
total = x['total_transaction']
return(total-topup-recharge)/x['num_transfer_trx']
submission.to_csv('titanic.csv', index=False, header=['PassengerID', 'Survived'] )<set_options>
train['average_transfer_trx'] = train.apply(update_average_trx, axis=1)
 test['average_transfer_trx'] = test.apply(update_average_trx, axis=1 )
%matplotlib inline 
<load_from_csv>
def fill_total_transaction(x):
if(np.isnan(x['total_transaction'])) :
if(np.isnan(x['average_transfer_trx'])) :
return x
x['total_transaction'] =(
(x['average_topup_trx'] * x['num_topup_trx'])+ 
(x['average_recharge_trx'] * x['num_recharge_trx'])+ 
(x['average_transfer_trx'] * x['num_transfer_trx'])
)
 return x
train = train.apply(fill_total_transaction, axis=1 )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
train[train['isActive'].isnull() ]
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
train.drop(train[train['isActive'].isnull() ].index, inplace=True )
y_train = train['label']
x_train = train.drop('label', axis=1)
 y_train.shape, x_train.shape<count_values>
test['premium'].value_counts()
y_train.value_counts()<feature_engineering>
test['premium'] = test['premium'].fillna(False )
x_train = x_train/255.0
test = test/255.0<categorify>
train.drop(columns='isUpgradedUser', inplace=True)
 test.drop(columns='isUpgradedUser', inplace=True )
y_train = pd.DataFrame(data=y_train)
 one_hot = OneHotEncoder(handle_unknown='ignore')
 one_hot.fit(y_train.values)
 y_train = one_hot.transform(y_train.values ).toarray()<split>
train.drop(columns=['random_number'], inplace=True)
 test.drop(columns=['random_number'], inplace=True )
random_seed = 3
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=random_seed)
 x_train.shape, x_val.shape, y_train.shape, y_val.shape<choose_model_class>
model = Sequential([
Conv2D(filters = 64, input_shape=(28,28,1), kernel_size=(3,3), strides=(1,1), padding='valid'),
Activation('relu'),
MaxPooling2D(pool_size=(2,2), strides=(1,1), padding='valid'),
BatchNormalization() ,
Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='valid'),
Activation('relu'),
MaxPooling2D(pool_size=(2,2), strides=(1,1), padding='valid'),
BatchNormalization() ,
Conv2D(filters=192, kernel_size=(1,1), strides=(1,1), padding='valid'),
Activation('relu'),
BatchNormalization() ,
Conv2D(filters=192, kernel_size=(3,3), strides=(1,1), padding='valid'),
Activation('relu'),
BatchNormalization() ,
Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='valid'),
Activation('relu'),
MaxPooling2D(pool_size=(2,2), strides=(1,1), padding='valid'),
BatchNormalization() ,
Flatten() ,
Dense(2048),
Activation('relu'),
Dropout(0.4),
BatchNormalization() ,
Dense(2048),
Activation('relu'),
Dropout(0.4),
BatchNormalization() ,
Dense(800),
Activation('relu'),
Dropout(0.4),
BatchNormalization() ,
Dense(10),
Activation('softmax'),
] )<choose_model_class>
print(train[train.duplicated() ].shape[0])
 print(train[train['total_transaction'].isnull() ].shape[0])
 print(train[train['average_transfer_trx'] < 0].shape[0] )
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
train.drop(train[train['average_transfer_trx'] < 0].index, inplace=True )
model.fit(x_train, y_train, batch_size=200, validation_data=(x_val,y_val), epochs = 10 )<predict_on_test>
train.drop(train[train.duplicated() ].index, inplace=True )
results = model.predict(test )<save_to_csv>
train.drop(train[train['total_transaction'].isnull() ].index, inplace=True )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_submission1.csv",index=False )<import_modules>
print(train[train['num_transfer_trx'] != 0]['isChurned'].value_counts())
 print(train[train['num_transfer_trx'] == 0]['isChurned'].value_counts() )
import numpy as np
import os
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
import optuna
from sklearn import linear_model
from sklearn import metrics
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score
from optuna.samplers import RandomSampler, GridSampler, TPESampler
import sklearn
import xgboost as xgb
from scipy.misc import derivative
from sklearn.metrics import mean_squared_error
import pickle
import category_encoders as ce
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
import random
import lightgbm as lgb
import random<define_variables>
def update_transaction(x):
if(x['num_transaction'] != 0):
x['num_transaction'] -= x['num_transfer_trx']
x['total_transaction'] -=(x['average_transfer_trx'] * x['num_transfer_trx'])
 if(x['total_transaction'] > -1e-8 and x['total_transaction'] < 1e-8):
x['total_transaction'] = 0
return x
train = train.apply(update_transaction, axis=1)
 test = test.apply(update_transaction, axis=1 )
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed )<define_variables>
train.drop(columns=['num_transfer_trx', 'average_transfer_trx', 'min_transfer_trx', 'max_transfer_trx'], inplace=True)
 test.drop(columns=['num_transfer_trx', 'average_transfer_trx', 'min_transfer_trx', 'max_transfer_trx'], inplace=True )
seed_everything(0 )<load_from_csv>
train['average_recharge_trx'] /= 15000
train['min_recharge_trx'] /= 15000
train['max_recharge_trx'] /= 15000
train['average_topup_trx'] /= 15000
train['min_topup_trx'] /= 15000
train['max_topup_trx'] /= 15000
train['total_transaction'] /= 15000
df_train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 df_test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 y_train = np.log(df_train['SalePrice'])
 X_train = df_train.drop(['SalePrice','Id'],axis=1)
 X_test = df_test.drop('Id',axis=1)
 print('Train data: ',X_train.shape)
 print('Test data: ',X_test.shape )<load_pretrained>
test['average_recharge_trx'] /= 15000
test['min_recharge_trx'] /= 15000
test['max_recharge_trx'] /= 15000
test['average_topup_trx'] /= 15000
test['min_topup_trx'] /= 15000
test['max_topup_trx'] /= 15000
test['total_transaction'] /= 15000
cat_nominal_features = pickle.load(open('.. /input/features-housing/cat_nominal_features.p', "rb"))
cat_ordinal_features = pickle.load(open('.. /input/features-housing/cat_ordinal_features.p', "rb"))
num_features = pickle.load(open('.. /input/features-housing/num_features.p', "rb"))
cat_features = cat_nominal_features + cat_ordinal_features
print('Number of numeric features: ',len(num_features))
print('Number of ordinal features: ',len(cat_ordinal_features))
print('Number of nominal featuures: ',len(cat_nominal_features))<data_type_conversions>
train['total_recharge'] = train['average_recharge_trx'] * train['num_recharge_trx']
train['total_topup'] = train['average_topup_trx'] * train['num_topup_trx']
test['total_recharge'] = test['average_recharge_trx'] * test['num_recharge_trx']
test['total_topup'] = test['average_topup_trx'] * test['num_topup_trx']
X_train[cat_ordinal_features] = X_train[cat_ordinal_features].fillna(np.nan)
 X_train[cat_nominal_features] = X_train[cat_nominal_features].fillna(np.nan)
 X_test[cat_ordinal_features] = X_test[cat_ordinal_features].fillna(np.nan)
 X_test[cat_nominal_features] = X_test[cat_nominal_features].fillna(np.nan )<define_variables>
cols = ['userId',
'date_collected',
'num_recharge_trx',
'average_recharge_trx',
'max_recharge_trx',
'min_recharge_trx',
'num_topup_trx',
'average_topup_trx',
'max_topup_trx',
'min_topup_trx',
'total_recharge',
'total_topup',
'num_transaction',
'total_transaction',
'isActive',
'isVerifiedPhone',
'isVerifiedEmail',
'blocked',
'premium',
'super',
'userLevel',
'pinEnabled',
'isChurned']
train = train[cols]
test = test[cols[:-1]]
ord_mapping=[{'col': 'Street', 'mapping': {'Grvl': 1, 'Pave': 2}},
{'col': 'Alley', 'mapping': {np.nan:0,'Grvl': 1, 'Pave': 2}},
{'col': 'Utilities', 'mapping': {'NoSeWa': 1, 'AllPub':2}},
{'col': 'ExterQual', 'mapping': {'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'ExterCond', 'mapping': {'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'BsmtCond', 'mapping': {np.nan:0,'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'BsmtQual', 'mapping': {np.nan:0,'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'BsmtExposure', 'mapping': {np.nan:0,'No':1,'Mn':2,'Av':3,
'Gd':4}},
{'col': 'BsmtFinType1', 'mapping': {np.nan:0,'Unf':1,'LwQ':2,'Rec':3,
'BLQ':4,'ALQ':5,'GLQ':6}},
{'col': 'BsmtFinType2', 'mapping': {np.nan:0,'Unf':1,'LwQ':2,'Rec':3,
'BLQ':4,'ALQ':5,'GLQ':6}},
{'col': 'HeatingQC', 'mapping': {'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'CentralAir', 'mapping': {'Y':1,'N':0}},
{'col': 'KitchenQual', 'mapping': {'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'Functional', 'mapping': {'Typ':8,'Min1':7,'Min2':6,
'Mod':5,'Maj1':4,'Maj2':3,
'Sev':2,"Sal":1}},
{'col': 'FireplaceQu', 'mapping': {np.nan:0,'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'GarageFinish', 'mapping': {np.nan:0,'Unf':1,'RFn':2,'Fin':3}},
{'col': 'GarageQual', 'mapping': {np.nan:0,'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'GarageCond', 'mapping': {np.nan:0,'Po':1,'Fa':2,'TA':3,
'Gd':4,'Ex':5}},
{'col': 'PavedDrive', 'mapping': {'N':1,'P':2,'Y':3}},
{'col': 'PoolQC', 'mapping': {np.nan:0,'Fa':1,'TA':2,
'Gd':3,'Ex':4}},
{'col': 'Fence', 'mapping': {np.nan:0,'MnWw':1,'GdWo':2,'MnPrv':3,
'GdPrv':4}}]
mapping_cols = ['Street','Alley','Utilities','ExterQual','ExterCond','BsmtCond','BsmtQual','BsmtExposure',
'BsmtFinType1','BsmtFinType2','HeatingQC','CentralAir','KitchenQual',
'Functional','FireplaceQu','GarageFinish','GarageQual','GarageCond','PavedDrive',
'PoolQC','Fence']
def construct_ord_nom(ordinal_features,nominal_features):
mapp = []
ord_features = []
for c in ordinal_features:
if c not in mapping_cols:
continue
idx = mapping_cols.index(c)
 mapp.append(ord_mapping[idx])
 ord_features.append(c)
 ce_ord = ce.OrdinalEncoder(cols=ord_features,mapping=mapp,
handle_unknown='return_nan',handle_missing='return_nan')
 ce_nom = ce.OneHotEncoder(cols=nominal_features,handle_unknown='return_nan',handle_missing='return_nan')
 return ce_ord,ce_nom
def get_CT(ord_features,nom_features,num_features_new,ce_ord,ce_nom):
numeric_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='constant',fill_value=-1)) ,
])
 ce_ord, ce_nom = construct_ord_nom(ord_features,nom_features)
 ct1 = ColumnTransformer(
 transformers=[ 
('nominal',ce_nom,nom_features), 
('ordinal',ce_ord,ord_features), 
('num',numeric_transformer,num_features_new)
 ],remainder = 'passthrough')
 return ct1<compute_test_metric>
encode = ['premium', 'super', 'pinEnabled']
lbl = LabelEncoder()
for col in encode:
train[col] = lbl.fit_transform(train[col].values)
 test[col] = lbl.transform(test[col].values )
ce_ord, ce_nom = construct_ord_nom(cat_ordinal_features,cat_nominal_features)
 CT = get_CT(cat_ordinal_features,cat_nominal_features,num_features,ce_ord,ce_nom )<train_model>
train.drop(columns=[
'userId',
'date_collected',
], inplace=True)
 test.drop(columns=[
'userId',
'date_collected',
], inplace=True )
X_train_new = CT.fit_transform(X_train)
 X_test_new = CT.transform(X_test)
 print(X_train_new.shape)
 print(X_test_new.shape)
 dtrain = lgb.Dataset(X_train_new,y_train )<init_hyperparams>
no_dup_train = train.drop(train[train.duplicated() ].index ).copy()
dup_index = no_dup_train[no_dup_train.drop(columns='isChurned' ).duplicated() ].index
class cparams() :
def __init__(self):
self.seed = 0
self.num_iterations = 100
self.learning_rate = 0.1
self.num_leaves = 31
self.min_child_samples = 20
self.min_child_weight = 0.001
self.bagging_fraction = 1.0
self.feature_fraction = 1.0
self.bagging_freq = 0
self.alpha = 0.25
self.gamma = 2.0
self.l1 = 0.0
self.l2 = 0.0
def calibrate(self,num_round):
param = {'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'learning_rate': self.learning_rate,
'num_leaves': self.num_leaves,
'min_data_in_leaf': self.min_child_samples,
'min_sum_hessian_in_leaf':self.min_child_weight,
'bagging_fraction': self.bagging_fraction,
'bagging_freq': self.bagging_freq,
'feature_fraction': self.feature_fraction,
'lambda_l1': self.l1,
'lambda_l2': self.l2,
'seed': self.seed
}
bst = lgb.cv(param, dtrain,num_boost_round=num_round, stratified=False, \
shuffle=True,early_stopping_rounds=100,verbose_eval=10,seed=0)
 return bst
def get_param(self):
param = {'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'learning_rate': self.learning_rate,
'num_leaves': self.num_leaves,
'min_data_in_leaf': self.min_child_samples,
'min_sum_hessian_in_leaf':self.min_child_weight,
'bagging_fraction': self.bagging_fraction,
'bagging_freq': self.bagging_freq,
'feature_fraction': self.feature_fraction,
'lambda_l1': self.l1,
'lambda_l2': self.l2,
'seed': self.seed
}
return param<find_best_params>
def get_same_row(index, data=train):
row = data.loc[index]
return train[(train['num_recharge_trx'] == row['num_recharge_trx'])& 
(train['average_recharge_trx'] == row['average_recharge_trx'])& 
(train['max_recharge_trx'] == row['max_recharge_trx'])& 
(train['min_recharge_trx'] == row['min_recharge_trx'])& 
(train['total_recharge'] == row['total_recharge'])& 
(train['num_topup_trx'] == row['num_topup_trx'])& 
(train['average_topup_trx'] == row['average_topup_trx'])& 
(train['max_topup_trx'] == row['max_topup_trx'])& 
(train['min_topup_trx'] == row['min_topup_trx'])& 
(train['total_topup'] == row['total_topup'])& 
(train['num_transaction'] == row['num_transaction'])& 
(train['total_transaction'] == row['total_transaction'])& 
(train['isActive'] == row['isActive'])& 
(train['isVerifiedPhone'] == row['isVerifiedPhone'])& 
(train['isVerifiedEmail'] == row['isVerifiedEmail'])& 
(train['blocked'] == row['blocked'])& 
(train['premium'] == row['premium'])& 
(train['super'] == row['super'])& 
(train['userLevel'] == row['userLevel'])& 
(train['pinEnabled'] == row['pinEnabled'])]
current_model = cparams()<init_hyperparams>
def update_row(threshold=0.3, dup_index=dup_index):
for a, i in enumerate(dup_index):
dup_row = get_same_row(i)
 index = dup_row.index
zero, one = dup_row['isChurned'].value_counts() [0], dup_row['isChurned'].value_counts() [1]
if(min(zero, one)/(zero+one)<= threshold):
mode = dup_row.mode() ['isChurned'][0]
for idx in index:
train.at[idx, 'isChurned'] = mode
if(a % 100 == 0):
print(a )
params = {
'objective': 'regression',
'metric': 'rmse',
"verbosity": 1,
"boosting_type": "gbdt",
'seed':0
}
eval_history = lgb.cv(
 params, dtrain, verbose_eval=20,
stratified=False, num_boost_round=1000, early_stopping_rounds=100,
nfold=5,seed=0 )<train_model>
train['isChurned'].value_counts()
print('Best score: ', eval_history['rmse-mean'][-1])
 print('Number of estimators: ', len(eval_history['rmse-mean']))
current_model.num_iterations = len(eval_history['rmse-mean'] )<find_best_params>
train['isChurned'].value_counts()
study_name2 = 'lgb_leaves'
study_leaves = optuna.create_study(study_name=study_name2,direction='maximize',sampler=TPESampler(0))<train_on_grid>
df_x = train.drop(['isChurned'], axis=1)
 df_y = train['isChurned']
x = df_x
y = df_y
def opt_leaves(trial):
params = {
'objective': 'regression',
'metric': 'rmse',
"verbosity": 1,
"boosting_type": "gbdt",
'seed':0,
'num_leaves':int(trial.suggest_loguniform("num_leaves", 3,32))
}
score = lgb.cv(
 params, dtrain, verbose_eval=0,
stratified=False, num_boost_round=current_model.num_iterations,
nfold=5,seed=0)
 return -score['rmse-mean'][-1]<find_best_params>
scaler = StandardScaler()
x = scaler.fit_transform(x)
 new_test = scaler.transform(test )
study_leaves.optimize(opt_leaves, n_trials=50 )<train_model>
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42 )
print('Total number of trials: ',len(study_leaves.trials))
trial_leaves = study_leaves.best_trial
print('Best score : {}'.format(-trial_leaves.value))
for key, value in trial_leaves.params.items() :
print(" {}: {}".format(key, value))<find_best_params>
from sklearn.metrics import confusion_matrix, f1_score
current_model.num_leaves = int(trial_leaves.params['num_leaves'] )<find_best_params>
def train_model(models, x=x_train, y=y_train):
for key, model in models.items() :
print(f'Currently Training {key}')
 model.fit(x, y )
study_name3 = 'lgb_child_weight_sample'
study_sample_weight = optuna.create_study(study_name=study_name3,direction='maximize',sampler=TPESampler(0))<init_hyperparams>
def preds(models, x_test=x_test, y_test=y_test, new_test=new_test):
preds = {
'models': [],
'val_f1_score': [],
'train_f1_score': [],
'confusion_matrix': [],
'prediction': []
}
for key, model in models.items() :
preds['models'].append(key)
 test_pred = model.predict(x_test)
 train_pred = model.predict(x_train)
 preds['val_f1_score'].append(f1_score(test_pred, y_test))
preds['train_f1_score'].append(f1_score(train_pred, y_train))
preds['confusion_matrix'].append(confusion_matrix(test_pred, y_test))
preds['prediction'].append(model.predict(new_test))
return pd.DataFrame(preds )
def opt_sample_weight(trial):
params = {
'objective': 'regression',
'metric': 'rmse',
"verbosity": 1,
"boosting_type": "gbdt",
'seed':0,
'num_leaves':current_model.num_leaves,
'min_data_in_leaf':int(trial.suggest_discrete_uniform('data_in_leaf',4,32,q=2)) ,
'min_sum_hessian_in_leaf':trial.suggest_discrete_uniform('min_hessian',0.001,0.003,q=0.0005)
 }
score = lgb.cv(
 params, dtrain, verbose_eval=0,
stratified=False, num_boost_round=current_model.num_iterations,
nfold=5,seed=0)
 return -score['rmse-mean'][-1]<train_model>
evaluation = []
study_sample_weight.optimize(opt_sample_weight, n_trials=50 )<find_best_params>
from sklearn.linear_model import LogisticRegression, Perceptron, RidgeClassifier, SGDClassifier
print('Total number of trials: ',len(study_sample_weight.trials))
trial_sample_weight = study_sample_weight.best_trial
print('Best score : {}'.format(-trial_sample_weight.value))
for key, value in trial_sample_weight.params.items() :
print(" {}: {}".format(key, value))<find_best_params>
linear_models = {
'LogisticRegression': LogisticRegression() ,
'Perceptron': Perceptron() ,
'RidgeClassifier': RidgeClassifier() ,
'SGDClassifier': SGDClassifier()
}
current_model.min_child_samples = int(trial_sample_weight.params['data_in_leaf'])
 current_model.min_child_weight = trial_sample_weight.params['min_hessian']<choose_model_class>
train_model(linear_models )
study_name4 = 'lgb_bagging'
study_bagging = optuna.create_study(study_name=study_name4,direction='maximize',sampler=TPESampler(0))<load_pretrained>
evaluation.append(preds(linear_models))
def opt_bagging(trial):
params = {
'objective': 'regression',
'metric': 'rmse',
"verbosity": 1,
"boosting_type": "gbdt",
'seed':0,
'num_leaves':current_model.num_leaves,
'min_data_in_leaf':current_model.min_child_samples,
'min_sum_hessian_in_leaf':current_model.min_child_weight,
'bagging_fraction': trial.suggest_discrete_uniform('bfrac',0.4,1.0,q=0.05),
'bagging_freq': int(trial.suggest_discrete_uniform('bfreq',1,7,q=1.0)) ,
'feature_fraction':trial.suggest_discrete_uniform('feature',0.4,1.0,q=0.05)
 }
score = lgb.cv(
 params, dtrain, verbose_eval=0,
stratified=False, num_boost_round=current_model.num_iterations,
nfold=5,seed=0)
 return -score['rmse-mean'][-1]<train_model>
from sklearn.naive_bayes import GaussianNB
study_bagging.optimize(opt_bagging, n_trials=50 )<train_model>
naive_bayes = {
'GaussianNB': GaussianNB()
}
print('Total number of trials: ',len(study_bagging.trials))
trial_bagging = study_bagging.best_trial
print('Best score : {}'.format(trial_bagging.value))
for key, value in trial_bagging.params.items() :
print(" {}: {}".format(key, value))<find_best_params>
train_model(naive_bayes )
current_model.bagging_fraction = trial_bagging.params['bfrac']
current_model.bagging_freq = int(trial_bagging.params['bfreq'])
 current_model.feature_fraction = trial_bagging.params['feature']<choose_model_class>
evaluation.append(preds(naive_bayes))
study_name5 = 'l1_l2'
study_reg = optuna.create_study(study_name=study_name5,direction='maximize',sampler=TPESampler(0))<init_hyperparams>
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
def opt_reg(trial):
params = {
'objective': 'regression',
'metric': 'rmse',
"verbosity": 1,
"boosting_type": "gbdt",
'seed':0,
'num_leaves':current_model.num_leaves,
'min_data_in_leaf':current_model.min_child_samples,
'min_sum_hessian_in_leaf':current_model.min_child_weight,
'bagging_fraction': current_model.bagging_fraction,
'bagging_freq': current_model.bagging_freq,
'feature_fraction':current_model.feature_fraction,
'lambda_l1': trial.suggest_loguniform("lambda_l1", 1e-7, 10),
'lambda_l2': trial.suggest_loguniform("lambda_l2", 1e-7, 10)
 }
score = lgb.cv(
 params, dtrain, verbose_eval=0,
stratified=False, num_boost_round=current_model.num_iterations,
nfold=5,seed=0)
 return -score['rmse-mean'][-1]<train_model>
tree_model = {
'DecisionTree': DecisionTreeClassifier() ,
'ExtraTree': ExtraTreeClassifier()
}
study_reg.optimize(opt_reg, n_trials=50 )<find_best_params>
train_model(tree_model )
print('Total number of trials: ',len(study_reg.trials))
trial_reg = study_reg.best_trial
print('Best score : {}'.format(trial_reg.value))
for key, value in trial_reg.params.items() :
print(" {}: {}".format(key, value))<find_best_params>
evaluation.append(preds(tree_model))
current_model.l1 = trial_reg.params['lambda_l1']
current_model.l2 = trial_reg.params['lambda_l2']<train_model>
!pip install catboost
current_model.learning_rate = 0.05
lr1 = current_model.calibrate(10000)
 print('Best score: ', lr1['rmse-mean'][-1])
 print('Number of estimators: ', len(lr1['rmse-mean']))<train_model>
boosting_model = {
'CatBoost': CatBoostClassifier(eval_metric='F1', logging_level='Silent'),
'XGBoost': XGBClassifier() ,
'LGBM': LGBMClassifier() ,
'AdaBoost': AdaBoostClassifier()
}
current_model.learning_rate = 0.01
lr2 = current_model.calibrate(10000)
 print('Best score: ', lr2['rmse-mean'][-1])
 print('Number of estimators: ', len(lr2['rmse-mean']))<find_best_params>
train_model(boosting_model )
current_model.learning_rate = 0.005
lr3 = current_model.calibrate(10000)
 print('Best score: ', lr3['rmse-mean'][-1])
 print('Number of estimators: ', len(lr3['rmse-mean']))<find_best_params>
evaluation.append(preds(boosting_model))
current_model.learning_rate = 0.005
current_param = current_model.get_param()
for key, value in current_param.items() :
print(" {}: {}".format(key, value))<train_model>
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
def cv_training(train_data,y_train_data):
kFold = KFold(n_splits=5, random_state=0, shuffle=True)
 models = []
eval_history = []
oof_pred = []
oof_target = []
scores = []
for fold,(trn_idx, val_idx)in enumerate(kFold.split(train_data)) :
X_train = train_data[trn_idx]
X_val = train_data[val_idx]
y_train = y_train_data[trn_idx]
y_val = y_train_data[val_idx]
dtrain = lgb.Dataset(X_train,y_train)
 dval = lgb.Dataset(X_val,y_val)
 evals_result = {}
model = lgb.train(current_param, dtrain,num_boost_round=5000,
evals_result=evals_result,valid_sets=dval,verbose_eval=20,early_stopping_rounds=100)
 models.append(model)
 y_pred = model.predict(X_val)
 score_temp = np.sqrt(mean_squared_error(y_pred,y_val))
scores.append(score_temp)
 oof_pred.append(y_pred)
 oof_target.append(y_val)
 eval_history.append(evals_result)
 oof_pred = np.concatenate(( oof_pred[0],oof_pred[1],oof_pred[2],
oof_pred[3],oof_pred[4]))
oof_target = np.concatenate(( oof_target[0],oof_target[1],oof_target[2],
oof_target[3],oof_target[4]))
oof_df = pd.DataFrame({'predictions':oof_pred,'target':oof_target})
 return models, eval_history, scores, oof_df<train_model>
ensemble_model = {
'RandomForest': RandomForestClassifier() ,
'ExtraTrees': ExtraTreesClassifier()
}
models, eval_history,scores,oof_df = cv_training(X_train_new,y_train )<save_to_csv>
train_model(ensemble_model )
oof_df.to_csv('oof_df.csv',index=False)
 oof_df.head()<find_best_params>
evaluation.append(preds(ensemble_model))
score = 0.0
for i in range(len(models)) :
print(models[i].best_iteration)
 print(eval_history[i]['valid_0']['rmse'][-1])
 score = score + eval_history[i]['valid_0']['rmse'][-1]
print('Average score: ',score/5 )<train_model>
eval = pd.concat(evaluation ).sort_values('val_f1_score',ascending=False ).reset_index(drop=True)
 eval
model_final = lgb.train(current_param, dtrain,num_boost_round=3920)
 ypred = model_final.predict(X_test_new )<load_from_csv>
from sklearn.model_selection import GridSearchCV
sub = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 submission = pd.DataFrame({
'Id': sub['Id'],
'SalePrice': np.exp(ypred)
 })
 submission.head()<save_to_csv>
from sklearn.model_selection import GridSearchCV
submission.to_csv('submission.csv',index=False )<import_modules>
params = {
'C': np.logspace(-2,2,20),
'solver': ['liblinear', 'lbfgs'],
'penalty': ['l2']
}
import numpy as np
import pandas as pd
import seaborn as seaborn
from sklearn import preprocessing
from sklearn import tree
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt<load_from_csv>
tuning_model = GridSearchCV(estimator = linear_models['LogisticRegression'],
param_grid = params,
verbose=True,
scoring = 'f1',
n_jobs=5 )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.info()
test.info()<categorify>
tuning_model.fit(x, y)
 predict = pd.DataFrame(tuning_model.best_estimator_.predict(new_test))
predict[0].value_counts()
def dummies(col,train,test):
train_dum = pd.get_dummies(train[col])
 test_dum = pd.get_dummies(test[col])
 train = pd.concat([train, train_dum], axis=1)
 test = pd.concat([test,test_dum],axis=1)
 train.drop(col,axis=1,inplace=True)
 test.drop(col,axis=1,inplace=True)
 return train, test
dropping = ['PassengerId', 'Name', 'Ticket']
train.drop(dropping,axis=1, inplace=True)
 test.drop(dropping,axis=1, inplace=True)
<count_values>
print(tuning_model.best_estimator_)
 print(tuning_model.best_params_ )
print(train.Pclass.value_counts())
 seaborn.factorplot("Pclass",'Survived',data=train,order=[1,2,3])
 train, test = dummies('Pclass',train,test )<categorify>
y_pred = predict[0]
print(train.Sex.value_counts(dropna=False))
seaborn.factorplot('Sex','Survived',data=train)
 train,test = dummies('Sex',train,test)
 train.drop('male',axis=1,inplace=True)
 test.drop('male',axis=1,inplace=True)
<count_missing_values>
print(train.Cabin.isnull().sum())
 print(test.Cabin.isnull().sum())
 train.drop('Cabin',axis=1,inplace=True)
 test.drop('Cabin',axis=1,inplace=True )<import_modules>
def compare(col):
count_diff = 0
for i, row in comparison.iterrows() :
if(row['prediction'] != row[col]):
count_diff += 1
return count_diff
def modeling(clf,ft,target):
acc = cross_val_score(clf,ft,target,cv=kf)
 acc_lst.append(acc.mean())
 return
accuracy = []
def ml(ft,target,time):
accuracy.append(acc_lst)
 logreg = LogisticRegression()
modeling(logreg,ft,target)
 rf = RandomForestClassifier(n_estimators=50,min_samples_split=4,min_samples_leaf=2)
 modeling(rf,ft,target)
 svc1 = SVC(kernel="rbf")
 modeling(svc1,ft,target)
 svc2 = SVC(kernel="linear")
 modeling(svc2,ft,target)
 knn = KNeighborsClassifier(n_neighbors = 3)
 modeling(knn,ft,target)
 mlp = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(15,3),random_state=10)
 modeling(mlp, ft, target)
 logreg.fit(ft,target)
 feature = pd.DataFrame(ft.columns)
 feature.columns = ['Features']
feature["Coefficient Estimate"] = pd.Series(logreg.coef_[0])
 print(feature)
 return 
<prepare_x_and_y>
idx = pd.read_csv('.. /input/seleksidukungaib/test.csv')['idx']
train_ft = train.drop('Survived',axis=1)
 train_y = train['Survived']
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft,train_y,'test_1')
<train_model>
submission = pd.DataFrame({
'idx': idx,
'isChurned': y_pred.astype(int)
 } )
train_ft_2=train.drop(['Survived','young'],axis=1)
 test_2 = test.drop('young',axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst=[]
ml(train_ft_2,train_y,'test_2')
<categorify>
submission['isChurned'].value_counts()
train_ft_3=train.drop(['Survived','young','C'],axis=1)
 test_3 = test.drop(['young','C'],axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft_3,train_y,'test_3')
<drop_column>
submission.to_csv('submission.csv', index=False )
train_ft_4=train.drop(['Survived','Fare'],axis=1)
 test_4 = test.drop(['Fare'],axis=1)
 train_ft.head()
kf = KFold(n_splits=3,random_state=1)
 acc_lst = []
ml(train_ft_4,train_y,'test_4')
<create_dataframe>
submission.to_csv('submission.csv', index=False )
<save_to_csv><EOS> 
!kaggle competitions submit -c seleksidukungaib -f submission.csv -m "No Comment"