Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
%matplotlib inline
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 GradientBoostingClassifier, ExtraTreesClassifier, VotingClassifier)
test_id=test.id
test=test.drop('id',axis=1)
 test=test/255
test=test.values.reshape(-1,28,28,1)
<predict_on_test>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 target = train['Survived']
dataset = train.append(test, sort=False)
 PassengerId = test['PassengerId']
print('train', train.shape)
 print('test', test.shape)
 train.head()
y_pre=model.predict(test)
 y_pre=np.argmax(y_pre,axis=1 )<save_to_csv>
label = LabelEncoder()
dataset['Fare'].fillna(dataset['Fare'].median() , inplace=True)
 dataset['Embarked'].fillna(dataset['Embarked'].mode() [0], inplace=True)
 dataset['Age'].fillna(dataset['Age'].median() , inplace=True)
 dataset['Fare'] = pd.qcut(dataset['Fare'], 4)
 dataset['Age'] = pd.cut(dataset['Age'].astype('int'), 5)
 dataset['Title'] = dataset['Name'].str.split(', ', expand=True)[1].str.split('.', expand=True)[0]
title_names =(dataset['Title'].value_counts() < 10)
 dataset['Title'] = dataset['Title'].apply(lambda x: 'Other' if title_names.loc[x] == True else x)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['Alone'] = 0
dataset['Alone'].loc[dataset['FamilySize'] > 1] = 1
dataset['HasCabin'] = dataset['Cabin'].apply(lambda x: 0 if type(x)== float else 1)
 dataset['Name_length'] = dataset['Name'].apply(len)
 dataset['Sex'] = label.fit_transform(dataset['Sex'])
 dataset['Title'] = label.fit_transform(dataset['Title'])
 dataset['Embarked'] = label.fit_transform(dataset['Embarked'])
 dataset['Fare'] = label.fit_transform(dataset['Fare'])
 dataset['Age'] = label.fit_transform(dataset['Age'] )
sample_sub['label']=y_pre
sample_sub.to_csv('submission.csv',index=False)
<set_options>
dataset['LastName'] = dataset['Name'].apply(lambda x: str.split(x, ",")[0])
 DEFAULT_SURVIVAL_VALUE = 0.5
