Kaggle competition: Titanic - Machine Learning from Disaster
def clean_text(text):
text = text.lower()
text = re.sub(r"what's", "what is ", text)
 text = re.sub(r"'s", " ", text)
 text = re.sub(r"'ve", " have ", text)
 text = re.sub(r"can't", "cannot ", text)
 text = re.sub(r"n't", " not ", text)
 text = re.sub(r"i'm", "i am ", text)
 text = re.sub(r"'re", " are ", text)
 text = re.sub(r"'d", " would ", text)
 text = re.sub(r"'ll", " will ", text)
 text = re.sub(r"'scuse", " excuse ", text)
 text = re.sub('\W', ' ', text)
 text = re.sub('\s+', ' ', text)
 text = text.strip(' ')
 return text<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train_df['comment_text'] = train_df['comment_text'].map(lambda com : clean_text(com))<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
test_df['comment_text'] = test_df['comment_text'].map(lambda com : clean_text(com))<drop_column>
print(train.isnull().sum() /train.shape[0] )
train_df = train_df.drop('char_length',axis=1 )<prepare_x_and_y>
print(test.isnull().sum() /test.shape[0] )
X = train_df.comment_text
test_X = test_df.comment_text<set_options>
print(train.groupby(['FamilyBin', 'Pclass'])[['Survived']].mean())
 print(train.groupby(['FamilyBin', 'Pclass'])[['Survived']].count() )
vect = TfidfVectorizer(max_features=5000,stop_words='english')
 vect<normalization>
print(train.groupby(['Embarked', 'Pclass'])[['Survived']].count())
 print(train.groupby(['Embarked', 'Pclass'])[['Survived']].mean())
 print(train.groupby(['Embarked'])[['Pclass']].count() )
X_dtm = vect.fit_transform(X)
 X_dtm<normalization>
def titleExtract(data):
data['Title'] = data.Name.str.extract('([A-Za-z]+)\.', expand=False)
 data['Title'] = data['Title'].replace(['Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer'], 'Mr')
 data['Title'] = data['Title'].replace(['Ms', 'Miss'], 'Miss')
 data['Title'] = data['Title'].replace(['Mlle', 'Mme','Lady', 'Countess', 'Dona'], 'Mrs')
 return data
train=titleExtract(train)
 test=titleExtract(test )
test_X_dtm = vect.transform(test_X)
 test_X_dtm<load_from_csv>
print(train.groupby(['Title', 'Sex'])[['Survived']].mean())
 print(train.groupby(['Title', 'Sex'])[['Survived']].count() )
logreg = LogisticRegression(C=12.0)
 submission_binary = pd.read_csv('.. /input/sample_submission.csv')
 for label in cols_target:
print('...Processing {}'.format(label))
y = train_df[label]
logreg.fit(X_dtm, y)
 y_pred_X = logreg.predict(X_dtm)
 print('Training accuracy is {}'.format(accuracy_score(y, y_pred_X)))
 test_y_prob = logreg.predict_proba(test_X_dtm)[:,1]
submission_binary[label] = test_y_prob<save_to_csv>
print(train.groupby(['Title', 'Pclass'])[['Survived']].mean())
 print(train.groupby(['Title', 'Pclass'])[['Survived']].count() )
submission_binary.to_csv('submission_binary.csv',index=False )<load_from_csv>
def mapTitle(data):
title_map = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4}
data['Title'] = data['Title'].map(title_map ).astype(int)
 return data
train=mapTitle(train)
 test=mapTitle(test )
submission_chains = pd.read_csv('.. /input/sample_submission.csv')
 def add_feature(X, feature_to_add):

 return hstack([X, csr_matrix(feature_to_add ).T], 'csr' )<predict_on_test>
def fill_fare_nulls(data):
data.loc[(data.Fare.isnull())&(data.Pclass==1), 'Fare']=60.3
data.loc[(data.Fare.isnull())&(data.Pclass==2), 'Fare']=14.25
data.loc[(data.Fare.isnull())&(data.Pclass==3), 'Fare']=8.05
return data
test=fill_fare_nulls(test)
for label in cols_target:
print('...Processing {}'.format(label))
y = train_df[label]
logreg.fit(X_dtm,y)
 y_pred_X = logreg.predict(X_dtm)
 print('Training Accuracy is {}'.format(accuracy_score(y,y_pred_X)))
 test_y = logreg.predict(test_X_dtm)
 test_y_prob = logreg.predict_proba(test_X_dtm)[:,1]
