Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<find_best_params>
import pandas as pd
import numpy as np
max_depth = list(range(1,30))
params_grid = {'max_depth':max_depth,'max_leaf_nodes':[2,3,4,5,6,7,8]}
dt_grid_model = GridSearchCV(dt_model,param_grid=params_grid)
<predict_on_test>
train = pd.read_csv(r'/kaggle/input/titanic/train.csv')
 test = pd.read_csv(r'/kaggle/input/titanic/test.csv' )
dt_grid_model.fit(X_train,y_train)
 y_dt_grid_pred = dt_grid_model.predict(X_test )<find_best_params>
train.isna().sum()
dt_grid_model.best_params_<import_modules>
dfs = [train ,test]
for df in dfs:
df['Age'].fillna(df['Age'].median() , inplace = True )
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
train.isna().sum()
rf_model = RandomForestClassifier()<train_model>
train['Cabin'].value_counts()
rf_model.fit(X_train,y_train )<predict_on_test>
letters = []
for i in cabins:
letter= i[0]
letters.append(letter )
y_rf_pred = rf_model.predict(X_test )<create_dataframe>
train['Cabin'] = letters
pd.DataFrame(index=X_train.columns,data=rf_model.feature_importances_,columns=['Feature Importance'])
<define_search_space>
letters = []
for i in cabins:
letter = i[0]
letters.append(letter )
no_of_trees = list(range(10,50,500))
max_depth = list(range(1,30))
param_grid_rf = {'n_estimators':no_of_trees,'criterion':['gini','entropy'],
'max_depth':max_depth,'max_leaf_nodes':[2,3,4,5,6,7,8]}<train_on_grid>
test['Cabin'] = letters
rf_grid_model = GridSearchCV(rf_model,param_grid=param_grid_rf )<predict_on_test>
train['Embarked'].value_counts()
rf_grid_model.fit(X_train,y_train)
 y_rf_grid_pred = rf_grid_model.predict(X_test )<find_best_params>
len(train[train['Pclass'] == 1]), len(train[train['Pclass'] == 2]), len(train[train['Pclass'] == 3] )
rf_grid_model.best_params_<compute_test_metric>
percentages = []
first = 136 / 216
second = 87/ 184
third = 119/491
percentages.append(first)
 percentages.append(second)
 percentages.append(third )
print('Logistic_Regression_best_accuracy: ',accuracy_score(y_test,y_lr_pred))
print('
')
 print('KNN_best_accuracy: ',accuracy_score(y_test,y_knn_grid_pred))
print('
')
 print('SVM_best_accuracy: ',accuracy_score(y_test,y_svc_grid_pred))
print('
')
 print('Decision_tree_best_accuracy: ',accuracy_score(y_test,y_dt_grid_pred))
print('
')
 print('Random_forest_best_accuracy: ',accuracy_score(y_test,y_rf_grid_pred))<predict_on_test>
percents = pd.DataFrame(percentages)
 percents.index+=1
test_pred = dt_grid_model.predict(X_test )<load_from_csv>
train['Family'] = train.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1)
 test['Family'] = test.apply(lambda x: x['SibSp'] + x['Parch'], axis = 1 )
test2 = pd.read_csv('/kaggle/input/titanic/test.csv')
 test2.head()<define_variables>
train.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True)
 test.drop(['SibSp', 'Parch', 'Name', 'Ticket'], axis = 1, inplace = True )
passengerid = test2['PassengerId']<save_to_csv>
test.isna().sum()
submission = pd.DataFrame({"PassengerId": passengerid,"Survived": test_pred})
 submission.PassengerId = submission.PassengerId.astype(int)
 submission.Survived = submission.Survived.astype(int)
 submission.to_csv("titanic1_submission.csv", index=False )<load_from_csv>
test['Fare'].fillna(test['Fare'].median() , inplace = True )
url="https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv"
s=requests.get(url ).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
 test_data_with_labels = c
test_data = pd.read_csv('.. /input/titanic/test.csv' )<set_options>
train_df = pd.get_dummies(train)
 test_df = pd.get_dummies(test )
warnings.filterwarnings('ignore' )<feature_engineering>
train_df.drop('PassengerId', axis = 1, inplace = True )
for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name )<filter>
y = train_df['Survived']
train_df.drop('Survived', axis = 1, inplace = True)
 train_df.drop('Cabin_T', axis = 1, inplace = True)
 test_df.drop('PassengerId', axis = 1, inplace = True )
survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))<save_to_csv>
X_test = test_df
X_train = train_df
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = survived
submission.to_csv('submission.csv', index=False )<load_from_csv>
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
%matplotlib inline
train= pd.read_csv('/kaggle/input/titanic/train.csv')
 def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else :
return 24
else:
return Age 
<feature_engineering>
rfc = RandomForestClassifier()
train['Age']=train[['Age','Pclass']].apply(impute_age,axis = 1 )<drop_column>
param_grid = {
'n_estimators': [200, 500, 1000],
'max_features': ['auto'],
'max_depth': [6, 7, 8],
'criterion': ['entropy']
}
train.drop('Cabin',inplace = True,axis =1 )<categorify>
CV = GridSearchCV(estimator = rfc, param_grid = param_grid, cv = 5)
 CV.fit(X_train, y)
 CV.best_estimator_
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True )<concatenate>
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, VotingClassifier
train = pd.concat([train,sex,embark],axis=1)
<drop_column>
rfc = RandomForestClassifier(criterion = 'entropy', max_depth = 8, n_estimators = 500, random_state = 42)
 gbc = GradientBoostingClassifier()
ada = AdaBoostClassifier()
train.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )<normalization>
rfc.fit(X_train, y)
 gbc.fit(X_train, y)
 ada.fit(X_train, y )
st = StandardScaler()
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale] )<categorify>
stack_gen = StackingCVClassifier(classifiers =(rfc, gbc, ada),
meta_classifier = rfc,
use_features_in_secondary = True)
 stack_gen.fit(X_train.values, y)
 y_pred = stack_gen.predict(X_test.values )
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale])
<prepare_x_and_y>
def blend(X_test):
y_pred =(( 0.25 * gbc.predict(X_test)) + \
0.25 * ada.predict(X_test)+ \
0.5 * stack_gen.predict(np.array(X_test)))
 return y_pred
x = train.drop(['Survived'],axis=1)
 y = train['Survived']<import_modules>
submission = y_pred.reshape(-1, 1 )
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier<train_model>
sub_df = pd.DataFrame(submission )
tree = DecisionTreeClassifier()
tree.fit(x,y)
 tree.score(x,y )<load_from_csv>
sub_df['PassengerId'] = test['PassengerId']
sub_df['Survived'] = submission
cols = ['PassengerId',
'Survived']
sub_df.drop(0, axis = 1, inplace = True)
 sub_df.columns = [i for i in cols]
sub_df = sub_df.set_index('PassengerId' )
test = pd.read_csv('/kaggle/input/titanic/test.csv' )<create_dataframe>
sub_df.to_csv(r'submission.csv' )