Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
%matplotlib inline
del train_df_black
gc.collect()<filter>
df = pd.read_csv("/kaggle/input/titanic/train.csv")
 df.set_index('PassengerId', inplace=True)
 print("Shape : ", df.shape)
 print(df.dtypes)
 df.head()
train_df = train_df.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")' )<data_type_conversions>
df = df.drop(["Name","Ticket"], axis=1 )
( train_df[train_df.building_id == 954].meter_reading==0 ).astype(int ).plot()<filter>
missing_columns = df.columns[df.isnull().any() ]
df.isnull().sum(axis=0 ).loc[missing_columns]
train_df = train_df.query('not(building_id == 954 & meter_reading == 0)')
 train_df = train_df.query('not(building_id == 1221 & meter_reading == 0)' )<feature_engineering>
df = df.drop(["Cabin"], axis=1 )
funny_bids = [993, 1168, 904, 954, 778, 1021]
print('before', len(train_df))
if outlier:
train_df.loc[(train_df.building_id == 993)&(train_df.meter == 0)&(train_df.meter_reading > 30000), 'meter_reading'] = 31921
train_df.loc[(train_df.building_id == 993)&(train_df.meter == 1)&(train_df.meter_reading > 90000), 'meter_reading'] = 96545.5
train_df = train_df[(( train_df.building_id == 1168)&(train_df.meter == 0)&(train_df.meter_reading >10000)) == False]
train_df.loc[(train_df.building_id == 904)&(train_df.meter == 0)&(train_df.meter_reading >10000), 'meter_reading'] = 11306
train_df = train_df[(( train_df.building_id == 954)&(train_df.meter_reading >10000)) ==False]
if rescale:
train_df.loc[(train_df.building_id == 778)&(train_df.meter == 1), 'meter_reading'] = train_df.loc[(train_df.building_id == 778)&(train_df.meter == 1), 'meter_reading']/ 100
train_df.loc[(train_df.building_id == 1021)&(train_df.meter == 3), 'meter_reading'] = train_df.loc[(train_df.building_id == 1021)&(train_df.meter == 3), 'meter_reading']/ 1000
train_df = train_df.reset_index()
print('after', len(train_df))
gc.collect()<feature_engineering>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 X_test = test_data.copy()
X_test = X_test.drop(["Name","Ticket","Cabin","PassengerId"], axis=1)
 X_test.head()
train_df.loc[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0), 'meter_reading'] = train_df[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0)]['meter_reading'] * 0.2931<feature_engineering>
y = df.Survived
X = df.drop(['Survived'], axis=1)
 numerical_cols = [cname for cname in X if X[cname].dtype in ['int64', 'float64']]
categorical_cols = [cname for cname in X if X[cname].dtype == "object"]
numerical_transformer = SimpleImputer(strategy='mean')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore', sparse=False))
])
 preprocessor_temp = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 preprocessor = Pipeline(steps=[ 
('preprocessor_temp', preprocessor_temp), 
('scaler', StandardScaler())])
 preprocessor.fit(X )
train_df['date'] = train_df['timestamp'].dt.date
train_df['meter_reading_log1p'] = np.log1p(train_df['meter_reading'] )<feature_engineering>
X.iloc[0,:]
def preprocess(df):
df["hour"] = df["timestamp"].dt.hour
df["day"] = df["timestamp"].dt.day
df["weekend"] = df["timestamp"].dt.weekday
df["month"] = df["timestamp"].dt.month
df["dayofweek"] = df["timestamp"].dt.dayofweek 
<categorify>
dim_trans = preprocessor.transform(X ).shape[1]
preprocessor.transform(X)[0,:]
preprocess(train_df )<sort_values>
params_knn = {"kNN__n_neighbors": np.arange(3,10), "kNN__weights":["uniform", "distance"]}
kNN = KNeighborsClassifier()
pipe = Pipeline(steps=[('preprocessor', preprocessor),('kNN', kNN)])
 knnpipe = GridSearchCV(pipe, params_knn, n_jobs=-1)
 knnpipe.fit(X, y)
 print("Best parameter(CV score=%0.3f):" % knnpipe.best_score_)
 print(knnpipe.best_params_)
 print()
print("Accuracy training set %0.3f" % accuracy_score(y, knnpipe.predict(X)) )
if use_ucf:
train_df = train_df.sort_values('month')
 train_df = train_df.reset_index()<categorify>
params_logit = {"logit__penalty": ["l1","l2"], "logit__C":np.arange(0.5,1.5,0.1)}
logit = LogisticRegression()
pipe = Pipeline(steps=[('preprocessor', preprocessor),('logit', logit)])
 logitpipe = GridSearchCV(pipe, params_logit, n_jobs=-1)
 logitpipe.fit(X, y)
 print("Best parameter(CV score=%0.3f):" % logitpipe.best_score_)
 print(logitpipe.best_params_)
 print()
print("Accuracy training set %0.3f" % accuracy_score(y, logitpipe.predict(X)) )
<count_missing_values>
params_svm = {"svm__kernel": ["linear","poly","rbf"], "svm__C":np.arange(0.5,1.5,0.1)}
svm = SVC()
pipe = Pipeline(steps=[('preprocessor', preprocessor),('svm', svm)])
 svmpipe = GridSearchCV(pipe, params_svm, n_jobs=-1)
 svmpipe.fit(X, y)
 print("Best parameter(CV score=%0.3f):" % svmpipe.best_score_)
 print(svmpipe.best_params_)
 print()
print("Accuracy training set %0.3f" % accuracy_score(y, svmpipe.predict(X)) )
weather_train_df.isna().sum()<groupby>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state = 1)
 preprocessor.fit(X_train)
 X_train = preprocessor.transform(X_train)
 X_valid = preprocessor.transform(X_valid)
 network = Sequential([
Dense(16, activation='relu', input_shape=(dim_trans,)) ,
Dense(16, activation='relu'),
Dense(1, activation='sigmoid'),
])
 network.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
 es = EarlyStopping(monitor='val_accuracy', mode='max', verbose=1, patience=10)
 mc = ModelCheckpoint('best_model.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
 history = network.fit(X_train,
y_train,
epochs=1000,
verbose=1,
validation_data=(X_valid,y_valid),
callbacks=[es, mc])
 saved_model = load_model('best_model.h5')
 print("Accuracy training set %0.3f" % accuracy_score(y, saved_model.predict(preprocessor.transform(X)) >0.5))
weather_train_df.groupby('site_id' ).apply(lambda group: group.isna().sum() )<groupby>
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state = 1)
 preprocessor.fit(X_train)
 X_train = preprocessor.transform(X_train)
 X_valid = preprocessor.transform(X_valid)
 xgb = XGBClassifier(n_estimators=1000, learning_rate=0.05, n_jobs=-1)
 xgb.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_valid, y_valid)],
verbose=True)
 print("Accuracy training set %0.3f" % accuracy_score(y, xgb.predict(preprocessor.transform(X),ntree_limit=xgb.best_ntree_limit)) )
weather_train_df = weather_train_df.groupby('site_id' ).apply(lambda group: group.interpolate(limit_direction='both'))<groupby>
predictions = xgb.predict(preprocessor.transform(X_test),ntree_limit=xgb.best_ntree_limit)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )