Kaggle competition: Titanic - Machine Learning from Disaster
model2 = build_model(input_shape=(28, 28, 1), classes = 10 )<choose_model_class>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 gender_submision=pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 train.head()
sgd2 = SGD(lr=0.1, momentum=0.0, decay=0.0, nesterov=False)
 learning_rate_reduction2 = ReduceLROnPlateau(monitor='val_loss', patience=5, verbose=1, factor=0.5, min_lr=0.00001)
 checkpoint2 = ModelCheckpoint("bestmodel2.model", monitor='val_acc', verbose=1, save_best_only=True )<choose_model_class>
train['titlelst']=train.Name.str.split(' ' ).apply(lambda x:x[-1])
 test['titlelst']=test.Name.str.split(' ' ).apply(lambda x:x[-1])
 train['title1st']=train.Name.str.split(',' ).apply(lambda x:x[1] ).str.split('.' ).apply(lambda x:x[0])
 test['title1st']=test.Name.str.split(',' ).apply(lambda x:x[1] ).str.split('.' ).apply(lambda x:x[0] )
model2.compile(loss='categorical_crossentropy', optimizer=sgd2, metrics=['acc'] )<train_model>
dict(test.title1st.value_counts() )
history2 = model2.fit_generator(
 train_datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=test_datagen.flow(x_test, y_test),
validation_steps=x_test.shape[0] // batch_size,
callbacks=[checkpoint2, learning_rate_reduction2] )<prepare_output>
print(train.titlelst.value_counts())
 droplist=['titlelst']
results=model2.predict(img_test/255.0)
 results=np.argmax(results, axis=1)
 sub=pd.DataFrame()
sub['id']=list(test_data.values[0:,0])
 sub['label']=results<save_to_csv>
droplist.append('Name' )
sub.to_csv("submission.csv", index=False )<load_from_csv>
print(train.Ticket.str.isnumeric().value_counts())
 train.Ticket[train.Ticket.str.isnumeric().apply(lambda x:not x)]
kannada_test_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 kannada_train_data =pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 kannada_dig_data = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')
 print(kannada_test_data.head())
 print(kannada_train_data.head())
 print(kannada_dig_data.head())
 print(kannada_test_data.shape, kannada_train_data.shape, kannada_dig_data.shape)
<count_values>
train.Ticket=train.Ticket.apply(lambda x:'num' if x.isnumeric() else x)
 train.Ticket=train.Ticket.str.split('/' ).apply(lambda x:x[0] ).str.split(' ' ).apply(lambda x:x[0] ).str.split('.' ).apply(lambda x:x[0] )
kannada_train_data['label'].value_counts().sort_index()<count_values>
test.Ticket=test.Ticket.apply(lambda x:'num' if x.isnumeric() else x)
 test.Ticket=test.Ticket.str.split('/' ).apply(lambda x:x[0] ).str.split(' ' ).apply(lambda x:x[0] ).str.split('.' ).apply(lambda x:x[0] )
kannada_dig_data['label'].value_counts().sort_index()<prepare_x_and_y>
train.Ticket.value_counts()
X = kannada_train_data.drop(['label'], axis=1 ).values
Y = kannada_train_data['label']
X_test = kannada_test_data.drop(['id'], axis=1 ).values
X_test_1 = kannada_dig_data.drop(['label'], axis=1 ).values
Y_test_1 = kannada_dig_data['label']
print(X.shape, Y.shape, X_test.shape, X_test_1.shape, Y_test_1.shape )<split>
train.Ticket.replace(dict(zip(['WE','P','SW','Fa','F4','SO','SCO'],['Rare']*7)) ,inplace=True)
 test.Ticket.replace(dict(zip(['WE','P','SW','Fa','F4','SO','SCO','LP','AQ'],['Rare']*9)) ,inplace=True )
X_train, X_devtest, Y_train, Y_devtest = train_test_split(X, Y, test_size = 0.2)
 print(X_train.shape, Y_train.shape)
 print(X_devtest.shape, Y_devtest.shape )<categorify>
