Kaggle competition: Personality Profile Prediction
<SOS> metric: meancolumnwiselogloss Kaggle data source: personality-profile-prediction<compute_test_metric>
%matplotlib inline
class color:
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
warnings.filterwarnings("ignore" )
cs2 = Catmodel.score(x_test,y_test)
 print(cs2 )<train_model>
df_train = pd.read_csv('.. /input/train.csv')
 df_train.head()
lightgbm.fit(x_train,y_train)
 lightgbm1.fit(x_train,y_train)
<compute_test_metric>
test_df = pd.read_csv('.. /input/test.csv')
 test_df.head()
ls1 = lightgbm.score(x_train,y_train)
 print(ls1 )<compute_test_metric>
print('Training set consist of', round(len(df_train)/8675*100),'% data')
 print('Testing set consist of', round(len(test_df)/8675*100),'% data' )
lg1 = lightgbm1.score(x_train,y_train)
 print(lg1 )<compute_test_metric>
len(df_train.iloc[1,1].split('|||'))
ls2 = lightgbm.score(x_test,y_test)
 print(ls2 )<compute_test_metric>
train_val_count=df_train['type'].value_counts()
train_val_count
lg2 = lightgbm1.score(x_test,y_test)
 print(lg2 )<train_model>
I_E= df_train['type'].map(lambda type: type[0] ).value_counts()
N_S= df_train['type'].map(lambda type: type[1] ).value_counts()
T_F= df_train['type'].map(lambda type: type[2] ).value_counts()
J_P= df_train['type'].map(lambda type: type[3] ).value_counts()
Catmodel.fit(X_train,Y_train )<train_model>
print(color.BOLD +"Introverts and Extroverts Percentages:" + color.END)
 Introversion_perc= 4998/len(df_train)
 print('Introversion_percentage is:',round(Introversion_perc *100),'%')
 Extroversion_perc= 1508/len(df_train)
 print('Extroversion_percentage is:',round(Extroversion_perc *100),'%','
')
 print(color.BOLD +"Intuition and Sensing Percentages:" + color.END)
 Intuition_perc= 5612/len(df_train)
 print('Intuition_percentage is:',round(Intuition_perc *100),'%')
 Sensing_perc= 894/len(df_train)
 print('Sensing_percentage is:',round(Sensing_perc *100),'%','
')
 print(color.BOLD +"Thinking and Feeling Percentages:" + color.END)
 Thinking_perc= 3518/len(df_train)
 print('Thinking_percentage is:',round(Thinking_perc *100),'%')
 Feeling_perc= 2988/len(df_train)
 print('Feeling_percentage is:',round(Feeling_perc *100),'%','
')
 print(color.BOLD +"Judging and Perceiving Percentages:" + color.END)
 Judging_perc= 3932/len(df_train)
 print('Thinking_percentage is:',round(Judging_perc *100),'%')
 Perceiving_perc= 2574/len(df_train)
 print('Perceiving_percentage is:',round(Perceiving_perc *100),'%' )
lightgbm.fit(X_train,Y_train)
 lightgbm1.fit(X_train,Y_train)
<predict_on_test>
temp = {'Introverts':[77],
'Extroverts':[23],
'Intuition':[86],
'Sensing':[14],
'Thinking':[54],
'Feeling':[46],
'Judging':[60],
'Perceiving':[40]}
results = pd.DataFrame.from_dict(temp, orient='index', columns=['Percentages'])
 results
prediction_lightgbm = lightgbm.predict(test)
 prediction_lightgbm1 = lightgbm1.predict(test)
 print('Ran' )<predict_on_test>
my_func= lambda x: float(x)
 results['Percentages']=results['Percentages'].apply(my_func )
prediction_catbooster = Catmodel.predict(test )<save_to_csv>
p = df_train.copy()
z = test_df.copy()
submit=pd.DataFrame()
submit['Id']=test['Id']
submit['SalePrice']=prediction_lightgbm1
submit.to_csv('submission.csv',index=False)
 print('Ran' )<load_from_csv>
p['clean'] = p['posts'].apply(lambda x: ' '.join(x.split('|||')))
 p.head()
input_io_dir=".. /input/titanic/"
original_train_data=pd.read_csv(input_io_dir+"train.csv")
 original_test_data=pd.read_csv(input_io_dir+"test.csv")
 print('original_train_data',original_train_data.shape)
 print('original_test_data',original_test_data.shape )<load_from_csv>
z['clean'] = z['posts'].apply(lambda x: ' '.join(x.split('|||')))
 z.head()
input_io_dir='.. /input/titanic-competition-feature-engineering-1/'
def PrepareDataSets() :
passengerId=pd.read_csv(input_io_dir+"passengerId.csv",header=None)
 train_features=pd.read_csv(input_io_dir+"train_features.csv",header=0)
 train_labels=pd.read_csv(input_io_dir+"train_labels.csv",header=None)
 test_features=pd.read_csv(input_io_dir+"test_features.csv",header=0)
 print('PrepareDataSets: passengerId loaded(%d)'% len(passengerId))
print('PrepareDataSets: train_features loaded(%d)'% len(train_features))
print('PrepareDataSets: train_labels loaded(%d)'% len(train_labels))
print('PrepareDataSets: test_features loaded(%d)'% len(test_features))
return passengerId,train_features,train_labels, test_features
def ModelSelection(clf_list,name_list,train_features,train_labels,scoring='accuracy'):
best_score=0
for clf, name in zip(clf_list,name_list):
scores = model_selection.cross_val_score(clf, train_features.values.astype(float), train_labels.values.ravel().astype(float), cv=10, scoring=scoring)
 print("ModelSelection: Scoring %0.2f +/- %0.2f(%s 95%% CI)" %(scores.mean() , scores.std() *2, name))
reference_score=scores.mean() +scores.std()
if(reference_score>best_score):
best_clf=name
best_score=reference_score
learning_model=clf
print("ModelSelection: Best model - "+best_clf)
 return learning_model
def ConfigureLearningModelsForBinaryClassification() :
xgb_clf = XGBClassifier(n_estimators=100,max_depth=40, random_state=42)
 dt_clf = DecisionTreeClassifier(random_state=42)
 rf_clf = ensemble.RandomForestClassifier(n_estimators=100, random_state=42)
 et_clf = ensemble.ExtraTreesClassifier(n_estimators=100, random_state=42)
 gb_clf = ensemble.GradientBoostingClassifier(n_estimators=100, random_state=42)
 ada_clf = ensemble.AdaBoostClassifier(n_estimators=100, random_state=42)
 svm_clf = svm.LinearSVC(C=0.1,random_state=42)
 lg_clf = LogisticRegression(solver='lbfgs', multi_class='auto', max_iter=400,random_state=42)
 e_clf = ensemble.VotingClassifier(estimators=[('xgb', xgb_clf),('dt', dt_clf),('rf',rf_clf),('et',et_clf),('gbc',gb_clf),('ada',ada_clf),('svm',svm_clf),('lg',lg_clf)])
 clf_list = [xgb_clf, dt_clf, rf_clf, et_clf, gb_clf, ada_clf, svm_clf,lg_clf,e_clf]
name_list = ['XGBoost', 'Decision Trees','Random Forest', 'Extra Trees', 'Gradient Boosted', 'AdaBoost', 'Support Vector Machine', 'LogisticRegression','Ensemble']
return clf_list,name_list
passengerId,train_features,train_labels, test_features=PrepareDataSets()
clf_list,name_list=ConfigureLearningModelsForBinaryClassification()
learning_model=ModelSelection(clf_list,name_list,train_features,train_labels)
<train_on_grid>
pattern_url = r'http[s]?://(?:[A-Za-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9A-Fa-f][0-9A-Fa-f])) +'
subs_url = r'url-web'
p['clean'] = p['clean'].replace(to_replace = pattern_url, value = subs_url, regex = True)
 p.head()
warnings.filterwarnings("ignore", category=DeprecationWarning)
 def FineTuneLearningModel(learning_model, param_grid, train_features,train_labels,scoring='accuracy'):
grid_search = GridSearchCV(learning_model, param_grid, scoring,cv=10)
 grid_search.fit(train_features.values.astype(float),train_labels.values.ravel().astype(float))
cvres = grid_search.cv_results_
for mean_score,std_score, params in zip(cvres["mean_test_score"], cvres["std_test_score"],cvres["params"]):
print('FineTuneLearningModel:',mean_score,'+-',std_score, params)
 print('FineTuneLearningModel: Best params - '+str(grid_search.best_params_))
return grid_search.best_estimator_
passengerId,train_features,train_labels, test_features=PrepareDataSets()
learning_model = XGBClassifier(objective='binary:logistic')
 print('FineTuneLearningModel: round 1 - booster type')
 print('---------------------------------------------')
 param_grid = [
{'booster':['gbtree','gblinear'],'n_estimators': [10,30,100],'learning_rate':[0.1]}
]
learning_model=FineTuneLearningModel(learning_model, param_grid,train_features,train_labels)
 print('Current model params:'+str(learning_model))
print('FineTuneLearningModel: round 2 - complexity params')
 print('--------------------------------------------------')
 param_grid = [
{'max_depth':range(3,10,2),'min_child_weight':range(1,6,2),'gamma':[i/10.0 for i in range(0,5)]}
]
learning_model=FineTuneLearningModel(learning_model, param_grid,train_features,train_labels)
 print('Current model params:'+str(learning_model))
print('FineTuneLearningModel: round 3 - robustness params')
 print('--------------------------------------------------')
 param_grid = [
{ 'subsample':[1e-5,1e-2,0.1,0.2,0.5,0.8,1], 'colsample_bytree':[1e-5,1e-2,0.1,0.2,0.5,0.8,1]}
]
learning_model=FineTuneLearningModel(learning_model, param_grid,train_features,train_labels)
 print('Current model params:'+str(learning_model))
print('FineTuneLearningModel: round 4 - regularisation')
 print('-----------------------------------------------')
 param_grid = [
{ 'reg_alpha':[1e-5, 1e-2, 0.1, 1, 10,50],'reg_lambda':[0.1,0.5, 1, 2,5,10,50]}
]
learning_model=FineTuneLearningModel(learning_model, param_grid,train_features,train_labels)
 print('Current model params:'+str(learning_model))
print('FineTuneLearningModel: round 5 - reduce learning rate as it prevents overfitting')
 print('--------------------------------------------------------------------------------')
 param_grid = [
{ 'learning_rate': [1e-5,0.01],'n_estimators': [10,100,200,500,1000]}
]
learning_model=FineTuneLearningModel(learning_model, param_grid,train_features,train_labels)
 print('Current model params:'+str(learning_model))<predict_on_test>
z['clean'] = z['clean'].replace(to_replace = pattern_url, value = subs_url, regex = True)
 z.tail()
def TrainModelAndGeneratePredictionsOnTestSet(learning_model,train_features,train_labels,test_features, threshold=-1):
learning_model.fit(train_features.values.astype(float),train_labels.values.ravel().astype(float))
if threshold==-1:
predictions = learning_model.predict(test_features.values.astype(float))
else:
if hasattr(learning_model,"decision_function"):
y_scores=learning_model.decision_function(test_features.values.astype(float))
else:
y_proba=learning_model.predict_proba(test_features.values.astype(float))
y_scores=y_proba[:,1]
predictions=(y_scores>threshold ).astype(float)
 pred=pd.Series(predictions)
 return pred.apply(lambda x: 1 if x>0 else 0)
 predictions=TrainModelAndGeneratePredictionsOnTestSet(learning_model,train_features,train_labels,test_features)
 print('TrainModelAndGeneratePredictionsOnTestSet: predictions ready' )<save_to_csv>
p['clean_2'] = p['clean'].str.lower()
p.head()
def GenerateOutputFile(passengerId,predictions):
output = pd.DataFrame({ 'PassengerId': passengerId,
'Survived': predictions })
 output.to_csv("output.csv", index=False)
 passengerId = original_test_data['PassengerId']
GenerateOutputFile(passengerId,predictions )<predict_on_test>
p['clean_2'] = p['clean_2'].apply(lambda x : x.translate(str.maketrans(' ',' ',string.punctuation)))
 p.head()
training_predictions = pd.DataFrame(learning_model.predict(train_features.values.astype(float)))
 training_predictions.iloc[:,0]=training_predictions.iloc[:,0].astype(int )<merge>
p['clean_2'] = p['clean_2'].apply(lambda x : x.translate(str.maketrans(' ',' ',digits)))
 p.head()
result=original_train_data.join(training_predictions!=train_labels)
<rename_columns>
p['clean_2'] = p['clean_2'].str.strip()
p.head()
result.rename(columns={0:'Error'},inplace=True )<set_options>
tokeniser = TreebankWordTokenizer()
p['tokens'] = p['clean_2'].apply(tokeniser.tokenize)
 p.head()
warnings.filterwarnings("ignore")
<load_from_csv>
stemmer = SnowballStemmer('english')
 def mbti_stemmer(words, stemmer):
return [stemmer.stem(word)for word in words]
dtrain=pd.read_csv('.. /input/train.csv')
 dtest=pd.read_csv('.. /input/test.csv')
 print(dtrain.shape)
 print(dtest.shape )<drop_column>
p['stem'] = p['tokens'].apply(mbti_stemmer, args=(stemmer,))
p.head()
test_ID=dtest['Id']
dtrain=dtrain.drop(['Id'],axis=1)
 dtest=dtest.drop(['Id'],axis=1 )<prepare_x_and_y>
Stops = set(stopwords.words('english'))
y_train=dtrain['SalePrice']
data=pd.concat([dtrain,dtest],ignore_index=True)
 data=data.drop(['SalePrice'],axis=1)
 data.shape<sort_values>
p['no_stop'] = p['stem'].apply(lambda x: [word for word in list(x)if word not in Stops])
 p.head()
total = data.isnull().sum().sort_values(ascending=False)
 percent =(( data.isnull().sum() /data.isnull().count() ).sort_values(ascending=False)) *100
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20)
<data_type_conversions>
p['no_stop'] = p['no_stop'].apply(lambda x: ' '.join(x))
p.head()
data['PoolQC']=data['PoolQC'].fillna("None")
 data['MiscFeature']=data['MiscFeature'].fillna("None")
 data['Alley']=data['Alley'].fillna("None")
 data['Fence']=data['Fence'].fillna("None")
 data['FireplaceQu']=data['FireplaceQu'].fillna("None")
 data["LotFrontage"] = data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
unique_type = list(p['type'].unique())
 encoder = LabelEncoder().fit(unique_type)
 codes = []
for i in range(0, len(p)) :
codes.append(p['type'][i])
 coder = encoder.transform(codes )
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
data[col] = data[col].fillna('None' )<feature_engineering>
p['codes'] = coder
p.head()
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
data[col] = data[col].fillna(0 )<categorify>
X = p.clean
y = p.codes
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
data[col] = data[col].fillna('None' )<data_type_conversions>
X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=14 )
data["MasVnrType"] = data["MasVnrType"].fillna("None")
 data["MasVnrArea"] = data["MasVnrArea"].fillna(0 )<drop_column>
Vect = CountVectorizer(ngram_range=(1, 1), stop_words='english', lowercase = True, max_features = 5000 )
data=data.drop(['GarageCars','1stFlrSF','2ndFlrSF','TotRmsAbvGrd','GarageYrBlt'],axis=1 )<feature_engineering>
LRmodel = LogisticRegression(class_weight="balanced", C=0.005, penalty = "l2")
 pipe = Pipeline([('vec', Vect),('model', LRmodel)] )
num_f=data.dtypes[data.dtypes!=object].index
skew_f=data[num_f].apply(lambda x: skew(x.dropna()))
skew_f=skew_f[skew_f>0.75]
skew_f=skew_f.index
data[skew_f]=np.log1p(data[skew_f] )<categorify>
pipe.fit(X_train, y_train )
data = pd.get_dummies(data)
 data.shape<prepare_x_and_y>
y_pred_train= pipe.predict(X_train )
x_train=data[:dtrain.shape[0]]
test=data[dtrain.shape[0]:]<import_modules>
print('Accuracy: '+ str(metrics.accuracy_score(y_train, y_pred_train)))
 print('Precision: '+ str(metrics.precision_score(y_train, y_pred_train, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_train, y_pred_train, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_train, y_pred_train, average='macro')) )
from sklearn.model_selection import cross_val_score
from sklearn.metrics import mean_squared_error
import xgboost as xgb<prepare_x_and_y>
y_pred_test = pipe.predict(X_test )
dtrain=xgb.DMatrix(x_train,label=y_train)
 dtest=xgb.DMatrix(test )<train_on_grid>
print('Accuracy: '+ str(metrics.accuracy_score(y_test, y_pred_test)))
 print('Precision: '+ str(metrics.precision_score(y_test, y_pred_test, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_test, y_pred_test, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_test, y_pred_test, average='macro')) )
def_params={'min_child_weight': 1,
'max_depth': 6,
'subsample': 1.0,
'colsample_bytree': 1.0,
'reg_lambda': 1,
'reg_alpha': 0,
'learning-rate':0.3,
'silent':1
}
cv_res=xgb.cv(def_params,dtrain,nfold=5)
 cv_res.tail()<choose_model_class>
cm_logistic_reg = np.array(metrics.confusion_matrix(y_test, y_pred_test))
cm_logistic = pd.DataFrame(cm_logistic_reg, index=['ENFJ', 'ENFP', 'ENTJ', 'ENTP', 'ESFJ', 'ESFP',
'ESTJ', 'ESTP', 'INFJ', 'INFP', 'INTJ', 'INTP',
'ISFJ', 'ISFP', 'ISTJ', 'ISTP'],
columns=['predict_ENFJ','predict_ENFP','predict_ENTJ',
'predict_ENTP','predict_ESFJ','predict_ESFP',
'predict_ESTJ','predict_ESTP','predict_INFJ',
'predict_INFP','predict_INTJ','predict_INTP',
'predict_ISFJ','predict_ISFP','predict_ISTJ',
'predict_ISTP'])
 cm_logistic
warnings.filterwarnings('ignore', message='The objective has been evaluated at this point before.')
 params={'min_child_weight':(0, 50,),
'max_depth':(0, 10),
'subsample':(0.5, 1.0),
'colsample_bytree':(0.5, 1.0),
'reg_lambda':(1e-5,100,'log-uniform'),
'reg_alpha':(1e-5,100,'log-uniform'),
'learning-rate':(0.01,0.2,'log-uniform')
 }
bayes=BayesSearchCV(xgb.XGBRegressor() ,params,n_iter=10,scoring='neg_mean_squared_error',cv=5,random_state=42)
 res=bayes.fit(x_train,y_train)
 print(res.best_params_ )<train_on_grid>
print(metrics.classification_report(y_test, y_pred_test, target_names=unique_type))
final_params={'colsample_bytree': 0.50, 'max_depth': 7, 'min_child_weight':13, 'reg_alpha': 0.112, 'reg_lambda': 0.0008, 'subsample': 0.65,'eta':0.11,'silent':1}
cv_res=xgb.cv(final_params,dtrain,num_boost_round=1000,early_stopping_rounds=100,nfold=5 )<train_model>
test_preds = pipe.predict(z['clean'] )
model_xgb=xgb.train(final_params,dtrain=dtrain,num_boost_round=151)
 final_pred=np.expm1(model_xgb.predict(dtest))<save_to_csv>
z['type'] = encoder.inverse_transform(test_preds)
 z.head()
s=pd.DataFrame()
s['Id']=test_ID
s['SalePrice']=final_pred
s.to_csv('my_submission.csv',index=False )<import_modules>
z['E or I'] = z.apply(lambda x: x['type'][0], axis = 1)
 z['N or S'] = z.apply(lambda x: x['type'][1], axis = 1)
 z['T or F'] = z.apply(lambda x: x['type'][2], axis = 1)
 z['J or P'] = z.apply(lambda x: x['type'][3], axis = 1)
 mind = z['E or I'].astype(str ).apply(lambda x: x[0] == 'E' ).astype('int')
 energy = z['N or S'].astype(str ).apply(lambda x: x[0] == 'N' ).astype('int')
 nature = z['T or F'].astype(str ).apply(lambda x: x[0] == 'T' ).astype('int')
 tactics = z['J or P'].astype(str ).apply(lambda x: x[0] == 'J' ).astype('int')
 z.head()
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.python.framework import ops
import matplotlib.pyplot as plt<load_from_csv>
df_LogReg = pd.DataFrame({"id":test_df['id'], "mind":mind, "energy":energy, "nature":nature, 'tactics':tactics})
 df_LogReg.head()
def read_data(file_name):

 data = pd.read_csv('.. /input/'+file_name+'.csv')
 return data<feature_engineering>
df_LogReg.to_csv('EDSA_Team_8_Classification1.csv', index = False )
def prepare_age(data):

 age = data['Age']
mean_age = age.mean()
var_age = age.var()
age[age.isnull() ] = mean_age
age = age - mean_age
age = age / var_age
return age.as_matrix()<categorify>
LRmodel2 = LogisticRegression(class_weight="balanced", C=0.004, penalty = "l2")
 pipe2 = Pipeline([('vec', Vect),('model', LRmodel2)] )
def prepare_fare(data):

 fare = data['Fare']
mean_fare = fare.mean()
var_fare = fare.var()
fare = fare - mean_fare
fare = fare / var_fare
return fare.as_matrix()<categorify>
pipe2.fit(X_train, y_train )
def prepare_sex(data):

 sex = data['Sex']
sex = np.where(sex=='male',0,1)
 return sex<feature_engineering>
y_pred_train2= pipe2.predict(X_train )
def prepare_embarquation(data):

 embarked = data['Embarked']
embarked[embarked.isnull() ] = 3
embarked = np.where(embarked=='C', 0, embarked)
 embarked = np.where(embarked=='Q', 1, embarked)
 embarked = np.where(embarked=='S', 2, embarked)
 mean_embarked = embarked.mean()
var_embarked = embarked.var()
embarked = embarked - mean_embarked
embarked = embarked / var_embarked
return embarked<feature_engineering>
print('Accuracy: '+ str(metrics.accuracy_score(y_train, y_pred_train2)))
 print('Precision: '+ str(metrics.precision_score(y_train, y_pred_train2, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_train, y_pred_train2, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_train, y_pred_train2, average='macro')) )
def prepare_sibligs(data):

 sib = data['SibSp']
mean_sib = sib.mean()
var_sib = sib.var()
sib = sib - mean_sib
sib = sib / var_sib
return sib<categorify>
y_pred_test2 = pipe2.predict(X_test )
def prepare_parch(data):

 parch = data['Parch']
mean_parch = parch.mean()
var_parch = parch.var()
parch = parch - mean_parch
parch = parch / var_parch
return parch<categorify>
print('Accuracy: '+ str(metrics.accuracy_score(y_test, y_pred_test2)))
 print('Precision: '+ str(metrics.precision_score(y_test, y_pred_test2, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_test, y_pred_test2, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_test, y_pred_test2, average='macro')) )
def prepare_family_size(data):

 parch = data['Parch']
sib = data['SibSp']
family_size = parch + sib
mean_family_size = family_size.mean()
var_family_size = family_size.var()
family_size = family_size - mean_family_size
family_size = family_size / var_family_size
return family_size 
<categorify>
con_matrix_test2= metrics.confusion_matrix(y_test, y_pred_test2)
 cm_logistic_df = pd.DataFrame(con_matrix_test2, index=['ENFJ', 'ENFP', 'ENTJ', 'ENTP', 'ESFJ', 'ESFP',
'ESTJ', 'ESTP', 'INFJ', 'INFP', 'INTJ', 'INTP',
'ISFJ', 'ISFP', 'ISTJ', 'ISTP'],
columns=['predict_ENFJ','predict_ENFP','predict_ENTJ',
'predict_ENTP','predict_ESFJ','predict_ESFP',
'predict_ESTJ','predict_ESTP','predict_INFJ',
'predict_INFP','predict_INTJ','predict_INTP',
'predict_ISFJ','predict_ISFP','predict_ISTJ',
'predict_ISTP'])
def normalize_features(data):

 age = prepare_age(data)
 sex = prepare_sex(data)
 embark = prepare_embarquation(data)
 fare = prepare_fare(data)
 sib = prepare_sibligs(data)
 parch = prepare_parch(data)
 family_size = prepare_family_size(data)
 X_train = np.column_stack(( sex, age, family_size, embark))
return X_train.T 
<prepare_x_and_y>
print(metrics.classification_report(y_test, y_pred_test2, target_names=unique_type))
def prepare_training_data() :

 pd.set_option('mode.chained_assignment', None)
 data = read_data('train')
 X_train = normalize_features(data)
 Y_train = np.reshape(data['Survived'].as_matrix() ,(X_train.shape[1],1)).T
return X_train, Y_train<feature_engineering>
test_preds2 = pipe2.predict(z['clean'] )
def prepare_test_data() :

 pd.set_option('mode.chained_assignment', None)
 data = read_data('test')
 X_test = normalize_features(data)
 return X_test<init_hyperparams>
z['type'] = encoder.inverse_transform(test_preds2)
 z.head()
def initialize_Parameters(nb_features):

 W1 = tf.get_variable("W1", [5, nb_features], initializer = tf.contrib.layers.xavier_initializer())
 b1 = tf.get_variable("b1", [5,1], initializer = tf.zeros_initializer())
 W2 = tf.get_variable("W2", [8,5], initializer = tf.contrib.layers.xavier_initializer())
 b2 = tf.get_variable("b2", [8,1], initializer = tf.zeros_initializer())
 W3 = tf.get_variable("W3", [6,8], initializer = tf.contrib.layers.xavier_initializer())
 b3 = tf.get_variable("b3", [6,1], initializer = tf.zeros_initializer())
 W4 = tf.get_variable("W4", [4,6], initializer = tf.contrib.layers.xavier_initializer())
 b4 = tf.get_variable("b4", [4,1], initializer = tf.zeros_initializer())
 W5 = tf.get_variable("W5", [1,4], initializer = tf.contrib.layers.xavier_initializer())
 b5 = tf.get_variable("b5", [1,1], initializer = tf.zeros_initializer())
 parameters = {"W1": W1, "b1": b1,
"W2": W2, "b2": b2,
"W3": W3, "b3": b3,
"W4": W4, "b4": b4,
"W5": W5, "b5": b5
}
return parameters 
<normalization>
z['E or I'] = z.apply(lambda x: x['type'][0], axis = 1)
 z['N or S'] = z.apply(lambda x: x['type'][1], axis = 1)
 z['T or F'] = z.apply(lambda x: x['type'][2], axis = 1)
 z['J or P'] = z.apply(lambda x: x['type'][3], axis = 1)
 mind = z['E or I'].astype(str ).apply(lambda x: x[0] == 'E' ).astype('int')
 energy = z['N or S'].astype(str ).apply(lambda x: x[0] == 'N' ).astype('int')
 nature = z['T or F'].astype(str ).apply(lambda x: x[0] == 'T' ).astype('int')
 tactics = z['J or P'].astype(str ).apply(lambda x: x[0] == 'J' ).astype('int')
 z.head()
def forward_propagation(X, parameters):

 W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
W4 = parameters['W4']
b4 = parameters['b4']
W5 = parameters['W5']
b5 = parameters['b5']
Z1 = tf.add(tf.matmul(W1, X), b1)
 A1 = tf.nn.relu(Z1)
 Z2 = tf.add(tf.matmul(W2, A1), b2)
 A2 = tf.nn.relu(Z2)
 Z3 = tf.add(tf.matmul(W3, A2), b3)
 A3 = tf.nn.relu(Z3)
 Z4 = tf.add(tf.matmul(W4, A3), b4)
 A4 = tf.nn.relu(Z4)
 Z5 = tf.add(tf.matmul(W5, A4), b5)
 return Z5<categorify>
df_LogReg2 = pd.DataFrame({"id":test_df['id'], "mind":mind, "energy":energy, "nature":nature, 'tactics':tactics})
 df_LogReg2.head()
def create_placeholders(n_x, n_y):

 X = tf.placeholder(dtype=tf.float32, shape=([n_x, None]), name="X")
 Y = tf.placeholder(dtype=tf.float32, shape=([n_y, None]), name="Y")
 return X, Y<compute_train_metric>
df_LogReg2.to_csv('EDSA_Team_8_Classification2.csv', index = False )
def compute_cost(Z, Y):

 logits = tf.transpose(Z)
 labels = tf.transpose(Y)
 cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
return cost<init_hyperparams>
random= RandomForestClassifier(min_samples_leaf=2, min_samples_split=3, n_estimators=79,
criterion='entropy', bootstrap='False', n_jobs= -1, random_state=123)
 random_pipe= Pipeline([('vec', Vect),('model', random)] )
def train_predict_model(learning_rate, epoch, X_train, Y_train, X_test):

 X, Y = create_placeholders(X_train.shape[0], Y_train.shape[0])
 parameters = initialize_Parameters(X_train.shape[0])
 Z4 = forward_propagation(X, parameters)
 cost = compute_cost(Z4, Y)
 optimizer = tf.train.AdamOptimizer(learning_rate ).minimize(cost)
 costs = []
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
 for i in range(1,epoch):
_ , calculated_cost = sess.run([optimizer, cost], feed_dict={X: X_train, Y: Y_train})
 if i % 50 == 0:
costs.append(calculated_cost)
 plot_cost(costs, learning_rate)
 parameters = sess.run(parameters)
 y_pred_proba = tf.cast(tf.nn.sigmoid(Z4),dtype = tf.float32);
y_pred_class = tf.cast(tf.greater(y_pred_proba, 0.5),'float')
 accuracy = tf.reduce_mean(tf.cast(tf.equal(y_pred_class, Y), 'float'))
prediction=tf.argmax(tf.nn.sigmoid(Z4))
sess.run([prediction], feed_dict={X: X_train, Y: Y_train})
 print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
prediction_test = sess.run(y_pred_class, feed_dict={X:X_test})
 return prediction_test 
<predict_on_test>
random_pipe.fit(X_train, y_train )
ops.reset_default_graph()
X_train, Y_train = prepare_training_data()
X_test = prepare_test_data()
prediction_test = train_predict_model(learning_rate=0.0001, epoch=40000, X_train= X_train
, Y_train = Y_train, X_test = X_test)
 prediction_test = np.where(prediction_test < 1, 0, 1)
 data = read_data('test')
 submission = pd.DataFrame({
"PassengerId": data["PassengerId"],
"Survived": prediction_test.reshape(X_test.shape[1])
 })
 submission.to_csv('submission.csv', index=False)
 print(check_output(["ls", "."] ).decode("utf8")) 
<load_from_csv>
y_pred_random=random_pipe.predict(X_train )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train["n"] = 0
test["n"] = 1
global tot
tot = pd.concat([train,test],sort = False )<feature_engineering>
print('Accuracy: '+ str(metrics.accuracy_score(y_train, y_pred_random)))
 print('Precision: '+ str(metrics.precision_score(y_train, y_pred_random, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_train, y_pred_random, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_train, y_pred_random, average='macro')) )
def check_survive(label):
global tot
return tot[["Survived",label]].groupby(label ).Survived.mean()
def process_Sex() :
global tot
tot.Sex = tot.Sex.replace({"male": 0, "female": 1})
 def process_Name() :
global tot
tot["Title"] = tot.Name.str.extract("([A-Za-z]+)\.")
 tot["Title"].replace(['Lady', 'Countess','Sir', 'Jonkheer', 'Dona'], 'Upper',inplace=True)
 tot["Title"].replace(['Don', 'Rev','Capt','Col','Major','Dr','Rev'], 'Officer',inplace=True)
 tot.replace({"Mlle": "Miss", "Ms": "Miss", "Mme":"Mrs"},inplace=True)
 temp = check_survive("Title" ).sort_values()
temp.iloc[:] = np.arange(len(temp))
dic = temp.to_dict()
tot.Title.replace(dic, inplace= True)
 def process_Family() :
global tot
tot["FamilySize"] = tot.SibSp + tot.Parch + 1
tot.FamilySize = tot.FamilySize.map(lambda x: 2 if 1< x < 5 else 1 if x == 1 else 0)
 def process_Embarked() :
global tot
tot.Embarked.fillna("S",inplace =True)
 tot.Embarked = tot.Embarked.replace({"S": 0, "Q":1, "C":2})
 def fareof(x):
if x < 7.5: return 0
if x < 14 : return 1
if x < 50 : return 2
if x < 80 : return 3
return 4
def process_Fare() :
global tot
tot.Fare.fillna(0, inplace=True)
 tot.Fare[tot.Fare.notna() ] = tot.Fare[tot.Fare.notna() ].map(lambda x: fareof(x))
def ageof(x):
if x < 10: return 0
if x < 25 : return 1
if x < 35 : return 2
return 3
def process_Age() :
global tot
tot.Age[tot.Age.notna() ] = tot.Age[tot.Age.notna() ].map(lambda x: ageof(x))
dic = tot[tot.Age.notna() ][["Age","Title"]].groupby("Title" ).Age.mean().to_dict()
tot.Age[tot.Age.isna() ] = tot.Title[tot.Age.isna() ].replace(dic ).map(lambda x: int(x))
def process_Ticket() :
global tot
tot["RT"] = tot.Ticket.map(lambda x: x.split(" ")[0] if len(x.split(" ")) ==1 else x.split(" ")[1])
 tot["LT"] = tot.Ticket.map(lambda x: str(x.split(" ")[0])[0] if len(x.split(" ")) >1 else np.nan)
 tot["Tlen"] = tot.RT.map(lambda x: len(x))
tot["RT"] = tot.RT.map(lambda x: str(x)[0])
 def process_Cabin() :
global tot
def process_all() :
global tot
process_Name()
process_Embarked()
process_Family()
process_Fare()
process_Sex()
process_Age()
process_Ticket()
process_Cabin() 
<count_values>
print("Classification Report:")
 print(metrics.classification_report(y_train, y_pred_random, target_names=unique_type))
lnames = tot.Name.map(lambda x: x.split(",")[0])
 tot.Name = lnames
tnum = tot.Ticket.map(lambda x: x.split(" ")[-1])
 tot.Ticket = tnum
tot["FamSize"] = tot.SibSp + tot.Parch
nlist = tot.Name.value_counts().index 
<categorify>
y_pred_random_test = random_pipe.predict(X_test )
tot["FamDeath"] = np.nan
for i in range(len(tot)) :
if tot.iloc[i,:].FamSize > 0:
hisname = tot.iloc[i,:].Name
hisfam = tot.iloc[i,:].FamSize
temp = pd.concat([tot.iloc[:i,:], tot.iloc[i+1:,:]])
 family = temp[(temp.Name == hisname)*(temp.FamSize == hisfam)]
if len(family)== 0:
continue
tot.FamDeath.iloc[i] = family.Survived.mean()
tot.FamDeath.fillna(0.5,inplace=True)
<drop_column>
print('Accuracy: '+ str(metrics.accuracy_score(y_test, y_pred_random_test)))
 print('Precision: '+ str(metrics.precision_score(y_test, y_pred_random_test, average='macro')))
 print('Recall: '+ str(metrics.recall_score(y_test, y_pred_random_test, average='macro')))
 print('F1_Score: '+ str(metrics.f1_score(y_test, y_pred_random_test, average='macro')) )
del tot["Ticket"], tot["Cabin"], tot["RT"], tot["LT"]
del tot["FamSize"], tot["Name"]<train_on_grid>
con_matrix_random= metrics.confusion_matrix(y_test, y_pred_random_test)
 cm_logistic_rf = pd.DataFrame(con_matrix_random, index=['ENFJ', 'ENFP', 'ENTJ', 'ENTP', 'ESFJ', 'ESFP',
'ESTJ', 'ESTP', 'INFJ', 'INFP', 'INTJ', 'INTP',
'ISFJ', 'ISFP', 'ISTJ', 'ISTP'],
columns=['predict_ENFJ','predict_ENFP','predict_ENTJ',
'predict_ENTP','predict_ESFJ','predict_ESFP',
'predict_ESTJ','predict_ESTP','predict_INFJ',
'predict_INFP','predict_INTJ','predict_INTP',
'predict_ISFJ','predict_ISFP','predict_ISTJ',
'predict_ISTP'])
 fig, ax = plt.subplots(figsize=(10,8))
sns.heatmap(cm_logistic_rf, robust=True, annot=True, linewidth=0.3,
fmt='', cmap='BrBG', vmax=303, ax=ax)
 plt.title('Confusion Matrix for Random Forest Classifier ', fontsize=10,
fontweight='bold', y=1.00)
 plt.xticks(fontsize=10)
 plt.yticks(rotation=0, fontsize=10)
 plt.show()
dropped = ["Survived","n","PassengerId","Embarked","Parch","Age","SibSp","Tlen"]
parameters = {
'n_estimators' : [100],
'random_state' : [1],
'n_jobs' : [3],
'min_samples_split': np.arange(8,12),
'max_depth' : np.arange(2,6)
 }
clf = grid_search.GridSearchCV(RandomForestClassifier() , parameters)
 clf.fit(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n==0].Survived)
 print(clf.best_estimator_)
