Kaggle competition: Titanic - Machine Learning from Disaster
learn = cnn_learner(dls, resnet50, metrics=[error_rate, accuracy] ).to_native_fp16()<set_options>
print('reading input files.. ')
 data = pd.read_csv('.. /input/train.csv')
 sampl = pd.read_csv('.. /input/gender_submission.csv' )
if train:
learn.fine_tune(10, cbs=[MixUp(0.5)] )<load_pretrained>
test = pd.read_csv('.. /input/test.csv' )
model_name = "resnet50-fine_tune-10_resize-512"
if train:
learn.export(Path(f"{Path(os.getcwd() ).parent}/output/cassava-leaf-disease-classification/{model_name}.pkl"))
else:
learn = load_learner(Path(f"{Path(os.getcwd() ).parent}/input/cassava-modelresnet34fine-tune10pkl/{model_name}.pkl"), cpu=False)
 learn = learn.to_native_fp32()
learn<import_modules>
df = data.append(test, sort = False )
if train:
cleaner = ImageClassifierCleaner(learn)
 cleaner<load_from_csv>
ticketNum = pd.DataFrame(df.Ticket.value_counts())
 ticketNum.rename(columns = {'Ticket' : 'TicketNum'}, inplace = True)
 ticketNum['TicketId'] = pd.Categorical(ticketNum.index ).codes
ticketNum.loc[ticketNum.TicketNum < 3, 'TicketId'] = -1
df = pd.merge(left = df, right = ticketNum, left_on = 'Ticket',
right_index = True, how = 'left', sort = False)
 df = df.drop(['TicketNum'],axis=1)
 df.head()
test_labels = pd.read_csv(path / 'sample_submission.csv')
 test_labels["image_id"] = test_labels["image_id"].apply(
 lambda x: f'test_images/{x}')
 test_labels.head()<predict_on_test>
df['FamilyName'] = df.Name.apply(lambda x : str.split(x, ',')[0] )
test_predictions = []
for i, r in test_labels.iterrows() :
test_predictions.append(int(learn.predict(f'.. /input/cassava-leaf-disease-classification/{r.image_id}')[0]))
test_predictions[0]<save_to_csv>
df['FamilySurv'] = 0.5
for _, grup in df.groupby(['FamilyName','Fare']):
if len(grup)!= 1:
for index, row in grup.iterrows() :
smax = grup.drop(index ).Survived.max()
smin = grup.drop(index ).Survived.min()
pid = row.PassengerId
if smax == 1:
df.loc[df.PassengerId == pid, 'FamilySurv'] = 1.0
elif smin == 0:
df.loc[df.PassengerId == pid, 'FamilySurv'] = 0.0
for _, grup in df.groupby(['Ticket']):
if len(grup)!= 1:
for index, row in grup.iterrows() :
if(row.FamilySurv == 0.0 or row.FamilySurv == 0.5):
smax = grup.drop(index ).Survived.max()
smin = grup.drop(index ).Survived.min()
pid = row.PassengerId
if smax == 1:
df.loc[df.PassengerId == pid, 'FamilySurv'] = 1.0
elif smin == 0:
df.loc[df.PassengerId == pid, 'FamilySurv'] = 0.0
df.FamilySurv.value_counts()
submission = pd.read_csv(path / 'sample_submission.csv')
 submission['label'] = test_predictions
submission.head()
submission.to_csv('submission.csv',index=False )<import_modules>
def CabinNum(data):
data.Cabin = data.Cabin.fillna('0')
 regex = re.compile('\s*(\w+)\s*')
 data['CabinNum'] = data.Cabin.apply(lambda x : len(regex.findall(x)))
 CabinNum(df )
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os<load_from_csv>
df.CabinNum.value_counts()
train_input = ".. /input/digit-recognizer/train.csv"
test_input = ".. /input/digit-recognizer/test.csv"
train_dataset = pd.read_csv(train_input)
 test_dataset = pd.read_csv(test_input )<categorify>
df.loc[df['Fare'].isnull() ]
train_labels = tf.keras.utils.to_categorical(train_dataset.pop("label"))<prepare_x_and_y>
df.loc[(df['Age'] >= 60)&(df['Pclass'] ==3)&(df['Sex'] == 'male')&(df['Embarked'] =='S')]
train_dataset = np.array(train_dataset.values.reshape(-1, 28, 28, 1))
test_dataset = np.array(test_dataset.values.reshape(-1, 28, 28, 1))<feature_engineering>
def FareFunc(data):
data.loc[data['Fare'].isnull() , 'Fare'] = 7
data['FareCat'] = 0
data.loc[data['Fare'] < 8, 'FareCat'] = 0
data.loc[(data['Fare'] >= 8)&(data['Fare'] < 16),'FareCat' ] = 1
data.loc[(data['Fare'] >= 16)&(data['Fare'] < 30),'FareCat' ] = 2
data.loc[(data['Fare'] >= 30)&(data['Fare'] < 45),'FareCat' ] = 3
data.loc[(data['Fare'] >= 45)&(data['Fare'] < 80),'FareCat' ] = 4
data.loc[(data['Fare'] >= 80)&(data['Fare'] < 160),'FareCat' ] = 5
data.loc[(data['Fare'] >= 160)&(data['Fare'] < 270),'FareCat' ] = 6
data.loc[(data['Fare'] >= 270), 'FareCat'] = 7
FareFunc(df )
train_dataset = train_dataset / 255.0
test_dataset = test_dataset / 255.0<define_variables>
def FamlSize(data):
data['FamlSize'] = 0
data['FamlSize'] = data['SibSp'] + data['Parch'] + 1
def IsAlone(data):
data['IsAlone'] = 0
data.loc[(data['FamlSize'] == 1), 'IsAlone'] = 0
data.loc[(data['FamlSize'] > 1), 'IsAlone'] = 1
FamlSize(df)
 IsAlone(df )