submission_chains[label] = test_y_prob
X_dtm = add_feature(X_dtm, y)
 print('Shape of X_dtm is now {}'.format(X_dtm.shape))
test_X_dtm = add_feature(test_X_dtm, test_y)
 print('Shape of test_X_dtm is now {}'.format(test_X_dtm.shape))<save_to_csv>
def splitClass_Train(data):
data.loc[(data.Fare>60.3)&(data.Pclass==1), 'PassengerCat']=0
data.loc[(data.Fare<=60.3)&(data.Pclass==1), 'PassengerCat']=1
data.loc[(data.Fare>14.3)&(data.Pclass==2), 'PassengerCat']=2
data.loc[(data.Fare<=14.3)&(data.Pclass==2), 'PassengerCat']=3
data.loc[(data.Fare>8.1)&(data.Pclass==3), 'PassengerCat']=4
data.loc[(data.Fare<=8.1)&(data.Pclass==3), 'PassengerCat']=5
data['PassengerCat']=data['PassengerCat'].astype(int)
 return data
train=splitClass_Train(train)
 def splitClass_Test(data):
data.loc[(data.Fare>60.00)&(data.Pclass==1), 'PassengerCat']=0
data.loc[(data.Fare<=60.00)&(data.Pclass==1), 'PassengerCat']=1
data.loc[(data.Fare>15.8)&(data.Pclass==2), 'PassengerCat']=2
data.loc[(data.Fare<=15.8)&(data.Pclass==2), 'PassengerCat']=3
data.loc[(data.Fare>7.9)&(data.Pclass==3), 'PassengerCat']=4
data.loc[(data.Fare<=7.9)&(data.Pclass==3), 'PassengerCat']=5
data['PassengerCat']=data['PassengerCat'].astype(int)
 return data
test=splitClass_Test(test)
submission_chains.to_csv('submission_chains.csv', index=False )<load_from_csv>
avgAges=train.groupby(['Title', 'Pclass'], as_index=False)['Age'].median()
print(avgAges)
 avgAges = avgAges['Age']
submission_combined = pd.read_csv('.. /input/sample_submission.csv' )<feature_engineering>
print(train.groupby(['AgeBand', 'Sex'])[['Survived']].mean())
 print(train.groupby(['AgeBand', 'Sex'])[['Survived']].count() )
for label in cols_target:
submission_combined[label] = 0.5*(submission_chains[label]+submission_binary[label] )<save_to_csv>
train=train.drop(columns=['Name', 'Ticket', 'PassengerId', 'Cabin', 'SibSp','Parch', 'Embarked', 'FamilyBin','FareBand', 'Pclass', 'Sex', 'AgeBand'])
 test=test.drop(columns=['Name', 'Ticket', 'PassengerId', 'Cabin','SibSp', 'Parch', 'Embarked', 'FamilyBin','FareBand', 'Pclass', 'Sex', 'AgeBand'] )
submission_combined.to_csv('submission_combined.csv', index=False )<define_variables>
train=pd.get_dummies(train,columns=['Title', 'PassengerCat'])
 test=pd.get_dummies(test,columns=['Title', 'PassengerCat'] )
