Kaggle competition: Titanic - Machine Learning from Disaster
test['clicks_by_ip'] = test['clicks_by_ip'].astype('uint16')
 test = datatimeFeatures(test)
 test.drop(['click_id', 'ip'], axis=1, inplace=True)
 dtest = xgb.DMatrix(test)
 del test
gc.collect()<save_to_csv>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 train_original = train_df.copy()
test_original = test_df.copy()
train_df.head()
sub['is_attributed'] = model.predict(dtest, ntree_limit=model.best_ntree_limit)
 sub.to_csv('improved_xgb_sub_today.csv',float_format='%.8f',index=False)
 print('submission is done in [{}] seconds'.format(time.time() - start_time))<set_options>
train_df['Cabin'].value_counts().head()
%matplotlib inline 
<load_from_csv>
test_df['Cabin'].value_counts().head()
train_col = ['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed']
dtypes = {
'ip' : 'uint32',
'app' : 'uint16',
'device' : 'uint16',
'os' : 'uint16',
'channel' : 'uint16',
'is_attributed' : 'uint8',
'click_id' : 'uint32'
}
df = pd.read_csv('.. /input/train.csv', nrows=30000000,
usecols=train_col, dtype=dtypes)
 df.head()<data_type_conversions>
train_df['Survived'].value_counts()
df['hour'] = pd.to_datetime(df.click_time ).dt.hour.astype('uint8')
 df['minute'] = pd.to_datetime(df.click_time ).dt.minute.astype('uint8')
 df['day'] = pd.to_datetime(df.click_time ).dt.day.astype('uint8')
 df['dw'] = pd.to_datetime(df.click_time ).dt.dayofweek.astype('uint8')
 df_test = pd.read_csv('.. /input/test.csv')
 click_id = df_test['click_id']
df_test['hour'] = pd.to_datetime(df_test.click_time ).dt.hour.astype('uint8')
 df_test['minute'] = pd.to_datetime(df_test.click_time ).dt.minute.astype('uint8')
 df_test['day'] = pd.to_datetime(df_test.click_time ).dt.day.astype('uint8')
 df_test['dw'] = pd.to_datetime(df_test.click_time ).dt.dayofweek.astype('uint8' )<merge>
train_df['Survived'].value_counts(normalize = True)
X = pd.concat(( df[['ip', 'app', 'device', 'hour', 'minute', 'os', 'channel', 'day', 'dw']],
df_test[['ip', 'app', 'device', 'hour', 'minute', 'os', 'channel', 'day', 'dw']]))
del df_test; gc.collect()
group = X[['ip','day','hour', 'minute','channel']].groupby(by=['ip','day','hour', 'minute'])['channel'].count().\
reset_index().rename(index=str, columns={'channel': 'ip_day_time'})
 X = X.merge(group, on=['ip','day','hour', 'minute'], how='left')
 del group; gc.collect()
group = X[['ip', 'app', 'os', 'channel']].groupby(by=['ip', 'app', 'os'])['channel'].count().\
reset_index().rename(index=str, columns={'channel': 'ip_app_os'})
 X = X.merge(group, on=['ip', 'app', 'os'], how='left')
 del group; gc.collect()
X[['app','device','os', 'channel', 'hour', 'minute', 'day', 'dw']].apply(LabelEncoder().fit_transform)
 len(X )<define_variables>
def delete_features(df):
return df.drop(['PassengerId','Ticket','Cabin'], axis=1)
 def fill_value(df):
df.Embarked = df.Embarked.fillna("S")
 df['Age'] = df.groupby(['Sex'],sort=False)['Age'].apply(lambda x: x.fillna(x.median()))
df['Fare'] = df.groupby(['Pclass','Embarked'],sort=False)['Fare'].apply(lambda x : x.fillna(x.median()))
return df
def fill_cabin(df):
df.Cabin = df['Cabin'].fillna("N")

df.Cabin = df['Cabin'].apply(lambda c : c[0])
 return df
def extract_name_from_title(df):
df['Name'] = df['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 return df
def family_members(df):
df['Family'] = df['SibSp'] + df['Parch'] + 1
return df
def family_size_bin(df):

 df.loc[ df['Family'] == 1, 'Family'] = "Individual"
df['Family'].replace(to_replace = [2,3,4], value = 'small', inplace = True)
 df['Family'].replace(to_replace = [5,6], value = 'medium', inplace = True)
 df['Family'].replace(to_replace = [7,8,9, 10,11], value = 'large', inplace = True)
 return df
def transform_feature(df):
df = delete_features(df)
 df = fill_value(df)
 df = extract_name_from_title(df)
 df = family_members(df)
 df = family_size_bin(df)
 return df
train_df = transform_feature(train_df)
 test_df = transform_feature(test_df)
 train_df.head()
max_app = np.max(X.app)+1
max_device = np.max(X.device)+1
max_hour = np.max(X.hour)+1
max_minute = np.max(X.minute)+1
max_os = np.max(X.os)+1
max_channel = np.max(X.channel)+1
max_day = np.max(X.day)+1
max_dw = np.max(X.dw)+1
max_ip_day_time = np.max(X.ip_day_time)+1
max_ip_app_os = np.max(X.ip_app_os)+1<split>
display(train_df['Name'].value_counts() )
X_test = X[len(df):]
X = X[:len(df)]
y = df.is_attributed
X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=42, train_size=0.95 )<count_values>

 def replace_title(df):