<train_model>
print("Classification Report:")
 print(metrics.classification_report(y_test, y_pred_random_test, target_names=unique_type))
data = []
clf = clf.best_estimator_
num_trial = 10
for i in range(num_trial):
X_train, X_test, y_train, y_test = train_test_split(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n==0].Survived, random_state = i)
 clf.fit(X_train, y_train)
 data.append(clf.score(X_test, y_test))
plt.scatter(np.arange(num_trial),data )<predict_on_test>
test_preds_random = random_pipe.predict(z['clean'] )
clf.fit(tot[tot.n == 0].drop(dropped,axis = 1), tot[tot.n == 0].Survived)
 subm = tot[tot.n == 1].drop(["Survived"], axis = 1 ).join(pd.Series(clf.predict(tot[tot.n == 1].drop(dropped,axis = 1)) ,name="Survived"))<save_to_csv>
z['type'] = encoder.inverse_transform(test_preds_random)
 z.head()
subm = subm[["PassengerId","Survived"]].set_index("PassengerId")
 subm.Survived = subm.Survived.map(lambda x: int(x))
subm.to_csv("Submission.csv" )<import_modules>
z['E or I'] = z.apply(lambda x: x['type'][0], axis = 1)
 z['N or S'] = z.apply(lambda x: x['type'][1], axis = 1)
 z['T or F'] = z.apply(lambda x: x['type'][2], axis = 1)
 z['J or P'] = z.apply(lambda x: x['type'][3], axis = 1)
 mind = z['E or I'].astype(str ).apply(lambda x: x[0] == 'E' ).astype('int')
 energy = z['N or S'].astype(str ).apply(lambda x: x[0] == 'N' ).astype('int')
 nature = z['T or F'].astype(str ).apply(lambda x: x[0] == 'T' ).astype('int')
 tactics = z['J or P'].astype(str ).apply(lambda x: x[0] == 'J' ).astype('int')
 z.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns 
<load_from_csv>
df_random = pd.DataFrame({"id":test_df['id'], "mind":mind, "energy":energy, "nature":nature, 'tactics':tactics})
 df_random.head()
<drop_column><EOS> 
df_random.to_csv('EDSA_Team_8_Classification_random.csv', index = False )