Kaggle competition: Personality Profile Prediction
<SOS> metric: meancolumnwiselogloss Kaggle data source: personality-profile-prediction<filter>
%matplotlib inline
warnings.filterwarnings('ignore' )
print("Number of people embarking in Southampton(S):")
 southampton = df[df["Embarked"] == "S"].shape[0]
print(southampton)
 print("Number of people embarking in Cherbourg(C):")
 cherbourg = df[df["Embarked"] == "C"].shape[0]
print(cherbourg)
 print("Number of people embarking in Queenstown(Q):")
 queenstown = df[df["Embarked"] == "Q"].shape[0]
print(queenstown )<drop_column>
train_df = pd.read_csv('.. /input/train.csv' )
df=df.drop(['Ticket'],axis=1)
 test=test.drop(['Ticket'],axis=1)
 test.columns<feature_engineering>
test_df=pd.read_csv('.. /input/test.csv' )
combine = [df, test]
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 print(pd.crosstab(df['Title'], df['Sex']))<feature_engineering>
combined=pd.concat([train_df[['posts']],test_df[['posts']]] )
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<data_type_conversions>
vectorizer = CountVectorizer(stop_words='english',analyzer = "word",tokenizer = None,preprocessor = None,max_features = 10000)
 combined=vectorizer.fit_transform(combined['posts'] )
df['Age'] = df.groupby(['Title'])['Age'].transform(lambda x: x.fillna(x.mean()))
test['Age'] = test.groupby(['Title'])['Age'].transform(lambda x: x.fillna(x.mean()))
df['Age'] = df['Age'].astype(int)
 test['Age'] = test['Age'].astype(int)
 df.loc[ df['Age'] <= 16, 'Age'] = 0
df.loc[(df['Age'] > 16)&(df['Age'] <= 32), 'Age'] = 1
df.loc[(df['Age'] > 32)&(df['Age'] <= 48), 'Age'] = 2
df.loc[(df['Age'] > 48)&(df['Age'] <= 64), 'Age'] = 3
df.loc[(df['Age'] > 64), 'Age'] = 4
test.loc[ test['Age'] <= 16, 'Age'] = 0
test.loc[(test['Age'] > 16)&(test['Age'] <= 32), 'Age'] = 1
test.loc[(test['Age'] > 32)&(test['Age'] <= 48), 'Age'] = 2
test.loc[(test['Age'] > 48)&(test['Age'] <= 64), 'Age'] = 3
test.loc[(test['Age'] > 64), 'Age'] = 4
df['Age'].head(5 )<data_type_conversions>
train = combined[:train_df.shape[0]]
test = combined[train_df.shape[0]:]
y=train_df['type']
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 test.head(5 )<drop_column>
y_mind = y.apply(lambda x: 0 if x[0] == 'I' else 1)
 y_energy = y.apply(lambda x: 0 if x[1] == 'S' else 1)
 y_nature = y.apply(lambda x: 0 if x[2] == 'F' else 1)
 y_tactics = y.apply(lambda x: 0 if x[3] == 'P' else 1 )
df=df.drop(['Name'],axis=1)
 test=test.drop(['Name'],axis=1)
 df.columns<drop_column>
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_validate
from sklearn.ensemble import GradientBoostingClassifier
df.drop('AgeGroup',axis=1,inplace=True )<categorify>
clf = GradientBoostingClassifier(n_estimators=200,learning_rate=0.15, random_state=0)
 kfolds = StratifiedKFold(n_splits=5, shuffle=True, random_state=1)
 scoring = {'acc': 'accuracy', 'f1': 'f1_micro'}
df = pd.concat([df.drop('Sex', axis=1), pd.get_dummies(df['Sex'])], axis=1)
 test = pd.concat([test.drop('Sex', axis=1), pd.get_dummies(test['Sex'])], axis=1)
 test.head(5 )<drop_column>
result = cross_validate(clf,train, y_mind, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
df.drop('Class',axis=1,inplace=True)
 df.head()<categorify>
print('Y_mind model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
df['Embarked'].replace({'S':1,'C':2,'Q':3},inplace=True)
 df['Embarked']=df['Embarked'].fillna(1)
 test['Embarked'].replace({'S':1,'C':2,'Q':3},inplace=True)
 test['Embarked']=test['Embarked'].fillna(1)
 test.head(5 )<drop_column>
result = cross_validate(clf,train, y_energy, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
df=df.drop(['Title'],axis=1)
 test=test.drop(['Title'],axis=1)
 test.columns<split>
print('Y_energy model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
predictors=df.drop(['Survived','PassengerId'],axis=1)
 target=df['Survived']
x_train,x_cv,y_train,y_cv=train_test_split(predictors,target,test_size=0.35,random_state=0 )<compute_train_metric>
result = cross_validate(clf,train, y_nature, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
 y_pred = knn.predict(x_cv)
 acc_knn = round(accuracy_score(y_pred,y_cv)* 100, 2)
 print(acc_knn )<compute_train_metric>
print('Y_nature model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_cv)
 acc_logreg = round(accuracy_score(y_pred, y_cv)* 100, 2)
 print(acc_logreg )<train_model>
result = cross_validate(clf,train, y_tactics, scoring=scoring,
cv=kfolds, n_jobs=-1, verbose=1 )
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_cv)
 acc_randomforest = round(accuracy_score(y_pred, y_cv)* 100, 2)
 print(acc_randomforest )<compute_train_metric>
print('Y_tactics model performance:')
 pprint(result)
 for key in result:
print(key + ' : ', result[key].mean() )
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_cv)
 acc_svc = round(accuracy_score(y_pred, y_cv)* 100, 2)
 print(acc_svc )<create_dataframe>
clf.fit(train,y_mind)
 X_t = test
predictions_Mind = pd.DataFrame(clf.predict(X_t))
models = pd.DataFrame({
'Method': ['KNN', 'Logistic Regression',
'Random Forest', 'Support Vector Machine'],
'Score': [acc_knn, acc_logreg,
acc_randomforest, acc_svc]})
 models.sort_values(by='Score', ascending=False )<compute_train_metric>
clf.fit(train,y_energy)
 predictions_Energy = pd.DataFrame(clf.predict(X_t))
svc = MLPClassifier()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_cv)
 acc_svc = round(accuracy_score(y_pred, y_cv)* 100, 2)
 print(acc_svc)
<compute_train_metric>
clf.fit(train,y_tactics)
 predictions_Tactic = pd.DataFrame(clf.predict(X_t))
svc = NearestCentroid()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_cv)
 acc_svc = round(accuracy_score(y_pred, y_cv)* 100, 2)
 print(acc_svc)
<predict_on_test>
clf.fit(train,y_nature)
 predictions_Nature = pd.DataFrame(clf.predict(X_t))
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(test.drop('PassengerId',axis=1))
print(y_pred )<prepare_output>
submission = pd.concat([predictions_Mind,predictions_Energy,predictions_Nature,predictions_Tactic], axis=1 )
submission = pd.DataFrame({'PassengerId':test['PassengerId'],'Survived':y_pred})
 submission.head(5 )<save_to_csv>
submission['index'] = submission['index'] +1
submission.columns = ['id', 'mind', 'energy', 'nature', 'tactics']
submission.head()
<set_options><EOS> 
submission.to_csv('submission_Gradient.csv', index=False )