Kaggle competition: Titanic - Machine Learning from Disaster
submission = pd.DataFrame({
"id": df_test['id'],
"target": y_pred_test
})
 submission.to_csv('submission.csv', index=False )<load_pretrained>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.pipeline import Pipeline
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import LabelEncoder,MinMaxScaler
from sklearn.ensemble import GradientBoostingClassifier
xtr = np.load('.. /input/xtr.npy')
 xte = np.load('.. /input/xte.npy')
 ytr = np.load('.. /input/ytr.npy' )<data_type_conversions>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 target=train['Survived']
submit=pd.DataFrame(test['PassengerId'] )
xtr = np.expand_dims(xtr, -1 ).astype('float32')/ 255
xte = np.expand_dims(xte, -1 ).astype('float32')/ 255
ytr = np.array([[1 if ytr[i] == j else 0 for j in range(10)] for i in range(len(ytr)) ] )<choose_model_class>
print(train.isnull().sum())
 train['Age'].fillna(train['Age'].mean() ,inplace=True)
 train['Embarked'].fillna('S',inplace=True)
 train.drop('Cabin',axis=1,inplace=True)
 print(train.isnull().sum() )
model = Sequential()
model.add(Conv2D(16, 5, input_shape=(28, 28, 1), padding='same', activation='relu'))
model.add(MaxPool2D())
 model.add(Conv2D(32, 5, padding='same', activation='relu'))
model.add(MaxPool2D())
 model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'] )<choose_model_class>
print(test.isnull().sum())
 test['Age'].fillna(test['Age'].mean() ,inplace=True)
 test['Fare'].fillna(test['Fare'].median() ,inplace=True)
 test.drop('Cabin',axis=1,inplace=True)
 print(test.isnull().sum() )
datagen = ImageDataGenerator(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2 )<define_variables>
title=[]
for i in range(len(train['Name'])) :
a=train.loc[i,'Name'].split() [1]
title.append(a)
 title=pd.Series(title)
 train['Title']=title
title1=[]
for i in range(len(test['Name'])) :
a=test.loc[i,'Name'].split() [1]
title1.append(a)
 title1=pd.Series(title1)
 test['Title']=title1
train['Passenger']=train['SibSp']+train['Parch']+1
test['Passenger']=test['SibSp']+test['Parch']+1
train_idx = np.random.choice(range(len(xtr)) , size=int(np.floor(0.8 * len(xtr))), replace=False)
 val_idx = np.array([i for i in range(len(xtr)) if i not in train_idx] )<prepare_x_and_y>
encode=LabelEncoder()
train['Sex']=encode.fit_transform(train['Sex'])
 test['Sex']=encode.fit_transform(test['Sex'])
 train['Title']=encode.fit_transform(train['Title'])
 test['Title']=encode.fit_transform(test['Title'])
 train['Embarked']=encode.fit_transform(train['Embarked'])
 test['Embarked']=encode.fit_transform(test['Embarked'] )
xtrain = xtr[train_idx, :, :]
ytrain = ytr[train_idx, :]
xval = xtr[val_idx, :, :]
yval = ytr[val_idx, :]<train_model>
train['New1']=train['Sex']*train['Pclass']
test['New1']=test['Sex']*test['Pclass']
early_stop = EarlyStopping(patience=3, monitor='val_loss')
 batch_size = 128
epochs=50
model.fit_generator(datagen.flow(xtrain, ytrain, batch_size=batch_size),
steps_per_epoch = len(xtr)// batch_size,
epochs=epochs,
validation_data=(xval, yval),
callbacks=[early_stop] )<save_to_csv>
train.drop(['Name','Ticket','PassengerId','Parch','SibSp'],axis=1,inplace=True)
 test.drop(['Name','Ticket','PassengerId','Parch','SibSp'],axis=1,inplace=True )
predictions = model.predict(xte)
 preds = predictions.argmax(axis=1)
 submission = pd.DataFrame({'id': range(len(xte)) , 'label': preds})
 submission.to_csv('my_submission.csv', index=None )<import_modules>
steps=list()
steps.append(( 'scaler',MinMaxScaler()))
steps.append(( 'model',GradientBoostingClassifier()))
pipeline=Pipeline(steps=steps)
 x=train.drop('Survived',axis=1)
 y=train['Survived']
import pandas as pd
from sklearn.metrics import log_loss<load_from_csv>
cv=RepeatedStratifiedKFold(n_splits=10,n_repeats=10,random_state=1)
 score=cross_val_score(pipeline,x,y,scoring='accuracy',cv=cv,n_jobs=-1)
 print('Accuracy: %.3f(%.3f)' %(np.mean(score)*100, np.std(score)*100))
df_train = pd.read_csv('.. /input/dmia-sport-2019-fall-competition-1/train.csv' )<count_unique_values>
model=GradientBoostingClassifier()
model.fit(x,y)
 pred=model.predict(test)
 submit['Survived']=pred
checks_num = df_train.check_id.nunique()
checks_num<remove_duplicates>
submit.to_csv('ver1.csv',index=False )