Kaggle competition: Personality Profile Prediction
df_train["Fare"] = df_train["Fare"].map(lambda i:np.log(i)if i>0 else 0 )<count_missing_values>
warnings.filterwarnings('ignore')
 class bold:
START = '\033[1m'
END = '\033[0m'
print('\t'+'\t'+bold.START+ 'What Is Your Myers Briggs Personality Type?' + bold.END)
 HTML("<iframe width='560' height='315' src='https://www.youtube.com/embed/M4YLO-2Tb2w/' frameborder='0' allowfullscreen></iframe>")
df_train["Age"].isnull().sum()<feature_engineering>
warnings.filterwarnings('ignore' )
df_train["Initial"] = df_train["Name"].str.extract("([A-Za-z]+)\.")
 df_test["Initial"] = df_test["Name"].str.extract("([A-Za-z]+)\." )<categorify>
df_train = pd.read_csv('.. /input/train.csv' )
df_train["Initial"].replace(["Mlle","Mme", "Ms", "Dr","Major","Lady","Countess", "Jonkheer", "Col", "Rev", "Capt", "Sir", "Don", "Dona"],
["Miss", "Miss","Miss", "Mr", "Mr", "Mrs", "Mrs", "Other", "Other", "Other", "Mr", "Mr", "Mr", "Mr"], inplace = True)
 df_test["Initial"].replace(["Mlle","Mme", "Ms", "Dr","Major","Lady","Countess", "Jonkheer", "Col", "Rev", "Capt", "Sir", "Don", "Dona"],
["Miss", "Miss","Miss", "Mr", "Mr", "Mrs", "Mrs", "Other", "Other", "Other", "Mr", "Mr", "Mr", "Mr"], inplace = True )<groupby>
df_train['Mind'] = df_train['type'].str[0]
df_train['Energy'] = df_train['type'].str[1]
df_train['Nature'] = df_train['type'].str[2]
df_train['Tactics'] = df_train['type'].str[3]
df_train = df_train.replace({'Mind' : {'I':0, 'E':1},
'Energy' : {'S':0, 'N':1},
'Nature' : {'F':0, 'T':1},
'Tactics' : {'P':0, 'J':1}})
 df_train = df_train.drop('type', axis = 1 )
df_train.groupby("Initial" ).mean()<groupby>
df_test = pd.read_csv('.. /input/test.csv')
 len_train = len(df_train)
 df_all = pd.concat([df_train, df_test], sort=False )
df_all.groupby("Initial" ).mean() 
<feature_engineering>
tfv = TfidfVectorizer(stop_words='english',max_features=10000,lowercase=True,max_df=0.75,ngram_range=(1,2))
X = tfv.fit_transform(df_all['posts'] )
df_train.loc[(df_train["Age"].isnull())&(df_train["Initial"] == "Mr"), "Age"] = 33
df_train.loc[(df_train["Age"].isnull())&(df_train["Initial"] == "Master"),"Age"] = 5
df_train.loc[(df_train["Age"].isnull())&(df_train["Initial"] == "Miss"), "Age"] = 22
df_train.loc[(df_train["Age"].isnull())&(df_train["Initial"] == "Mrs"), "Age"] = 37
df_train.loc[(df_train["Age"].isnull())&(df_train["Initial"] == "Other"), "Age"] = 45
df_test.loc[(df_test["Age"].isnull())&(df_test["Initial"] == "Mr"), "Age"] = 33
df_test.loc[(df_test["Age"].isnull())&(df_test["Initial"] == "Master"),"Age"] = 5
df_test.loc[(df_test["Age"].isnull())&(df_test["Initial"] == "Miss"), "Age"] = 22
df_test.loc[(df_test["Age"].isnull())&(df_test["Initial"] == "Mrs"), "Age"] = 37
df_test.loc[(df_test["Age"].isnull())&(df_test["Initial"] == "Other"), "Age"] = 45 
<count_missing_values>
train_X = X[:len_train]
df_train["Age"].isnull().sum()<count_missing_values>
y_mind = df_train['Mind']
y_energy = df_train['Energy']
y_nature = df_train['Nature']
y_tactics = df_train['Tactics']
df_test["Age"].isnull().sum()<count_missing_values>
lr = LogisticRegression(n_jobs = -1)
 rf = RandomForestClassifier(n_jobs=-1)
 adb = AdaBoostClassifier()