df['Name'] = df['Name'].replace(['Lady','Countess','Capt', 'Col','Don', 'Major','Rev','Sir','Jonkheer','Dona'], 'Special')
 df['Name'] = df["Name"].replace(['Mlle','Ms','Miss'],'Miss')
 df['Name'] = df['Name'].replace(['Mrs','Mme'],'Mrs')
 return df
train_df = replace_title(train_df)
 test_df = replace_title(test_df)
 train_df.head()
print(np.sum(y_train)/ len(y_train))
print(np.sum(y_valid)/ len(y_valid))<prepare_x_and_y>
display(train_df['Name'].value_counts() )
del df; gc.collect()
def get_keras_data(data):
X = {
'app': np.array(data.app),
'device': np.array(data.device),
'hour': np.array(data.hour),
'minute': np.array(data.minute),
'os': np.array(data.os),
'channel': np.array(data.channel),
'day': np.array(data.day),
'dw': np.array(data.dw),
'ip_day_time': np.array(data.ip_day_time),
'ip_app_os': np.array(data.ip_app_os)
 }
return X
X_train = get_keras_data(X_train)
 X_valid = get_keras_data(X_valid)
 X_test = get_keras_data(X_test )<categorify>
def encoder(df):
scaler = MinMaxScaler()
numerical = ['Age', 'Fare', 'SibSp','Parch']
features_transform = pd.DataFrame(data= df)
 features_transform[numerical] = scaler.fit_transform(df[numerical])
 display(features_transform.head(n = 5))
return df
train_df = encoder(train_df)
 test_df = encoder(test_df)
def get_model() :
app = Input(shape=[1], name='app')
 device = Input(shape=[1], name='device')
 hour = Input(shape=[1], name='hour')
 minute = Input(shape=[1], name='minute')
 os = Input(shape=[1], name='os')
 channel = Input(shape=[1], name='channel')
 day = Input(shape=[1], name='day')
 dw = Input(shape=[1], name='dw')
 ip_day_time = Input(shape=[1], name='ip_day_time')
 ip_app_os = Input(shape=[1], name='ip_app_os')
 emb_app = Embedding(max_app, 30 )(app)
 emb_device = Embedding(max_device, 30 )(device)
 emb_hour = Embedding(max_hour, 30 )(hour)
 emb_minute = Embedding(max_minute, 30 )(minute)
 emb_os = Embedding(max_os, 30 )(os)
 emb_channel = Embedding(max_channel, 30 )(channel)
 emb_day = Embedding(max_day, 30 )(day)
 emb_dw = Embedding(max_dw, 30 )(dw)
 emb_ip_day_time = Embedding(max_ip_day_time, 30 )(ip_day_time)
 emb_ip_app_os = Embedding(max_ip_app_os, 30 )(ip_app_os)
 main_l = concatenate([Flatten()(emb_app), Flatten()(emb_hour), Flatten()(emb_minute),
Flatten()(emb_os), Flatten()(emb_device),
Flatten()(emb_day), Flatten()(emb_dw), Flatten()(emb_channel),
Flatten()(emb_ip_day_time), Flatten()(emb_ip_app_os)])
 main_l = Dropout(0.1 )(Dense(1000, activation='relu' )(main_l))
main_l = Dropout(0.1 )(Dense(1000, activation='relu' )(main_l))
output = Dense(1, activation='sigmoid' )(main_l)
 model = Model([app, device, hour, minute, os, channel, day, dw, ip_day_time, ip_app_os], output)
 model.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer='adam')
 return model
model = get_model()
model.summary() 
<train_model>
def convert_numerical(df):
df = pd.get_dummies(df)
 encoded = list(df.columns)
 print("{} total features after one-hot encoding.".format(len(encoded)))
 print(encoded)
 return df
train_df_final = convert_numerical(train_df)
 test_df_final = convert_numerical(test_df)
batch_size = 20000
epochs = 1
model.fit(X_train, np.array(y_train), epochs=epochs, batch_size=batch_size, verbose=1 )<predict_on_test>
ytest = train_df_final['Survived']
xtrain = train_df_final.drop(['Survived'], axis = 1)
 X_train, X_test, y_train, y_test = train_test_split(xtrain, ytest, test_size=.25, random_state=1 )
pred = model.predict(X_valid )<set_options>
clf = RandomForestClassifier(random_state = 1)
 parameters = {'n_estimators' : [10, 20, 30,50, 100] , 'max_features' : [0.6, 0.2, 0.3], 'min_samples_leaf' :[1,2,3],
'min_samples_split':[2,3,4,6]}
acc_scorer = make_scorer(accuracy_score)
 grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer, cv = 5)
 grid_obj = grid_obj.fit(X_train, y_train)
 best_clf = grid_obj.best_estimator_
best_clf.fit(X_train, y_train)
 best_predictions = best_clf.predict(X_test)
 print("
Optimized Model
------")
 print("Final accuracy score on the testing data: {:.4f}".format(accuracy_score(y_test, best_predictions)))
 print("Final F-score on the testing data: {:.4f}".format(fbeta_score(y_test, best_predictions, beta = 0.5)))
del X; gc.collect()<predict_on_test>
pred_test = best_clf.predict(test_df_final)
 submission = pd.read_csv('.. /input/gender_submission.csv')
 submission['Survived']=pred_test
submission['PassengerId']=test_original['PassengerId']
pd.DataFrame(submission, columns=['PassengerId','Survived'] ).to_csv('randomforest.csv', index = False)
 print(submission.head() )