Kaggle competition: Predicting user conversions
<SOS> metric: auc Kaggle data source: predicting-user-conversions<data_type_conversions>
pd.set_option('display.max_columns', 999)
 warnings.filterwarnings(action='ignore', category=DataConversionWarning )
data.Age=data.Age.fillna(data.Age.mean())
 data.Embarked=data.Embarked.fillna(data.Embarked.mode() [0])
 test.Age=test.Age.fillna(test.Age.mean() )<count_missing_values>
events = pd.read_csv('.. /input/events_up_to_01062018.csv', low_memory=False)
 labels = pd.read_csv('.. /input/labels_training_set.csv')
 test = pd.read_csv('.. /input/trocafone_kaggle_test.csv' )
( data.Cabin.isnull().sum() /(data.shape[0])) *100<drop_column>
def evaluate_model(y_true, model=None, X_test=None, prediction=None, probabilites=None):
if model is not None:
if prediction is None:
prediction = model.predict(X_test)
 if probabilites is None:
probabilites = model.predict_proba(X_test)[:, 1]
if prediction is not None:
print('Accuracy: ', accuracy_score(y_true, prediction))
print('ROC AUC Predict: ', roc_auc_score(y_true, prediction))
if probabilites is not None:
print('Avg Log loss: ', log_loss(y_true, probabilites))
print('Sum Log loss: ', log_loss(y_true, probabilites, normalize=False))
print('ROC AUC Proba: ', roc_auc_score(y_true, probabilites))
data=data.drop(['Cabin'],1)
 data.sample(3 )<filter>
all_false = np.zeros(len(labels))
evaluate_model(labels.label, prediction=all_false, probabilites=all_false )
a1=data[(data.Age <= 10)&(data.Survived == 1)].count() [0]
a2=data[(( data.Age>10)&(data.Age<=20)) &(data.Survived==1)].count() [0]
a3=data[(( data.Age>20)&(data.Age<=30)) &(data.Survived==1)].count() [0]
a4=data[(( data.Age>30)&(data.Age<=40)) &(data.Survived==1)].count() [0]
a5=data[(( data.Age>40)&(data.Age<=50)) &(data.Survived==1)].count() [0]
a6=data[(( data.Age>50)&(data.Age<=60)) &(data.Survived==1)].count() [0]
a7=data[(( data.Age>60)&(data.Age<=70)) &(data.Survived==1)].count() [0]
a8=data[(( data.Age>70)&(data.Age<=80)) &(data.Survived==1)].count() [0]
a9=data[(data.Age>80)&(data.Survived==1)].count() [0]
plt.figure(figsize=(8,6))
plt.pie([a1,a2,a3,a4,a5,a6,a7,a8,a9],labels=['0-10 ','10-20','20-30','30-40','40-50','50-60','60-70','70-80','80 above'],autopct='%1.1f%%', shadow=True, startangle=140,radius=1.6)
 plt.show() 
<drop_column>
full_train = get_train_set(events )
data=data.drop(['Name','Ticket','Fare','Parch'],1)
 test=test.drop(['Name','Ticket','Fare','Parch','Cabin'],1 )<categorify>
train = full_train.loc[labels.person]
assert all(train.index == labels.person )
le = LabelEncoder()
data['Sex']= le.fit_transform(data['Sex'])
 data['Embarked']= le.fit_transform(data['Embarked'])
 test['Sex']= le.fit_transform(test['Sex'])
 test['Embarked']= le.fit_transform(test['Embarked'])
<prepare_x_and_y>
scaler = StandardScaler()
features = scaler.fit_transform(train)
 features.shape
x=data.drop(['Survived'],1)
 y=data['Survived']<train_model>
X_train, y_train = features, labels.label
model = RandomForestClassifier(n_estimators=1300, min_samples_leaf=16,bootstrap = True,
max_features = 'sqrt')
 model.fit(x,y)
 prediction=model.predict(test )<load_from_csv>
y_test = test.set_index('person')
 X_test = scaler.transform(full_train.loc[y_test.index] )
sub=pd.read_csv('.. /input/titanic/gender_submission.csv')
 Yt=sub['Survived'].values<compute_test_metric>
print("Accuaracy:",accuracy_score(Yt, prediction))
print("Precision:", precision_score(Yt, prediction)) 
<load_from_csv>
lgb_model = lgb.LGBMClassifier(boosting_type='dart', num_leaves=5, n_estimators=1000, metric='AUC',
learning_rate=0.05, colsample_bytree=0.9)
 lgb_model.fit(X_train, y_train )
testf=pd.read_csv('.. /input/titanic/test.csv' )<save_to_csv>
evaluate_model(y_train, lgb_model, X_train )
<import_modules><EOS> 
conversion_probs = lgb_model.predict_proba(X_test)[:, 1]
subm = pd.DataFrame({'person': y_test.index, 'label': conversion_probs})
 subm.to_csv('submission.csv', index=False )