knn = KNeighborsClassifier(n_jobs=-1 )
df_train["Embarked"].isnull().sum()<categorify>
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5)
 print("Accuracy(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5)
 print("Accuracy(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5)
 print("Accuracy(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5)
 print("Accuracy(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
df_train["Embarked"].fillna("S", inplace = True)
<count_missing_values>
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='f1_macro')
 print("F1(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='f1_macro')
 print("F1(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='f1_macro')
 print("F1(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='f1_macro')
 print("F1(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
df_test["Embarked"].isnull().sum()<feature_engineering>
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='neg_log_loss')
 print("Log Loss(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='neg_log_loss')
 print("Log Loss(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='neg_log_loss')
 print("Log Loss(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='neg_log_loss')
 print("Log Loss(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
df_train["Age_Categ"] = 0
df_test["Age_Categ"] = 0 
<categorify>
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
def category_age(x):
if x < 10:
return 0
elif x < 20:
return 1
elif x < 30:
return 2
elif x < 40:
return 3
elif x < 50:
return 4
elif x < 60:
return 5
elif x < 70:
return 6
else:
return 7 
<feature_engineering>
test_X = X[len_train:]
df_train["Age_Categ"] = df_train["Age"].apply(category_age)
 df_test["Age_Categ"] = df_test["Age"].apply(category_age)
<drop_column>
lr_mind = LogisticRegression(n_jobs=-1)
 lr_energy = LogisticRegression(n_jobs=-1)
 lr_nature = LogisticRegression(n_jobs=-1)
 lr_tactics = LogisticRegression(n_jobs=-1 )
df_train.drop(["Age"], axis = 1 ,inplace = True)
 df_test.drop(["Age"], axis = 1, inplace = True )<categorify>
lr_mind.fit(train_X,y_mind)
 lr_energy.fit(train_X,y_energy)
 lr_nature.fit(train_X,y_nature)
 lr_tactics.fit(train_X,y_tactics )
df_train["Initial"] = df_train["Initial"].map({"Master" : 0, "Miss" : 1, "Mr" : 2, "Mrs" : 3, "Other" : 4})
 df_test["Initial"] = df_test["Initial"].map({"Master" : 0, "Miss" : 1, "Mr" : 2, "Mrs" : 3, "Other" : 4} )<count_values>
submission = pd.DataFrame({'id': df_test.id.values, 'mind': lr_mind.predict(test_X), 'energy': lr_energy.predict(test_X), 'nature': lr_nature.predict(test_X), 'tactics': lr_tactics.predict(test_X)} )
df_train["Embarked"].value_counts()<categorify>
mind_prob = lr_mind.predict_proba(test_X)
 mind_t = mind_prob[:,0].mean()
energy_prob = lr_energy.predict_proba(test_X)
 energy_t = energy_prob[:,0].mean()
nature_prob = lr_nature.predict_proba(test_X)
 nature_t = nature_prob[:,0].mean()
tactics_prob = lr_tactics.predict_proba(test_X)
 tactics_t = tactics_prob[:,0].mean()
mind_threshold = np.percentile(mind_prob[:,1], mind_t*100,)
 energy_threshold = np.percentile(energy_prob[:,1], energy_t*100,)
 nature_threshold = np.percentile(nature_prob[:,1], nature_t*100,)
 tactics_threshold = np.percentile(tactics_prob[:,1], tactics_t*100,)
 mind_pred_threshold =(mind_prob[:,1] > mind_threshold ).astype(int)
 energy_pred_threshold =(energy_prob[:,1] > energy_threshold ).astype(int)
 nature_pred_threshold =(nature_prob[:,1] > nature_threshold ).astype(int)
 tactics_pred_threshold =(tactics_prob[:,1] > tactics_threshold ).astype(int )
df_train["Embarked"] = df_train["Embarked"].map({"C" : 0, "Q" : 1, "S" : 2})
 df_test["Embarked"] = df_test["Embarked"].map({"C" : 0, "Q" : 1, "S" : 2} )<categorify>
submission = pd.DataFrame({'id': df_test.id.values, 'mind': mind_pred_threshold, 'energy': energy_pred_threshold, 'nature': nature_pred_threshold, 'tactics': tactics_pred_threshold} )
<define_variables><EOS> 
submission.to_csv('submission.csv', index=False )