Kaggle competition: Titanic - Machine Learning from Disaster
cm = LogisticRegression(penalty='l1', solver='liblinear', C=4, random_state=11)
 pred_train, pred_test, loss_train, loss_test, acc_test, matrix_test = build_model(X_train_vect, X_test_vect, y_train['mind'], y_test['mind'], cm)
 ll_scores.append(loss_test)
 ac_scores.append(acc_test)
 print('Mind:')
 print('Log loss on training set: ',loss_train)
 print('Log loss on test set: ', loss_test)
 print('Accuracy on test set:', acc_test)
 print('Test set confusion matrix:
', matrix_test )<train_model>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv")
 print(train.shape)
 train.head()
cm = LogisticRegression(penalty='l1', solver='liblinear', C=5, random_state=11)
 pred_train, pred_test, loss_train, loss_test, acc_test, matrix_test = build_model(X_train_vect, X_test_vect, y_train['energy'], y_test['energy'], cm)
 ll_scores.append(loss_test)
 ac_scores.append(acc_test)
 print('Energy:')
 print('Log loss on training set: ',loss_train)
 print('Log loss on test set: ', loss_test)
 print('Accuracy on test set:', acc_test)
 print('Test set confusion matrix:
', matrix_test )<train_model>
train.isnull().sum()
cm = LogisticRegression(penalty='l1', solver='liblinear', C=5, random_state=11)
 pred_train, pred_test, loss_train, loss_test, acc_test, matrix_test = build_model(X_train_vect, X_test_vect, y_train['nature'], y_test['nature'], cm)
 ll_scores.append(loss_test)
 ac_scores.append(acc_test)
 print('Nature:')
 print('Log loss on training set: ',loss_train)
 print('Log loss on test set: ', loss_test)
 print('Accuracy on test set:', acc_test)
 print('Test set confusion matrix:
', matrix_test )<train_model>
train = train.drop(["Cabin"],axis = 1)
 print(train.shape)
 train.head()
cm = LogisticRegression(penalty='l1', solver='liblinear', C=3, random_state=11)
 pred_train, pred_test, loss_train, loss_test, acc_test, matrix_test = build_model(X_train_vect, X_test_vect, y_train['tactics'], y_test['tactics'], cm)
 ll_scores.append(loss_test)
 ac_scores.append(acc_test)
 print('Tactics:')
 print('Log loss on training set: ',loss_train)
 print('Log loss on test set: ', loss_test)
 print('Accuracy on test set:', acc_test)
 print('Test set confusion matrix:
', matrix_test )<compute_test_metric>
train.isnull().sum()
print('Average log loss across personality categories: ', round(np.mean(ll_scores), 3))
print('Average accuracy across personality categories: ', round(np.mean(ac_scores), 3))<prepare_x_and_y>
train.isnull().sum()
results = test[['id']]
X_train_vect, X_test_vect = vectorise(train_clean['posts'], test_clean['posts'])
 X_train_vect.shape<predict_on_test>
train['Embarked'].fillna(method = 'backfill', inplace = True )
c_vals = [4, 5, 5, 3]
class1_proba = []
for i in range(4):
cm = LogisticRegression(penalty='l1', solver='liblinear', C=c_vals[i], random_state=11)
 cm.fit(X_train_vect, train[categories[i]].values)
 pred_test = cm.predict(X_test_vect)
 results[categories[i]] = pred_test
probability = cm.predict_proba(X_test_vect)
 class1_proba.append(probability[:,1] )<save_to_csv>
train['Age'].fillna(train['Age'].median() , inplace = True )
results.to_csv('MBTI_l1_liblinear_C4553_split_tt_all_pp_emoji_lib_handles.csv', index=False)
<feature_engineering>
train.isnull().sum()
for i in range(4):
results[categories[i]] = class1_proba[i]<save_to_csv>
train.isnull().sum()
results.to_csv('MBTI_l1_liblinear_C4553_split_tt_all_pp_emoji_lib_handles_proba.csv', index=False)
<set_options>
def str_to_cat(training_df):
for p,q in training_df.items() :
if is_string_dtype(q):
training_df[p] = q.astype('category' ).cat.as_ordered()
return training_df
sns.set_style('whitegrid')
<load_from_csv>
def mydf_to_nums(training_df, feature, null_status):
if not is_numeric_dtype(feature):
training_df[null_status] = feature.cat.codes + 1
def mydf_imputer(training_df, feature, null_status, null_table):
if is_numeric_dtype(feature):
if pd.isnull(feature ).sum() or(null_status in null_table):
filler = null_table[null_status] if null_status in null_table else feature.median()
training_df[null_status] = feature.fillna(filler)
 null_table[null_status] = filler
return null_table
def mydf_preprocessor(training_df, null_table):

 if null_table is None:
null_table = dict()
for p,q in training_df.items() :
null_table = mydf_imputer(training_df, q, p, null_table)
 for p,q in training_df.items() :
mydf_to_nums(training_df, q, p)
 training_df = pd.get_dummies(training_df, dummy_na = True)
 res = [training_df, null_table]
return res
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
<drop_column>
train = str_to_cat(train)
 train_df,my_table = mydf_preprocessor(train,null_table = None)
 print(train_df.shape)
 train_df.head()
ntrain = train.shape[0]
ntest = test.shape[0]
all_data = pd.concat(( train, test), sort=False ).reset_index(drop=True)
 all_data.drop("id", axis=1, inplace=True)
<groupby>
train.duplicated().sum()
grouped_wordclouds = all_data.groupby('type' ).sum()
grouped_wordclouds 
<create_dataframe>
test.isnull().sum()
<groupby>
test.isnull().sum()
print("There are {} words in the combination of all posts.".format(len(grouped_wordclouds['posts'].sum())) )<feature_engineering>
test = test.drop(["Cabin"],axis = 1)
 print(test.shape )
pattern_url = r'http[s]?://(?:[A-Za-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9A-Fa-f][0-9A-Fa-f])) +'
subs_url = r'url-web'
all_data['posts'] = all_data['posts'].replace(to_replace=pattern_url, value=subs_url, regex=True)
 all_data['posts'] = all_data['posts'].str.lower() 
<feature_engineering>
test['Age'].fillna(test['Age'].median() , inplace = True )
def remove_punctuation_numbers(post):

 punc_numbers = string.punctuation + '0123456789'
return ''.join([l for l in post if l not in punc_numbers])
 all_data['posts'] = all_data['posts'].apply(remove_punctuation_numbers)
<feature_engineering>
class_fares = test.groupby('Pclass')['Fare'].median()
test['median_fare'] = test['Pclass'].apply(lambda x: class_fares[x])
 test['Fare'].fillna(test['median_fare'], inplace = True,)
 del test['median_fare']
tokeniser = TreebankWordTokenizer()
all_data['tokens'] = all_data['posts'].apply(tokeniser.tokenize)
<feature_engineering>
test.isnull().sum()
stemmer = SnowballStemmer('english')
 def train_stemmer(words, stemmer):

 return [stemmer.stem(word)for word in words]
all_data['stem'] = all_data['tokens'].apply(train_stemmer, args=(stemmer,)) 
<categorify>
test.isnull().sum()
lemmatizer = WordNetLemmatizer()
def train_lemma(words, lemmatizer):

 return [lemmatizer.lemmatize(word)for word in words]
all_data['lemma'] = all_data['tokens'].apply(train_lemma, args=(lemmatizer,)) 
<feature_engineering>
test = str_to_cat(test)
 test,my_table = mydf_preprocessor(test,null_table = None)
 print(test.shape)
 test.head(5 )
detokenizer = TreebankWordDetokenizer()
all_data['detoken'] = all_data['lemma'].apply(lambda x: detokenizer.detokenize(x)) 
<split>
test.duplicated().sum()
train = all_data[:ntrain]
test = all_data[ntrain:] 
<split>
train = train.drop('Name', axis = 1)
 test = test.drop(columns=['Name'], axis = 1 )
X = train.detoken
y = train.type
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 X = pd.concat([X_train, y_train], axis=1)
 ESTP = X[X.type == 'ESTP']
ESTJ = X[X.type == 'ESTJ']
ESFP = X[X.type == 'ESFP']
ESFJ = X[X.type == 'ESFJ']
ENTP = X[X.type == 'ENTP']
ENTJ = X[X.type == 'ENTJ']
ENFP = X[X.type == 'ENFP']
ENFJ = X[X.type == 'ENFJ']
INFP = X[X.type == 'INFP']
mbti_upsampledESTP = resample(ESTP,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledESFP = resample(ESFP,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledESFJ = resample(ESFJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledESTJ = resample(ESTJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledENFJ = resample(ENFJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledENTJ = resample(ENTJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledENTP = resample(ENTP,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledENFP = resample(ENFP,
replace=True,
n_samples=len(INFP),
random_state=27)
 upsampled_E = pd.concat([INFP, mbti_upsampledESTP,mbti_upsampledESTJ,mbti_upsampledESFP,mbti_upsampledESFJ,mbti_upsampledENFJ,mbti_upsampledENTJ,mbti_upsampledENTP,mbti_upsampledENFP])
 upsampled_E.type.value_counts() 
<filter>
X_train = train.drop('Survived', axis=1)
 X_train = pd.get_dummies(X_train,columns=['Embarked','Pclass'])
 Y_train = train['Survived']
print(X_train.shape,Y_train.shape )
ISTP = X[X.type == 'ISTP']
ISTJ = X[X.type == 'ISTJ']
ISFP = X[X.type == 'ISFP']
ISFJ = X[X.type == 'ISFJ']
INTP = X[X.type == 'INTP']
INTJ = X[X.type == 'INTJ']
INFJ = X[X.type == 'INFJ']
INFP = X[X.type == 'INFP']
mbti_upsampledISTP = resample(ISTP,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledISFP = resample(ISFP,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledISFJ = resample(ISFJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledISTJ = resample(ISTJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledINFJ = resample(INFJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledINTJ = resample(INTJ,
replace=True,
n_samples=len(INFP),
random_state=27)
 mbti_upsampledINTP = resample(INTP,
replace=True,
n_samples=len(INFP),
random_state=27)
 upsampled_I = pd.concat([mbti_upsampledISTP, mbti_upsampledISTJ, mbti_upsampledISFP, mbti_upsampledISFJ, mbti_upsampledINFJ, mbti_upsampledINTJ, mbti_upsampledINTP])
 upsampled_I.type.value_counts() 
<concatenate>
scaler = StandardScaler().fit(X_train)
 scaled_X = scaler.transform(X_train)
 print(scaled_X )
upsampled_data = pd.concat([upsampled_E,upsampled_I])
 upsampled_data.head()<split>
x_test = pd.get_dummies(test,columns=['Embarked','Pclass'] )
X_train = upsampled_data.detoken
y_train = upsampled_data.type 
<predict_on_test>
sc = StandardScaler().fit(x_test)
 sc_X = sc.transform(x_test)
 print(sc_X )
clf = DummyClassifier(strategy='most_frequent' ).fit(X_train, y_train)
 y_pred = clf.predict(X_test)
 print(' Our baseline accuracy is %s' % accuracy_score(y_pred, y_test)) 
<train_model>
model_1 = RandomForestClassifier(n_jobs = -1, n_estimators = 10,
bootstrap = True)
 model_1.fit(scaled_X,Y_train )
nb = Pipeline([('vect', CountVectorizer(stop_words='english', ngram_range=(1, 2))), 
('tfidf', TfidfTransformer(sublinear_tf=True, norm='l2')) , 
('clf', MultinomialNB())])
 nb.fit(X_train, y_train)
<predict_on_test>
pred = model_1.predict(scaled_X )
y_predNB = nb.predict(X_test)
 print('accuracy %s' % accuracy_score(y_predNB, y_test)) 
<predict_on_test>
parameters = {'n_estimators': [20, 50, 60, 80, 90, 100, 120, 150, 200], 'max_features': ["auto", "sqrt", "log2"]}
rfc = RandomForestClassifier(random_state = 1)
 cls = GridSearchCV(estimator = rfc, param_grid = parameters)
 cls.fit(scaled_X, Y_train)
 cls.best_params_
y_probsNB = nb.predict_proba(X_test)
 print("The log loss error for our model is: ", log_loss(y_test, y_probsNB))<train_model>
model_2 = RandomForestClassifier(n_estimators=80, max_features="auto")
 model_2.fit(scaled_X, Y_train)
 y_pred = model_2.predict(sc_X )
logreg = Pipeline([('vect', CountVectorizer(stop_words='english', ngram_range=(1, 2))), 
('tfidf', TfidfTransformer(norm='l2')) , 
('clf', LogisticRegression(n_jobs=1, C=1e5)) ])
 logreg.fit(X_train, y_train)
<predict_on_test>
sub_df = pd.DataFrame({'PassengerId' : test["PassengerId"],'Survived' : y_pred})
 submission = sub_df.to_csv('submission.csv',index=False )