checkpoint_path = "logs/checkpoints/"<choose_model_class>
def LablFunc(data):
lsr = {'Title','Cabin'}
for i in lsr:
le.fit(data[i].astype(str))
data[i] = le.transform(data[i].astype(str))
LablFunc(df )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), input_shape=(28, 28, 1), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="softmax")
 ])
 epochs=50
callbacks = [
tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
monitor="accuracy",
save_best_only=True,
save_weights_only=True),
tf.keras.callbacks.EarlyStopping(monitor="accuracy", patience=15)
 ]
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 history = model.fit(train_dataset, train_labels, epochs=epochs, callbacks=callbacks, batch_size=64 )<load_pretrained>
features = ['Pclass','SibSp','Parch','TicketId','Fare','CabinNum','Title']
def AgeFunc(df):
Etr = ETRg(n_estimators = 200, random_state = 2)
 AgeX_Train = df[features][df.Age.notnull() ]
AgeY_Train = df['Age'][df.Age.notnull() ]
AgeX_Test = df[features][df.Age.isnull() ]
Etr.fit(AgeX_Train,np.ravel(AgeY_Train))
AgePred = Etr.predict(AgeX_Test)
 df.loc[df.Age.isnull() , 'Age'] = AgePred
AgeFunc(df )
model.load_weights(checkpoint_path )<prepare_output>
def AgeCat(data):
data['AgeCat'] = 0
data.loc[(data['Age'] <= 5), 'AgeCat'] = 0
data.loc[(data['Age'] <= 12)&(data['Age'] > 5), 'AgeCat'] = 1
data.loc[(data['Age'] <= 18)&(data['Age'] > 12), 'AgeCat'] = 2
data.loc[(data['Age'] <= 22)&(data['Age'] > 18), 'AgeCat'] = 3
data.loc[(data['Age'] <= 32)&(data['Age'] > 22), 'AgeCat'] = 4
data.loc[(data['Age'] <= 45)&(data['Age'] > 32), 'AgeCat'] = 5
data.loc[(data['Age'] <= 60)&(data['Age'] > 45), 'AgeCat'] = 6
data.loc[(data['Age'] <= 70)&(data['Age'] > 60), 'AgeCat'] = 7
data.loc[(data['Age'] > 70), 'AgeCat'] = 8
AgeCat(df )
labels = [np.argmax(predict)for predict in model.predict(test_dataset)]
df = pd.DataFrame({
"ImageId": list(range(1, len(test_dataset)+1)) ,
"Label": labels,
} )<save_to_csv>
def AgeCatTitle(data):
data['AgeCatTitle'] = data['Title'].map(str)+ data['AgeCat'].map(str)
df.to_csv("submission.csv", index=False )<save_model>
df.loc[df['Embarked'].isnull() ]
model.save("model.h5" )<set_options>
def FillEmbk(data):
var = 'Embarked'
data.loc[(data.Embarked.isnull()),'Embarked']= 'C'
FillEmbk(df )
plt.style.use('seaborn-white')
 sns.set_style("white")
 %matplotlib inline
t_start = time.time()<define_variables>
def LablFunc(data):
lst = {'Embarked','Sex'}
for i in lst:
le.fit(data[i].astype(str))
data[i] = le.transform(data[i].astype(str))
LablFunc(df )
version = 5
basic_name = f'Unet_resnet_v{version}'
save_model_name = basic_name + '.model'
submission_file = basic_name + '.csv'
print(save_model_name)
 print(submission_file )<train_model>
target = data['Survived'].values
select_features = ['Pclass', 'Age','AgeCat','SibSp', 'Parch', 'Fare',
'Embarked', 'TicketId', 'CabinNum', 'Title','Cabin',
'FareCat', 'FamlSize','FamilySurv','Sex']
scaler = StandardScaler()
dfScaled = scaler.fit_transform(df[select_features])
 train = dfScaled[0:891].copy()
test = dfScaled[891:].copy()
img_size_ori = 101
img_size_target = 101
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True )<load_from_csv>
selector = SelectKBest(f_classif, len(select_features))
selector.fit(train, target)
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Features importance:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], select_features[indices[i]]))
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
len(train_df )<feature_engineering>
numpy.random.seed(7)
 model = models.Sequential()
model.add(Dense(30,input_dim=15,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(15,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(5,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
 model.fit(train,target,epochs=150, batch_size=20, verbose=1 )
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<feature_engineering>
prc = model.predict(train)
 prc = [round(x[0])for x in prc]
accuracy_score(target,prc )
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<categorify>
snum = 0
enum = len(test)
 prdt2 = model.predict(test)
 prdt2 = [round(x[0])for x in prdt2]
prdt2 = list(map(int,prdt2))
print('Predicted result: ', prdt2 )
<split><EOS> 
sampl['Survived'] = pd.DataFrame(prdt2)
 sampl.to_csv('submission.csv', index=False )