train['family_size']=train.SibSp+train.Parch+1
train['issingle']=train.family_size.apply(lambda x:1 if x==1 else 0)
 train['smallfamily']=train.family_size.apply(lambda x:1 if x<=3 and x>1 else 0)
 train['middfamily']=train.family_size.apply(lambda x:1 if x<=8 and x>3 else 0)
 train['largefamily']=train.family_size.apply(lambda x:1 if x>8 else 0 )
n_x = 28
train_img = X_train.reshape(( -1,n_x,n_x, 1)).astype('float32')/255
dev_img = X_devtest.reshape(( -1,n_x,n_x,1)).astype('float32')/255
test_img = X_test.reshape(( -1,n_x,n_x,1)).astype('float32')/255
dig_img = X_test_1.reshape(( -1,n_x,n_x,1)).astype('float32')/255
print(train_img.shape, dev_img.shape, test_img.shape, dig_img.shape)
 train_labels = to_categorical(Y_train)
 dev_labels = to_categorical(Y_devtest)
 dig_labels = to_categorical(Y_test_1)
 print(train_labels.shape, dev_labels.shape, dig_labels.shape)
 print(Y_test_1[8],dig_labels[8])
 plt.figure(figsize=(1,1))
plt.imshow(X_test_1[8].reshape(( 28,28)) , cmap=plt.cm.binary)
 plt.show() 
<init_hyperparams>
test['family_size']=test.SibSp+test.Parch+1
test['issingle']=test.family_size.apply(lambda x:1 if x==1 else 0)
 test['smallfamily']=test.family_size.apply(lambda x:1 if x<=3 and x>1 else 0)
 test['middfamily']=test.family_size.apply(lambda x:1 if x<=8 and x>3 else 0)
 test['largefamily']=test.family_size.apply(lambda x:1 if x>8 else 0 )
data_augment = ImageDataGenerator(rotation_range =10, zoom_range =0.1, width_shift_range =0.1, height_shift_range = 0.1 )<choose_model_class>
train.isnull().sum()
model = models.Sequential()
model.add(layers.Conv2D(16, kernel_size=3, padding ='same', activation = 'relu', input_shape=(28,28,1)))
 model.add(layers.Conv2D(32, kernel_size =5, padding= 'same', activation = 'relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Dropout(rate=0.4))
model.add(layers.Conv2D(64, kernel_size = 5, activation= 'relu'))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Dropout(rate=0.4))
model.add(layers.Conv2D(128,kernel_size=3,activation = 'relu'))
model.add(layers.Flatten())
 model.add(layers.Dense(256, activation = 'relu'))
model.add(layers.Dropout(rate=0.4))
model.add(layers.Dense(10,activation = 'softmax'))
model.summary()<choose_model_class>
train.isnull().sum()
model.compile(optimizer ='adam', loss='categorical_crossentropy',metrics = ['accuracy'] )<train_model>
train.title1st.value_counts()
epochs = 30
batch_size = 64
history = model.fit_generator(data_augment.flow(train_img, train_labels, batch_size = batch_size),epochs=epochs, steps_per_epoch=train_img.shape[0]//batch_size,validation_data=(dev_img, dev_labels))<predict_on_test>
train.Age=train.Age.apply(lambda x:1 if x<=10 else 2 if x<=25 else 3 if x<=45 else 4 if x<=60 else 5)
 test.Age=test.Age.apply(lambda x:1 if x<=10 else 2 if x<=25 else 3 if x<=45 else 4 if x<=60 else 5 )
pred_dig = model.predict(dig_img)
 pred_dig_labels = []
for i in range(len(dig_img)) :
pred_dig_label = np.argmax(pred_dig[i])
 pred_dig_labels.append(pred_dig_label )<feature_engineering>
train=train.loc[train.Fare<300]
result = pd.DataFrame(Y_test_1)
 result['Y_pred'] = pred_dig_labels
result['correct'] = result['label'] - result['Y_pred']
error = result[result['correct'] != 0]
error_list = error.index
print('Number of error is :', len(error))
print('The incidents are : ', error_list )<predict_on_test>
train.Cabin.value_counts()
predictions = model.predict(test_img)
 print(predictions.shape )<prepare_output>
train['Cabin']=train.Cabin.map(str ).apply(lambda x:x[0])
 test['Cabin']=test.Cabin.map(str ).apply(lambda x:x[0] )
predicted_labels = []
for i in range(len(predictions)) :
predicted_label = np.argmax(predictions[i])
 predicted_labels.append(predicted_label )<load_from_csv>
train.Cabin.replace({'n':np.nan},inplace=True)
 test.Cabin.replace({'n':np.nan},inplace=True )
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<save_to_csv>
train.Cabin.value_counts()
submission['label'] = predicted_labels
submission.to_csv('submission.csv', index=False)
 print(submission )<load_from_csv>
test.isnull().sum()
train_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv')
 print(train_df.shape, test_df.shape, dig_df.shape )<count_values>
test.isnull().sum()
train_df['label'].value_counts().sort_index()<count_values>
train.Cabin.fillna('U',inplace=True)
dig_df['label'].value_counts().sort_index()<prepare_x_and_y>
df=train[['Fare','Age','Cabin']].dropna()
df.head()
X = train_df.drop(['label'], axis=1 ).values
Y = train_df['label']
X_test = test_df.drop(['id'], axis=1 ).values
X_dig = dig_df.drop(['label'], axis=1 ).values
Y_dig = dig_df['label']
print(X.shape, Y.shape, X_test.shape)
 print(X_dig.shape, Y_dig.shape )<split>
X_train,X_test,y_train,y_test=train_test_split(df[['Fare','Age']],df.Cabin)
 knn=KNeighborsClassifier(n_neighbors=3 ).fit(X_train,y_train)
 knn.score(X_test,y_test )
X_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size=0.1 )<categorify>
test.Fare=test.Fare.fillna(train.Fare.median() )
n_x = 28
train_img = X_train.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 dev_img = X_dev.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 test_img = X_test.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 dig_img = X_dig.reshape(( -1, n_x, n_x, 1)).astype('float32')/255.
 print(train_img.shape, dev_img.shape, test_img.shape, dig_img.shape)
 train_labels = to_categorical(Y_train)
 dev_labels = to_categorical(Y_dev)
 dig_labels = to_categorical(Y_dig)
 print(train_labels.shape, dev_labels.shape, dig_labels.shape)
 print(Y_dig[8], dig_labels[8])
 plt.figure(figsize=(1,1))
plt.imshow(X_dig[8].reshape(( 28,28)) ,cmap=plt.cm.binary)
 plt.show()<set_options>
_=test[['Fare','Age','Cabin']]
_=_.loc[_.isnull().Cabin==True,['Fare','Age']]
data_augment = ImageDataGenerator(rotation_range=10, zoom_range=0.1,
width_shift_range=0.1, height_shift_range=0.1 )<choose_model_class>
y_pred=knn.predict(_ )
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=3, padding='same', activation='relu', input_shape=(28, 28, 1)))
 model.add(layers.Conv2D(32, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(32, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'))
model.add(layers.Conv2D(64, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(64, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'))
model.add(layers.Conv2D(128, kernel_size=3, padding='same', activation='relu'))
model.add(layers.BatchNormalization(momentum=0.15))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.Conv2D(128, kernel_size=5, padding='same', activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Flatten())
 model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.4))
model.add(layers.Dense(10, activation='softmax'))
model.summary()<choose_model_class>
test.isnull().sum()
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'] )<choose_model_class>
test.isnull().sum()
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',patience=3,factor=0.5,min_lr=0.00001,
verbose=1 )<train_model>
_=test.copy()
_=_.loc[_.isnull().Cabin==True]
_.head()
epochs = 75
batch_size = 64
history = model.fit_generator(data_augment.flow(train_img, train_labels, batch_size=batch_size),
epochs=epochs, steps_per_epoch=train_img.shape[0]//batch_size,
validation_data=(dev_img, dev_labels), callbacks=[learning_rate_reduction] )<compute_test_metric>
test.dropna(inplace=True)
 test=pd.concat(( test,_),axis=0 )
model.evaluate(dig_img, dig_labels )<predict_on_test>
test.sort_index(inplace=True )
pred_dig = model.predict(dig_img)
 dig_df['pred'] = np.argmax(pred_dig, axis=1 )<feature_engineering>
train.drop(droplist,axis=1,inplace=True)
 test.drop(droplist,axis=1,inplace=True )
dig_df['correct'] = dig_df['label'] - dig_df['pred']
errors = dig_df[dig_df['correct'] != 0]
error_list = errors.index
print('Number of errors is ', len(errors))
print('The indices are ', error_list )<predict_on_test>
cat_features=train.columns[train.dtypes==object]
cat_features
predictions = model.predict(test_img)
 print(predictions.shape )<prepare_output>
train.dropna(inplace=True )
predicted_labels = np.argmax(predictions, axis=1 )<save_to_csv>
le=LabelEncoder()
for col in cat_features:
le.fit(train[col])
 test[col]=le.transform(test[col].map(str))
train[col]=le.transform(train[col].map(str))
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predicted_labels
submission.to_csv('submission.csv', index=False )<import_modules>
test.drop('PassengerId',axis=1,inplace=True)
 train.drop('PassengerId',axis=1,inplace=True )
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization
from keras.optimizers import RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau<load_from_csv>
feature=train.drop(['Survived'],axis=1)
 label=train.Survived
feature.head()
train=pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test=pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv' )<drop_column>
dmatrix=xgb.DMatrix(feature,label)
 params={'objective':'binary:logistic','max_depth':5,'colsample_bytree':.6,'eta':.1,'alpha':1}
result=xgb.cv(params,dmatrix,early_stopping_rounds=10,as_pandas=True,num_boost_round=100,nfold=5,metrics='error')
 result
test.head(3)
 test=test.drop('id',axis=1)
<prepare_x_and_y>
xgb_cl=xgb.train(params,dmatrix,num_boost_round=19)
 xgb.plot_importance(xgb_cl )
X_train=train.drop('label',axis=1)
 Y_train=train.label<feature_engineering>
feature.index=list(range(len(feature)))
 label.index=list(range(len(label)) )
X_train=X_train/255
test=test/255<categorify>
skf=StratifiedKFold(n_splits=5,shuffle=True,random_state=123)
 def validate(estimator,feature,label):
score=[]
for trainind,testind in skf.split(feature,label):
X_train,y_train=feature.loc[trainind],label[trainind]
X_test,y_test=feature.loc[testind],label[testind]
estimator.fit(X_train,y_train)
 score.append(accuracy_score(y_test,estimator.predict(X_test)))
 return score
Y_train=to_categorical(Y_train )<split>
xgb_cl=xgb.XGBClassifier(max_depth=10,colsample_bytree=.6,learning_rate=.01,alpha=1,)
 validate(xgb_cl,feature,label )
X_train,X_test,y_train,y_test=train_test_split(X_train,Y_train,random_state=42,test_size=0.15 )<init_hyperparams>
rf=RandomForestClassifier(max_depth=3,n_estimators=500)
 validate(rf,feature,label )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train)
<choose_model_class>
knn=KNeighborsClassifier(n_neighbors=3)
 validate(knn,feature,label )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(BatchNormalization(momentum=.15))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.4))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
