Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
!pip install fastai --upgrade
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
 x_val = x_val.reshape(x_val.shape[0], 28, 28,1)
 x_test = x_test.reshape(x_test.shape[0], 28, 28,1)
 print("Train size:{}
validation size:{}
Test size:{}".format(x_train.shape,x_val.shape, x_test.shape))
mean_px = x_train.mean().astype(np.float32)
 std_px = x_train.std().astype(np.float32)
<choose_model_class>
from fastai import *
from fastai.tabular import *
input = Input(shape=[28, 28, 1])
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv1' )(input)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch1' )(x)
 x = Activation('relu', name='relu1' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2' )(x)
 x = Activation('relu', name='relu2' )(x)
 x = Conv2D(32,(5, 5), strides=1, padding='same', name='conv2add' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch2add' )(x)
 x = Activation('relu', name='relu2add' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2, padding='same' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv3' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch3' )(x)
 x = Activation('relu', name='relu3' )(x)
 x = Conv2D(64,(3, 3), strides=1, padding='same', name='conv4' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch4' )(x)
 x = Activation('relu', name='relu4' )(x)
 x = Conv2D(32,(3, 3), strides=1, padding='same', name='conv5' )(x)
 x = BatchNormalization(momentum=0.1, epsilon=1e-5, gamma_initializer='uniform',name='batch5' )(x)
 x = Activation('relu', name='relu5' )(x)
 x = Dropout(0.15 )(x)
 x = MaxPool2D(pool_size=2, strides=2 )(x)
 x = Flatten()(x)
 x = Dense(100, name='Dense30' )(x)
 x = Activation('relu', name='relu6' )(x)
 x = Dropout(0.05 )(x)
 x = Dense(10, name='Dense10' )(x)
 x = Activation('softmax' )(x)
 model = Model(inputs = input, outputs =x)
 print(model.summary() )<init_hyperparams>
input_path = '/kaggle/input/'
train_df = pd.read_csv(f'{input_path}train.csv')
 test_df = pd.read_csv(f'{input_path}test.csv' )
checkpoint = ModelCheckpoint("best_weights.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
 datagen = ImageDataGenerator(
 rotation_range= 8,
zoom_range = 0.13,
width_shift_range=0.13,
height_shift_range=0.13)
 epochs = 70
lr_initial = 0.0011
optimizer = Adam(lr=lr_initial, decay= lr_initial /(epochs*1.3))
model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 datagen.fit(x_train)
 batch_size = 64
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(x_val,y_val),
verbose = 2, steps_per_epoch=x_train.shape[0] // batch_size, callbacks=[checkpoint])
 model.load_weights("best_weights.hdf5")
<load_from_csv>
for df in [train_df, test_df]:
df['Title'] = df['Name'].str.split(',' ).str[1].str.split(' ' ).str[1]
df['Deck'] = df['Cabin'].str[0]
all_df = pd.concat([train_df, test_df], sort=False)
 mean_age_by_title = all_df.groupby('Title' ).mean() ['Age']
for df in [train_df, test_df]:
for title, age in mean_age_by_title.iteritems() :
df.loc[df['Age'].isnull() &(df['Title'] == title), 'Age'] = age
test1 = pd.read_csv('.. /input/Kannada-MNIST/test.csv' ).values
print(test1)
 x_samples = test1[0:,1:]
id_samples = test1[0:,0]<save_to_csv>
test_df.Fare.fillna(0,inplace=True )
results = model.predict(x_test)
 results = np.argmax(results,axis = 1)
 dg = pd.DataFrame()
dg['id'] = list(id_samples)
 dg['label'] = results
dg.to_csv('submission.csv',index = False )<set_options>
dep_var = 'Survived'
cat_names = ['Pclass', 'Sex', 'Embarked', 'Title', 'Deck']
cont_names = ['Age', 'Fare', 'SibSp', 'Parch']
procs = [FillMissing, Categorify, Normalize]
test = TabularList.from_df(test_df, cat_names=cat_names, cont_names=cont_names, procs=procs)
 data =(TabularList.from_df(train_df, path='.', cat_names=cat_names, cont_names=cont_names, procs=procs)
.split_by_idx(list(range(0,200)))
.label_from_df(cols=dep_var)
.add_test(test, label=0)
.databunch() )
warnings.simplefilter("ignore" )<define_variables>
np.random.seed(101 )
BASE_PATH = '/kaggle/input/liverpool-ion-switching/'
MODEL_PATH = '/kaggle/input/ensemble-models/'
ROW_PER_BATCH = 500000
WINDOW_SIZES = [10, 50]
RANDOM_STATE = 42<feature_engineering>
learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
 learn.fit(15, 1e-1)
 learn.fit(15, 1e-2)
def add_test_batch_rem_drift(test):
test['batch'] = 0
for i in range(0, test.shape[0]//ROW_PER_BATCH):
test.iloc[i * ROW_PER_BATCH:(i+1)* ROW_PER_BATCH,2] = i
test['signal_undrifted'] = test.signal
start=500
a = 0; b = 100000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 start=510
a = 100000; b = 200000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 start=540
a = 400000; b = 500000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 start=560
a = 600000; b = 700000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 start=570
a = 700000; b = 800000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 start=580
a = 800000; b = 900000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - 3*(test.time.values[a:b]-start)/10.
 def f(x):
return -(0.00788)*(x-625)**2+2.345 +2.58
a = 1000000; b = 1500000
test.loc[test.index[a:b],'signal_undrifted'] = test.signal.values[a:b] - f(test.time[a:b].values)
 return test<feature_engineering>
learn.fit(1, 1e-3 )
def create_advance_features(df, window_sizes):
for window in window_sizes:
df["rolling_mean_" + str(window)] = df['signal_undrifted'].rolling(window=window ).mean()
df["rolling_std_" + str(window)] = df['signal_undrifted'].rolling(window=window ).std()
df["rolling_var_" + str(window)] = df['signal_undrifted'].rolling(window=window ).var()
df["rolling_min_" + str(window)] = df['signal_undrifted'].rolling(window=window ).min()
df["rolling_max_" + str(window)] = df['signal_undrifted'].rolling(window=window ).max()
df["rolling_min_max_ratio_" + str(window)] = df["rolling_min_" + str(window)] / df["rolling_max_" + str(window)]
df["rolling_min_max_diff_" + str(window)] = df["rolling_max_" + str(window)] - df["rolling_min_" + str(window)]
a =(df['signal'] - df['rolling_min_' + str(window)])/(df['rolling_max_' + str(window)] - df['rolling_min_' + str(window)])
 df["norm_" + str(window)] = a *(np.floor(df['rolling_max_' + str(window)])- np.ceil(df['rolling_min_' + str(window)]))
df = df.replace([np.inf, -np.inf], np.nan)
 df.fillna(0, inplace=True)
 return df
def features(df):
df = df.sort_values(by=['time'] ).reset_index(drop=True)
 df.index =(( df.time * 10_000)- 1 ).values
df['batch'] = df.index // 50_000
df['batch_index'] = df.index -(df.batch * 50_000)
 df['batch_slices'] = df['batch_index'] // 5_000
df['batch_slices2'] = df.apply(lambda r: '_'.join([str(r['batch'] ).zfill(3), str(r['batch_slices'] ).zfill(3)]), axis=1)
 for c in ['batch','batch_slices2']:
d = {}
d['mean'+c] = df.groupby([c])['signal_undrifted'].mean()
d['median'+c] = df.groupby([c])['signal_undrifted'].median()
d['max'+c] = df.groupby([c])['signal_undrifted'].max()
d['min'+c] = df.groupby([c])['signal_undrifted'].min()
d['std'+c] = df.groupby([c])['signal_undrifted'].std()
d['mean_abs_chg'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.mean(np.abs(np.diff(x))))
d['abs_max'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.max(np.abs(x)))
 d['abs_min'+c] = df.groupby([c])['signal_undrifted'].apply(lambda x: np.min(np.abs(x)))
 for v in d:
df[v] = df[c].map(d[v].to_dict())
 df['range'+c] = df['max'+c] - df['min'+c]
df['maxtomin'+c] = df['max'+c] / df['min'+c]
df['abs_avg'+c] =(df['abs_min'+c] + df['abs_max'+c])/ 2
df['signal_shift_+1'] = [0,] + list(df['signal_undrifted'].values[:-1])
 df['signal_shift_-1'] = list(df['signal_undrifted'].values[1:])+ [0]
for i in df[df['batch_index']==0].index:
df['signal_shift_+1'][i] = np.nan
for i in df[df['batch_index']==49999].index:
df['signal_shift_-1'][i] = np.nan
for c in [c1 for c1 in df.columns if c1 not in ['time', 'signal_undrifted', 'open_channels', 'batch', 'batch_index', 'batch_slices', 'batch_slices2']]:
df[c+'_msignal'] = df[c] - df['signal_undrifted']
return df<load_from_csv>
preds, targets = learn.get_preds()
predictions = np.argmax(preds, axis = 1)
 pd.crosstab(predictions, targets )
test = pd.read_csv(BASE_PATH + 'test.csv')
 test = add_test_batch_rem_drift(test)
 test = create_advance_features(test, WINDOW_SIZES)
 test = features(test )<drop_column>
predictions, *_ = learn.get_preds(DatasetType.Test)
 labels = np.argmax(predictions, 1 )
<load_pretrained><EOS> 
sub_df = pd.DataFrame({'PassengerId': test_df['PassengerId'], 'Survived': labels})
 sub_df.to_csv('submission.csv', index=False )