nbsvm = '.. /input/nbsvm/submissionNBSVM.csv'
tfidflr = '.. /input/tfidf-and-lr/word_submission.csv'<load_from_csv>
print(train.isnull().sum())
 print("
Test Set:")
 print(test.isnull().sum() )
p_nbsvm = pd.read_csv(nbsvm)
 p_tfidflr = pd.read_csv(tfidflr )<feature_engineering>
from sklearn.ensemble import RandomForestClassifier
from sklearn import tree
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.feature_selection import f_regression
from sklearn.ensemble import GradientBoostingClassifier
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_nbsvm.copy()
p_res[label_cols] =(p_tfidflr[label_cols] + p_nbsvm[label_cols])/ 2<feature_engineering>
train_X=train.drop('Survived', axis=1)
 train_Y=train['Survived'].astype(int)
 test_X=test
KF=KFold(n_splits=10, random_state=1)
 models=[]
modelScores=[]
modelSTD=[]
p_res2 = p_nbsvm.copy()
for i, j in enumerate(p_res2):
if j == 'id':
continue
temp_df = pd.concat([p_tfidflr[j], p_nbsvm[j]], axis=1)
 print(temp_df.head())
 p_res2[j] = temp_df.apply(max, axis=1 )<feature_engineering>
LR_model = LogisticRegression(solver = 'lbfgs', max_iter = 3000)
 CV=cross_val_score(LR_model,train_X,train_Y,cv=KF, scoring="accuracy")
 CV.mean()
LR_model.fit(train_X, train_Y)
 models.append('LogisticRegression')
 modelScores.append(round(CV.mean() ,3))
modelSTD.append(round(CV.std() ,3))
p_res3 = p_nbsvm.copy()
for i, j in enumerate(p_res2):
if j == 'id':
continue
temp_df = pd.concat([p_tfidflr[j], p_nbsvm[j]], axis=1)
 print(temp_df.head())
 p_res3[j] = temp_df.apply(lambda r: np.max(r)if np.mean(r)> 0.5 else np.mean(r), axis=1)
 p_res3.head()<save_to_csv>
RFC = RandomForestClassifier()
RF_grid = {"max_depth": [None],
"max_features": [4, 6, 8],
"min_samples_split": [3, 5, 7],
"min_samples_leaf": [5, 10, 15],
"bootstrap": [True],
"n_estimators" :[500],
"criterion": ["gini"]}
gsRFC = GridSearchCV(RFC,param_grid = RF_grid, cv=10, scoring="accuracy", n_jobs= -1, verbose = 1)
 gsRFC.fit(train_X,train_Y)
 RFC_best = gsRFC.best_estimator_
print(gsRFC.best_score_)
 print(gsRFC.best_params_ )
p_res.to_csv('submissionAvg.csv', index=False)
 p_res2.to_csv('submissionMax.csv', index=False)
 p_res3.to_csv('submissionCondMax.csv', index=False )<feature_engineering>
RF_model = RandomForestClassifier(bootstrap= True,
criterion = 'gini',
max_depth = None,
max_features=8,
min_samples_leaf = 5,
min_samples_split = 7,
n_estimators = 500, random_state=1)
 RF_model.fit(train_X, train_Y)
 CV=cross_val_score(RF_model,train_X,train_Y,cv=KF, scoring="accuracy")
 models.append('RandomForest')
 modelScores.append(round(CV.mean() ,3))
modelSTD.append('NA')
 featureImportance = pd.concat(( pd.DataFrame(train_X.columns, columns = ['Feature']),
pd.DataFrame(RF_model.feature_importances_, columns = ['Importance'])) ,
axis = 1 ).sort_values(by='Importance', ascending = False)[:20]
plt.subplots(figsize=(20,8))
sns.barplot(x=featureImportance['Feature'], y=featureImportance['Importance'], alpha=.5 ).set_title('Feature Importance' )
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
Actual_1 = 'My life is totally awesome'
Predict_1 = 'awesome life'
Actual_2 = 'We are active kagglers'
Predict_2 = 'We are active kagglers'
print("Jaccard score for first set of scentences: {}".format(jaccard(Actual_1,Predict_1)))
 print("Jaccard score for second set of scentences: {}".format(jaccard(Actual_2,Predict_2)) )<set_options>
ModelComparison=pd.DataFrame({'CV Score':modelScores, 'Std':modelSTD}, index=models)
 ModelComparison
<load_from_csv><EOS> 
test_ID = pd.read_csv('.. /input/test.csv')
 test_ID = test_ID['PassengerId']
Survival_predictions = RF_model.predict(test)
 ID=np.arange(892,1310,1)
 submission=pd.DataFrame({
"PassengerId": ID,
"Survived": Survival_predictions
})
 submission.to_csv('submission.csv', index=False )