lr=LogisticRegression()
validate(lr,feature,label )
optimizer=Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.999 )<choose_model_class>
svm=SVC(C=15)
 validate(svm,feature,label )
model.compile(optimizer=optimizer,loss=['categorical_crossentropy'],metrics=['accuracy'] )<choose_model_class>
lgb=LGBMClassifier(max_depth=5,colsample_bytree=.6,num_leaves=30,subsample_for_bin=800)
 validate(lgb,feature,label )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
models=(['xgboost',xgb_cl],['lgb',lgb],['lr',lr])
 vc=VotingClassifier(estimators=models)
 validate(vc,feature,label )
epochs=5
batch_size=64<train_model>
xgb_cl.fit(feature,label )
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,y_test),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<predict_on_test>
y_pred=xgb_cl.predict(test )
y_pre_test=model.predict(X_test)
 y_pre_test=np.argmax(y_pre_test,axis=1)
 y_test=np.argmax(y_test,axis=1)
<compute_test_metric>
t=pd.read_csv('/kaggle/input/titanic/test.csv')
 submission=t[['PassengerId']]
submission['Survived']=y_pred
conf=confusion_matrix(y_test,y_pre_test)
 conf=pd.DataFrame(conf,index=range(0,10),columns=range(0,10)) 
<define_variables>
submission.to_csv('submission_new_features_lgb.csv',index=False )
<load_from_csv><EOS> 
submission.Survived.value_counts() ,label.value_counts()