Kaggle competition: Personality Profile Prediction
<SOS> metric: meancolumnwiselogloss Kaggle data source: personality-profile-prediction<categorify>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import re
import string
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
df_train = pd.get_dummies(df_train, columns = ["Initial"], prefix = "Initial")
 df_test = pd.get_dummies(df_test, columns = ["Initial"], prefix = "Initial" )<categorify>
warnings.filterwarnings("ignore" )
df_train = pd.get_dummies(df_train, columns = ["Embarked"], prefix = "Embarked")
 df_test = pd.get_dummies(df_test, columns = ["Embarked"], prefix = "Embarked" )<drop_column>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
df_train.drop(["PassengerId", "Name", "SibSp", "Parch", "Ticket", "Cabin"], axis = 1, inplace = True)
 df_test.drop(["PassengerId", "Name", "SibSp", "Parch", "Ticket", "Cabin"], axis = 1, inplace = True )<choose_model_class>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
kfold = StratifiedKFold(n_splits=10 )<prepare_x_and_y>
combined_data = pd.concat([train[['posts']], test[['posts']]] )
df_train["Survived"] = df_train["Survived"].astype(int)
 Y_train = df_train["Survived"]
X_train = df_train.drop(labels = ["Survived"],axis = 1 )<choose_model_class>
def clean_text(text):

 text = re.sub('https?\S+', 'URL', text)
 text = re.sub('[0-9]+','', text)
 text = re.sub('@[a-z0-9]+', 'user', text)
 text = re.sub('\|\|\|', ' ', text)
 text = re.sub('[%s]*' % string.punctuation, '',text)
 text = text.lower()
tokenized_text = word_tokenize(text)
 lemmatizer = WordNetLemmatizer()
text = ' '.join([lemmatizer.lemmatize(a)for a in tokenized_text])
 return text
random_state = 2
classifiers = []
classifiers.append(SVC(random_state = random_state))
classifiers.append(DecisionTreeClassifier(random_state = random_state))
classifiers.append(AdaBoostClassifier(DecisionTreeClassifier(random_state = random_state), random_state = random_state, learning_rate = 0.1))
classifiers.append(RandomForestClassifier(random_state = random_state))
classifiers.append(ExtraTreesClassifier(random_state = random_state))
classifiers.append(GradientBoostingClassifier(random_state = random_state))
classifiers.append(MLPClassifier(random_state = random_state))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression(random_state = random_state))
classifiers.append(LinearDiscriminantAnalysis())
 cv_results = []
for classifier in classifiers:
cv_results.append(cross_val_score(classifier, X_train, y = Y_train, scoring = "accuracy", cv = kfold, n_jobs = 4))
cv_means = []
cv_std = []
for cv_result in cv_results:
cv_means.append(cv_result.mean())
 cv_std.append(cv_result.std())
 cv_res = pd.DataFrame({"CrossValMeans": cv_means, "CrossValerrors": cv_std,
"Algorithm": ["SVC", "DecisionTree", "AdaBoost", "RandomForest",
"ExtraTrees", "GradientBoosting", "MultipleLayerPerceptron", "KNeighboors",
"LogisticRegression", "LinearDiscriminantAnalysis"]})
 g = sns.barplot("CrossValMeans", "Algorithm", data = cv_res, palette = "Set3",
orient = "h", **{'xerr': cv_std})
 g.set_xlabel("Mean Accuracy")
 g = g.set_title("Cross validation scores" )<choose_model_class>
combined_data['clean_posts'] = combined_data['posts'].apply(clean_text )
DTC = DecisionTreeClassifier()
adaDTC = AdaBoostClassifier(DTC, random_state = 7)
 ada_param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter": ["best", "random"],
"algorithm": ["SAMME", "SAMME.R"],
"n_estimators": [1,2],
"learning_rate": [0.0001,0.001, 0.01, 0.1, 0.2, 0.3, 1.5]}
gsadaDTC = GridSearchCV(adaDTC,param_grid = ada_param_grid, cv = kfold, scoring = "accuracy",
n_jobs = 4, verbose = 1)
 gsadaDTC.fit(X_train, Y_train)
 ada_best = gsadaDTC.best_estimator_
