Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<define_variables>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 gender_submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 data = pd.concat([train, test], sort=False)
 data['Sex'].replace(['male', 'female'], [0, 1], inplace=True)
 data['Embarked'].fillna(( 'S'), inplace=True)
 data['Embarked'] = data['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 data['Fare'].fillna(np.mean(data['Fare']), inplace=True)
 data['Age'].fillna(data['Age'].median() , inplace=True)
 data['FamilySize'] = data['Parch'] + data['SibSp'] + 1
data['IsAlone'] = 0
data.loc[data['FamilySize'] == 1, 'IsAlone'] = 1
!rm -rf /kaggle/working/train_images_preprocessed/
print("Preprocessing test images...")
 !mkdir -p 'test_images_preprocessed'
for i, image_id in enumerate(tqdm(test_df['id_code'])) :
image = preprocess_image(f'.. /input/aptos2019-blindness-detection/test_images/{image_id}')
 cv2.imwrite(f'./test_images_preprocessed/{image_id}', image)
 test_datagen = ImageDataGenerator(rescale = 1./255)
 test_generator = test_datagen.flow_from_dataframe(
 dataframe = test_df,
directory = TEST_DATA_ROOT,
x_col = 'id_code',
target_size =(IMAGE_HEIGHT, IMAGE_WIDTH),
batch_size = 1,
shuffle = False,
class_mode = None)
 predict = model.predict_generator(test_generator, steps = len(test_generator.filenames))
results=pd.DataFrame({'id_code':test_generator.filenames,
'diagnosis':np.argmax(predict, axis = 1)})
 results['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])
 results.to_csv('submission.csv', index = False )<feature_engineering>
delete_columns = ['Name', 'PassengerId', 'Ticket', 'Cabin']
data.drop(delete_columns, axis=1, inplace=True)
 train = data[:len(train)]
test = data[len(train):]
y_train = train['Survived']
X_train = train.drop('Survived', axis=1)
 X_test = test.drop('Survived', axis=1 )
t_start = time.time()<set_options>
clf = LogisticRegression(penalty='l2', solver='sag', random_state=0 )
%reload_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0 )
from fastai.vision import *
from fastai.metrics import error_rate
from fastai.callbacks import *<import_modules>
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_test )
from functools import partial
from sklearn import metrics
from collections import Counter<import_modules>
sub = pd.read_csv('.. /input/titanic/gender_submission.csv' )
import os
import PIL
import cv2<define_variables>
sub['Survived'] = list(map(int, y_pred))
sub.to_csv('submission_randomforest.csv', index=False)
 sub.head()
dat_path = Path('.. /input/aptos2019-blindness-detection')
 trn_path = Path('.. /input/aptos2019-blindness-detection/train_images')
(dat_path,trn_path )<load_from_csv>
X_train, X_valid, y_train, y_valid = \
train_test_split(X_train, y_train, test_size=0.3,
random_state=0, stratify=y_train )
train_df = pd.read_csv(dat_path/'train.csv')
 train_df.shape<train_on_grid>
categorical_features = ['Embarked', 'Pclass', 'Sex']
IMG_SIZE = 512
def _load_format(path, convert_mode, after_open)->Image:
image = cv2.imread(path)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = crop_image_from_gray(image)
 image = cv2.resize(image,(IMG_SIZE, IMG_SIZE))
image=cv2.addWeighted(image,4, cv2.GaussianBlur(image ,(0,0), 10),-4 ,128)
 return Image(pil2tensor(image, np.float32 ).div_(255))
vision.data.open_image = _load_format<feature_engineering>
lgb_train = lgb.Dataset(X_train, y_train,
categorical_feature=categorical_features)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train,
categorical_feature=categorical_features)
 params = {
'objective': 'binary'
}
model = lgb.train(params, lgb_train,
valid_sets=[lgb_train, lgb_eval],
verbose_eval=10,
num_boost_round=1000,
early_stopping_rounds=10)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration )
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=0.10, max_zoom=1.3, max_warp=0.0, max_lighting=0.2 )<define_variables>
y_pred =(y_pred > 0.5 ).astype(int)
 y_pred[:10]
<create_dataframe><EOS> 
sub['Survived'] = y_pred
sub.to_csv('submission_lightgbm.csv', index=False)
 sub.head()