dataset['FamilySurvival'] = DEFAULT_SURVIVAL_VALUE
for grp, grp_df in dataset.groupby(['LastName', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
dataset.loc[dataset['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
dataset.loc[dataset['PassengerId'] == passID, 'FamilySurvival'] = 0
for _, grp_df in dataset.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['FamilySurvival'] == 0)|(row['FamilySurvival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
dataset.loc[dataset['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
dataset.loc[dataset['PassengerId'] == passID, 'FamilySurvival'] = 0
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
dataset.drop(['Name', 'LastName', 'PassengerId', 'SibSp', 'Parch', 'Ticket', 'Cabin'], axis=1, inplace=True)
 ss = StandardScaler()
ss.fit_transform(dataset)
 train = dataset[:891]
test = dataset[891:]
from fastai import *
from fastai.vision import *
import imageio<load_from_csv>
print(train[['Sex', 'Survived']].groupby(['Sex'], as_index=False ).mean())
 print(train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean())
 print(train[['Alone', 'Survived']].groupby(['Alone'], as_index=False ).mean())
 print(train[['HasCabin', 'Survived']].groupby(['HasCabin'], as_index=False ).mean())
 print(train[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean())
 print(train[['Fare', 'Survived']].groupby(['Fare'], as_index=False ).mean())
 print(train[['Age', 'Survived']].groupby(['Age'], as_index=False ).mean())
 print(train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean() )
path = Path('.. /input/Kannada-MNIST')
 train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test =pd.read_csv('.. /input/Kannada-MNIST/test.csv' )<train_model>
train_y = train['Survived']
train.drop(['Survived'], axis=1, inplace=True)
 test.drop(['Survived'], axis=1, inplace=True)
 train_X = train
def to_img_shape(data_X, data_y=[]):
data_X = np.array(data_X ).reshape(-1,28,28)
 data_X = np.stack(( data_X,)*3, axis=-1)
 data_y = np.array(data_y)
 return data_X,data_y<split>
rf_parameters = {
'n_estimators': [300, 500],
'warm_start': [True],
'max_depth' : [n for n in range(6, 12)],
'max_features': [n for n in range(5, 8)],
"min_samples_split": [n for n in range(8, 11)],
"bootstrap": [True, False]
}
et_parameters = {
'n_estimators': [300, 500],
'max_depth' : [n for n in range(2, 10)],
'max_features': [n for n in range(5, 8)],
"min_samples_leaf": [n for n in range(1, 6)],
"bootstrap": [True, False]
}
ada_parameters = {
'n_estimators': [300, 500],
'learning_rate': [0.01, 0.05, 0.1, 0.2, 0.35, 0.5, 0.75, 1]
}
gb_parameters = {
'loss' : ["deviance","exponential"],
'n_estimators' : [300, 500],
'learning_rate': [0.0025, 0.005, 0.0075, 0.01, 0.05, 0.1],
'max_depth': [n for n in range(2, 6)],
'max_features': [n for n in range(2, 6)],
'min_samples_leaf': [n for n in range(1, 7)],
}
svc_parameters = {
'kernel': ['rbf', 'linear'],
'C': [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1],
'gamma': [0.001 ,0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 10],
'probability': [True]
}
data_X, data_y = train.loc[:,'pixel0':'pixel783'], train['label']
train_X, val_X, train_y, val_y = train_test_split(data_X, data_y, test_size=0.01,random_state=7,stratify=data_y )<categorify>
rfc_cv = GridSearchCV(RandomForestClassifier() , rf_parameters, cv=4, n_jobs=8 ).fit(train_X, train_y)
 etc_cv = GridSearchCV(ExtraTreesClassifier() , et_parameters, cv=4, n_jobs=4 ).fit(train_X, train_y)
 ada_cv = GridSearchCV(AdaBoostClassifier() , ada_parameters, cv=4, n_jobs=4 ).fit(train_X, train_y)
 gbc_cv = GridSearchCV(GradientBoostingClassifier() , gb_parameters, cv=4, n_jobs=4 ).fit(train_X, train_y)
 svc_cv = GridSearchCV(SVC() , svc_parameters, cv=4, n_jobs=4 ).fit(train_X, train_y )
train_X,train_y = to_img_shape(train_X, train_y)
 val_X,val_y = to_img_shape(val_X,val_y )<categorify>
cols = train.columns.values
feature_dataframe = pd.DataFrame(
 {
'Random Forest feature importances': rfc_cv.best_estimator_.feature_importances_,
'Extra Trees feature importances': etc_cv.best_estimator_.feature_importances_,
'AdaBoost feature importances': ada_cv.best_estimator_.feature_importances_,
'Gradient Boost feature importances': gbc_cv.best_estimator_.feature_importances_
}
)
tfms = get_transforms(do_flip=False)
 data =(ImageList.from_folder('/data/')
.split_by_folder() 
.label_from_folder() 
.add_test_folder() 
.transform(tfms, size=64)
.databunch() )<define_variables>
feature_dataframe['mean'] = feature_dataframe.mean(axis=1)
 feature_dataframe.head(3 )
data.show_batch(3,figsize=(6,6))<load_pretrained>
vc = VotingClassifier(estimators=[ 
('rfc', rfc_cv.best_estimator_), 
('etc', etc_cv.best_estimator_), 
('ada', ada_cv.best_estimator_), 
('gbc', gbc_cv.best_estimator_), 
('svm', svc_cv.best_estimator_)
 ], voting='soft', n_jobs=4)
 vc = vc.fit(train_X, train_y )
!mkdir -p /tmp/.cache/torch/checkpoints
!cp /kaggle/input/fastai-pretrained-models/resnet50-19c8e357.pth /tmp/.cache/torch/checkpoints/resnet50-19c8e357.pth
learn = cnn_learner(data, models.resnet50, metrics=[error_rate, accuracy], model_dir = Path('.. /kaggle/working'),path = Path("."))<train_model>
predictions = vc.predict(test)
 submission = pd.DataFrame({
"PassengerId": PassengerId,
"Survived": predictions
}, dtype='int64')
 submission.head(15 )
<find_best_params><EOS> 
submission.to_csv('titanic.csv', index=False )