gsadaDTC.best_score_<define_search_model>
vectorizer = TfidfVectorizer()
combined_X = vectorizer.fit_transform(combined_data['clean_posts'] )
ExtC = ExtraTreesClassifier()
ex_param_grid = {"max_depth": [None],
"max_features": [1,2,10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1,3,10],
"bootstrap": [False],
"n_estimators": [100, 300],
"criterion": ["gini"]}
gsExtC = GridSearchCV(ExtC, param_grid = ex_param_grid, cv = kfold, scoring = "accuracy",
n_jobs = 4, verbose = 1)
 gsExtC.fit(X_train, Y_train)
 ExtC_best = gsExtC.best_estimator_
gsExtC.best_score_<choose_model_class>
svd = TruncatedSVD(n_components = 100)
 combined_X = svd.fit_transform(combined_X )
RFC = RandomForestClassifier()
rf_param_grid = {"max_depth": [None],
"max_features": [1,3,10],
"min_samples_split": [2,3,10],
"min_samples_leaf": [1,2,10],
"bootstrap": [False],
"n_estimators": [100,300],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC, param_grid = rf_param_grid, cv=kfold, scoring = "accuracy", n_jobs = 4,
verbose = 1)
 gsRFC.fit(X_train,Y_train)
 RFC_best = gsRFC.best_estimator_
gsRFC.best_score_ 
<choose_model_class>
X = combined_X[:6506,:]
test = combined_X[6506:,:]
GBC = GradientBoostingClassifier()
gb_param_grid = {"loss": ["deviance"],
"n_estimators": [100,200,300],
"learning_rate": [0.1, 0.05, 0.01],
"max_depth": [4, 8],
"min_samples_leaf": [100,150],
"max_features": [0.3, 0.1]}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv = kfold, scoring = "accuracy",
n_jobs = 4, verbose = 1)
 gsGBC.fit(X_train, Y_train)
 GBC_best = gsGBC.best_estimator_
gsGBC.best_score_<train_on_grid>
y = train['type']
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 )
SVMC = SVC(probability=True)
 svc_param_grid = {'kernel': ['rbf'],
'gamma': [ 0.001, 0.01, 0.1, 1],
'C': [1, 10, 50, 100,200,300, 1000]}
gsSVMC = GridSearchCV(SVMC,param_grid = svc_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsSVMC.fit(X_train,Y_train)
 SVMC_best = gsSVMC.best_estimator_
gsSVMC.best_score_<train_model>
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from mlxtend.classifier import StackingCVClassifier
from sklearn.model_selection import cross_val_score, GridSearchCV
votingC = VotingClassifier(estimators = [("rfc", RFC_best),("extc", ExtC_best), 
("svc", SVMC_best),("adac", ada_best), 
("gbc", GBC_best)], voting = "soft", n_jobs = 4)
 votingC = votingC.fit(X_train, Y_train )<load_from_csv>
LogR_model = LogisticRegression()
RFC_model = RandomForestClassifier()
SVC_model = SVC(probability = True)
 XGB_model = XGBClassifier()
category = [(y_mind, "Mind"),(y_energy, "Energy"),(y_nature, "Nature"),(y_tactics, "Tactics")]
models = [(LogR_model, "LOG"),(RFC_model, "RFC"),(SVC_model, 'SVC'),(XGB_model, 'XGB')]
submission = pd.read_csv(".. /input/gender_submission.csv" )<prepare_x_and_y>
LogR_model = LogisticRegression(C = 10 )
df_test["Fare"].fillna("35.6271", inplace = True)
 X_test = df_test.values<predict_on_test>
SVC_model = SVC(gamma = 5, probability = True )
prediction = votingC.predict(X_test )<prepare_output>
XGB_model = XGBClassifier(n_estimators=150, learning_rate=0.15,gamma= 0.5 )
submission["Survived"] = prediction<save_to_csv>
Meta_model = XGBClassifier()
Stack_model = StackingCVClassifier(classifiers = [SVC_model, XGB_model, LogR_model],
meta_classifier = Meta_model )
<load_from_csv><EOS> 
models = [(LogR_model, "log_submission.csv"),(SVC_model, 'svc_submission.csv'),(XGB_model, 'xgb_submission.csv')]
for model, file_name in models:
submission_file(model, file_name )