Kaggle competition: Titanic - Machine Learning from Disaster
missing_values =(100-full_train_df.count() / len(full_train_df)* 100 ).sort_values(ascending=False )<filter>
import matplotlib.pyplot
import seaborn as sns
%%time
missing_features = full_train_df.loc[:, missing_values > 0.0]
missing_features = missing_features.apply(mean_without_overflow_fast )<data_type_conversions>
dataset = pd.read_csv('.. /input/train.csv')
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
for key in full_train_df.loc[:, missing_values > 0.0].keys() :
if key == 'year_built' or key == 'floor_count':
full_train_df[key].fillna(math.floor(missing_features[key]), inplace=True)
 full_test_df[key].fillna(math.floor(missing_features[key]), inplace=True)
 else:
full_train_df[key].fillna(missing_features[key], inplace=True)
 full_test_df[key].fillna(missing_features[key], inplace=True )<data_type_conversions>
Y_train = train.iloc[:,1].values
train.drop(columns=['Survived','Name','Ticket'],axis=1,inplace=True)
 train.drop(columns=['Embarked','Cabin'],axis=1,inplace=True)
 train = pd.get_dummies(train,columns=['Sex'],drop_first=True )
full_train_df["timestamp"] = pd.to_datetime(full_train_df["timestamp"])
 full_test_df["timestamp"] = pd.to_datetime(full_test_df["timestamp"] )<feature_engineering>
train.drop(columns=['PassengerId'],axis=1,inplace=True )
def transform(df):
df['hour'] = np.uint8(df['timestamp'].dt.hour)
 df['day'] = np.uint8(df['timestamp'].dt.day)
 df['weekday'] = np.uint8(df['timestamp'].dt.weekday)
 df['month'] = np.uint8(df['timestamp'].dt.month)
 df['year'] = np.uint8(df['timestamp'].dt.year-1900)
 df['square_feet'] = np.log(df['square_feet'])
 return df<categorify>
mean_value=train['Age'].mean()
train['Age']=train['Age'].fillna(mean_value)
 mean_val=train['Fare'].mean()
train['Fare']=train['Fare'].fillna(mean_val )
full_train_df = transform(full_train_df)
 full_test_df = transform(full_test_df )<categorify>
X_train = train.iloc[:,:].values
def encode_cyclic_feature(df, col, max_vals):
df[col + '_sin'] = np.sin(2 * np.pi * df[col]/max_vals)
 del df[col]
return df<data_type_conversions>
test.drop(columns=['Name','Ticket'],axis=1,inplace=True)
 test.drop(columns=['Embarked','Cabin'],axis=1,inplace=True)
 test.drop(columns=['PassengerId'],axis=1,inplace=True)
 test = pd.get_dummies(test,columns=['Sex'],drop_first=True )
dates_range = pd.date_range(start='2015-12-31', end='2019-01-01')
 us_holidays = calendar().holidays(start=dates_range.min() , end=dates_range.max())
 full_train_df['is_holiday'] =(full_train_df['timestamp'].dt.date.astype('datetime64' ).isin(us_holidays)).astype(np.int8)
 full_test_df['is_holiday'] =(full_test_df['timestamp'].dt.date.astype('datetime64' ).isin(us_holidays)).astype(np.int8 )<feature_engineering>
mean_value=test['Age'].mean()
test['Age']=test['Age'].fillna(mean_value)
 mean_val=test['Fare'].mean()
test['Fare']=test['Fare'].fillna(mean_val )
full_train_df.loc[(full_train_df['weekday'] == 5)|(full_train_df['weekday'] == 6), 'is_holiday'] = 1
full_test_df.loc[(full_test_df['weekday'])== 5 |(full_test_df['weekday'] == 6), 'is_holiday'] = 1<feature_engineering>
X_test = test.iloc[:,:].values
<filter>
dectree = DecisionTreeClassifier()
dectree.fit(X_train, Y_train)
 y_pred = dectree.predict(X_test )
full_train_df = full_train_df.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")' )<train_model>
gender = pd.read_csv('.. /input/gender_submission.csv')
 y_test = gender.iloc[:,1].values
full_test_df = full_test_df.drop(['timestamp'], axis=1)
 full_train_df = full_train_df.drop(['timestamp'], axis=1)
 print(f'Shape of training dataset: {full_train_df.shape}')
 print(f'Shape of testing dataset: {full_test_df.shape}' )<set_options>
print(classification_report(y_test,y_pred))
full_train_df = reduce_mem_usage(full_train_df)
 full_test_df = reduce_mem_usage(full_test_df)
 gc.collect()<feature_engineering>
random_forest = RandomForestClassifier()
random_forest.fit(X_train, Y_train)
 y_pred = random_forest.predict(X_test )
beaufort = [(0, 0, 0.3),(1, 0.3, 1.6),(2, 1.6, 3.4),(3, 3.4, 5.5),(4, 5.5, 8),(5, 8, 10.8),(6, 10.8, 13.9), 
(7, 13.9, 17.2),(8, 17.2, 20.8),(9, 20.8, 24.5),(10, 24.5, 28.5),(11, 28.5, 33),(12, 33, 200)]
for item in beaufort:
full_train_df.loc[(full_train_df['wind_speed']>=item[1])&(full_train_df['wind_speed']<item[2]), 'beaufort_scale'] = item[0]<categorify>
print(classification_report(y_test,y_pred))
full_train_df['group'] = full_train_df['month']
full_train_df['group'].replace(( 1,2,3,4,5,6), 1,inplace=True)
 full_train_df['group'].replace(( 7,8,9,10,11,12), 2, inplace=True )<categorify>
model=xgb.XGBClassifier(random_state=1,learning_rate=0.01)
 model.fit(X_train, Y_train)
 y_pred = model.predict(X_test)
 model.score(X_test,y_test )
full_train_df = encode_cyclic_feature(full_train_df, 'weekday', 7)
 full_train_df = encode_cyclic_feature(full_train_df, 'hour', 24)
 full_train_df = encode_cyclic_feature(full_train_df, 'day', 31)
 full_train_df = encode_cyclic_feature(full_train_df, 'month', 12 )<feature_engineering>
df_test = pd.read_csv('.. /input/test.csv' )
full_train_df = reduce_mem_usage(full_train_df )<define_variables>
submission = pd.DataFrame({'PassengerId':df_test['PassengerId'],'Survived':y_pred} )
<set_options><EOS> 
filename = 'Titanic Predictions.csv'
submission.to_csv(filename,index=False)
 print('Saved file: ' + filename )