Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<concatenate>
!pip install fastai==0.7.0
%load_ext autoreload
%autoreload 2
%matplotlib inline
df_final=pd.concat([df1, df2, df3, df4])
 df_final.head()<save_to_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 train_df.head()
df_final.to_csv('submission.csv',index=None )<import_modules>
train_cats(train_df)
 apply_cats(test_df, train_df )
import os
from pprint import pprint
from six import BytesIO
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageOps
from tqdm import tqdm<categorify>
df_trn, y_trn, nas = proc_df(train_df, 'Survived')
 df_test, _, _ = proc_df(test_df, na_dict=nas)
 df_trn.head()
def format_prediction_string(image_id, result):
prediction_strings = []
for i in range(len(result['detection_scores'])) :
class_name = result['detection_class_names'][i].decode("utf-8")
 YMin,XMin,YMax,XMax = result['detection_boxes'][i]
score = result['detection_scores'][i]
prediction_strings.append(
 f"{class_name} {score} {XMin} {YMin} {XMax} {YMax}" 
)
 prediction_string = " ".join(prediction_strings)
 return {
"ImageID": image_id,
"PredictionString": prediction_string
}<categorify>
df_trn.drop(['Age_na'], axis =1, inplace = True)
 df_test.drop(['Age_na', 'Fare_na'], axis =1, inplace = True)
 df_test.head()
sample_image_path = ".. /input/test/6beb79b52308112d.jpg"
with tf.Graph().as_default() :
image_string_placeholder = tf.placeholder(tf.string)
 decoded_image = tf.image.decode_jpeg(image_string_placeholder)
 decoded_image_float = tf.image.convert_image_dtype(
 image=decoded_image, dtype=tf.float32 
)
 image_tensor = tf.expand_dims(decoded_image_float, 0)
 model_url = "https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1"
detector = hub.Module(model_url)
 detector_output = detector(image_tensor, as_dict=True)
 init_ops = [tf.global_variables_initializer() , tf.tables_initializer() ]
sess = tf.Session()
sess.run(init_ops)
 with tf.gfile.Open(sample_image_path, "rb")as binfile:
image_string = binfile.read()
result_out, image_out = sess.run(
 [detector_output, decoded_image],
feed_dict={image_string_placeholder: image_string}
)<categorify>
def rmse(x,y): return math.sqrt(((x-y)**2 ).mean())
 def print_score(m):
res = [rmse(m.predict(train_X), train_y), rmse(m.predict(val_X), val_y),
m.score(train_X, train_y), m.score(val_X, val_y)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
print(image_string_placeholder)
 print(decoded_image)
 print(decoded_image_float)
 print(image_tensor )<load_from_csv>
train_X, val_X, train_y, val_y = train_test_split(df_trn, y_trn, test_size=0.33, random_state=42 )
sample_submission_df = pd.read_csv('.. /input/sample_submission.csv')
 image_ids = sample_submission_df['ImageId']
predictions = []
for image_id in tqdm(image_ids):
image_path = f'.. /input/test/{image_id}.jpg'
with tf.gfile.Open(image_path, "rb")as binfile:
image_string = binfile.read()
result_out = sess.run(
 detector_output,
feed_dict={image_string_placeholder: image_string} 
)
 predictions.append(format_prediction_string(image_id, result_out))
sess.close()<prepare_output>
%time
m = RandomForestClassifier(n_estimators=1, min_samples_leaf=10, n_jobs=-1, max_depth = 3, oob_score=True)
 m.fit(train_X, train_y)
 print_score(m )
pred_df = pd.DataFrame(predictions)
 pred_df.head()<save_to_csv>
%time
m = RandomForestClassifier(n_estimators=20, min_samples_leaf=10, max_features=0.7, n_jobs=-1, oob_score=True)
 m.fit(train_X, train_y)
 print_score(m )
pred_df.to_csv('submission.csv', index=False )<load_pretrained>
perm = PermutationImportance(m, random_state=1 ).fit(val_X, val_y)
 eli5.show_weights(perm, feature_names = val_X.columns.tolist() )
with zipfile.ZipFile(".. /input/aerial-cactus-identification/train.zip","r")as z:
z.extractall("/kaggle/temp/")
 with zipfile.ZipFile(".. /input/aerial-cactus-identification/test.zip","r")as z:
z.extractall("/kaggle/temp/test/")
 print(len(os.listdir(".. /temp/train")))
 print(len(os.listdir(".. /temp/test/test")) )<load_from_csv>
%time
df_trn.drop(['Embarked', 'Fare', 'Cabin', 'Parch'], axis =1, inplace = True)
 df_test.drop(['Embarked', 'Fare', 'Cabin', 'Parch'], axis =1, inplace = True)
 train_X, val_X, train_y, val_y = train_test_split(df_trn, y_trn, test_size=0.33, random_state=42)
 m = RandomForestClassifier(n_estimators=20, min_samples_leaf=10, max_features=0.7, n_jobs=-1, oob_score=True)
 m.fit(train_X, train_y)
 print_score(m )
train_dir = ".. /temp/train"
test_dir = ".. /temp/test"
labels = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 labels.has_cactus = labels.has_cactus.astype(str)
 print(labels['has_cactus'].value_counts() )<define_variables>
submission['Survived'] = pred
submission.to_csv('rf_submission_v2.csv', index=False )