Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<prepare_output>
warnings.simplefilter("ignore" )
sample_submission['isFraud'] = sub_preds
gc.collect()<compute_test_metric>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
score = roc_auc_score(y_train, oof_preds)
 score<save_to_csv>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 sub = pd.read_csv('.. /input/gender_submission.csv' )
sample_submission.to_csv('sub_ieee_sample_xgb_%sfold_%.6f.csv'%(n_fold, score))<save_to_csv>
train_df.isnull().sum()
oof_train = pd.DataFrame(oof_preds, columns=["oof_preds"], index=train.index)
 oof_train.to_csv("ieee_xgb_%sfold_oof_%.6f.csv" %(n_fold, score))
oof_train.head()<load_from_csv>
test_df.isnull().sum()
xl = pd.ExcelFile("ieee_xgbfir_4Fold.xlsx" )<set_options>
imputer = SimpleImputer(np.nan, "mean")
 train_df['Age'] = imputer.fit_transform(np.array(train_df['Age'] ).reshape(891, 1))
train_df.Embarked.fillna(method='ffill', inplace=True)
 train_df.drop(['PassengerId', 'Name', 'Cabin'], axis=1, inplace=True)
 test_df['Age'] = imputer.fit_transform(np.array(test_df['Age'] ).reshape(418, 1))
test_df.Embarked.fillna(method='ffill', inplace=True)
 test_df.Fare.fillna(method='ffill', inplace=True)
 test_df.drop(['PassengerId', 'Name', 'Cabin'], axis=1, inplace=True )
xl.sheet_names<set_options>
%matplotlib inline
warnings.filterwarnings('ignore')
<load_from_csv>
le = LabelEncoder()
train_df[objects_cols] = train_df[objects_cols].apply(le.fit_transform)
 test_df[objects_cols] = test_df[objects_cols].apply(le.fit_transform)
 train_df[objects_cols].head()
sessions=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/sessions.csv.zip')
 countries=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/countries.csv.zip')
 age_gender=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/age_gender_bkts.csv.zip')
 submission=pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/sample_submission_NDF.csv.zip' )<load_from_csv>
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from xgboost import XGBClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
train= pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip')
 test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv.zip')
<sort_values>
X = train_df.drop(['Survived'], 1 ).values
y = train_df['Survived'].values
countries.sort_values(by='distance_km' )<merge>
scale = StandardScaler()
scale.fit(X)
 X = scale.transform(X )
a=train['country_destination'].value_counts()
a=a.drop(['NDF','other'],axis=0)
 df_value_counts = pd.DataFrame(a)
 df_value_counts = a.reset_index()
df_value_counts.columns = ['country_destination', 'value_counts']
mapdata= pd.merge(df_value_counts, countries,how= 'inner' , on='country_destination')
 mapdata['poppercent']=mapdata['value_counts']/mapdata['value_counts'].sum()
mapdata['text']=['United States', 'France', 'Italy','United Kingdom', 'Spanish','Canada' ,'German', 'Dutch','Australia', 'Brazil' ]<sort_values>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0 )
a=train.groupby('country_destination' ).count().sort_values(by='id',ascending=True)
 a.id 
<drop_column>
class Model:
def __init__(self, model):
self.model = model
self.X, self.y = X, y
self.X_train, self.X_test, self.y_train, self.y_test = X_train, X_test, y_train, y_test
self.train()
def model_name(self):
model_name = type(self.model ).__name__
return model_name
def cross_validation(self, cv=5):
print(f"Evaluate {self.model_name() } score by cross-validation...")
 CVS = cross_val_score(self.model, self.X, self.y, scoring='accuracy', cv=cv)
 print(CVS)
 print("="*60, "
Mean accuracy of cross-validation: ", CVS.mean())
 def train(self):
print(f"Training {self.model_name() } Model...")
 self.model.fit(X_train, y_train)
 print("Model Trained.")
 def prediction(self, test_x=None, test=False):
if test == False:
y_pred = self.model.predict(self.X_test)
 else:
y_pred = self.model.predict(test_x)
 return y_pred
def accuracy(self):
y_pred = self.prediction()
y_test = self.y_test
acc = accuracy_score(y_pred, y_test)
 print(f"{self.model_name() } Model Accuracy: ", acc )
age_gender.drop(age_gender.index[0],inplace=True)
 age_gender<filter>
xgb = XGBClassifier()
xgb = Model(xgb )
agebtw0n19= age_gender[(age_gender.age_bucket=='0-4')|(age_gender.age_bucket=='5-9')|(age_gender.age_bucket=='10-14')|(age_gender.age_bucket=='15-19')]
agebtw49n34= age_gender[(age_gender.age_bucket=='45-49')|(age_gender.age_bucket=='40-44')|(age_gender.age_bucket=='35-39')|(age_gender.age_bucket=='30-34')]
agebtw99n50= age_gender[(age_gender.age_bucket=='95-99')|(age_gender.age_bucket=='90-94')|(age_gender.age_bucket=='85-89')|(age_gender.age_bucket=='80-84')|(age_gender.age_bucket=='75-79')|(age_gender.age_bucket=='70-74')|(age_gender.age_bucket=='70-74')|(age_gender.age_bucket=='65-69')|(age_gender.age_bucket=='60-64')|(age_gender.age_bucket=='55-59')|(age_gender.age_bucket=='50-54')]
agebtw20n29= age_gender[(age_gender.age_bucket=='25-29')|(age_gender.age_bucket=='20-24')] 
<statistical_test>
gnb = GaussianNB()
gnb = Model(gnb )
actionanditsdetail=pd.crosstab(age_gender.age_bucket, age_gender.country_destination,margins=False)
 stat, p, dof, expected = chi2_contingency(actionanditsdetail)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)' )<statistical_test>
rfc = RandomForestClassifier()
rfc = Model(rfc)
 rfc.cross_validation()
rfc.accuracy()
actionanditsdetail=pd.crosstab(age_gender.country_destination, age_gender.gender,margins=False)
 stat, p, dof, expected = chi2_contingency(actionanditsdetail)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)')
<sort_values>
test_X = test_df.values
test_X = scale.transform(test_X )
sessions=sessions.sort_values('user_id')
 sessions.rename(columns={"user_id": "id"},inplace=True)
 sessions.sort_values(by='id',inplace=True)
 sessions = sessions[(sessions['secs_elapsed'].notnull())&(sessions['secs_elapsed'] > 0.0)]
sessions[(sessions['action_detail']=='booking')]<feature_engineering>
xgb_pred = xgb.prediction(test_x=test_X, test=True)
 gnb_pred = gnb.prediction(test_x=test_X, test=True)
 svc_pred = svc.prediction(test_x=test_X, test=True)
 rfc_pred = rfc.prediction(test_x=test_X, test=True )
groupedid=pd.DataFrame(sessions.groupby(['id'])['secs_elapsed'].sum())
 groupedid['hour_elapsed']=groupedid['secs_elapsed'].div(3600 ).round(decimals=0)
 groupedid.drop('secs_elapsed',axis=1,inplace=True)
 groupedid.reset_index(inplace=True)
 groupedid[(groupedid['id'] == '6udv3scuxe')|(groupedid['id'] == 'yxf0sm9sbw')|(groupedid['id'] == 'nttj7g9av6')]<statistical_test>
sum_pred = xgb_pred + gnb_pred + svc_pred + rfc_pred
max_pred = np.where(sum_pred > 0, 1, 0)
 min_pred = np.where(sum_pred > 0, 0, 1 )
actionndevicetype=pd.crosstab(sessions.action, sessions.device_type,margins=False)
 stat, p, dof, expected = chi2_contingency(actionndevicetype)
 alpha = 0.05
print("p value is " + str(p))
if p <= alpha:
	print('Dependent(reject H0)')
 else:
	print('Independent(H0 holds true)' )<count_values>
sub.head()
sub.to_csv('submission.csv', index=False)
 sub.head()
print(train.country_destination.value_counts())
 sns.countplot(train.country_destination )<load_from_csv>
sub['Survived'] = xgb_pred
sub.to_csv('xgb_submission.csv', index=False)
 sub.head(10 )
train= pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv.zip')
 test = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv.zip')
<sort_values>
sub['Survived'] = gnb_pred
sub.to_csv('gnb_submission.csv', index=False)
 sub.head(10 )
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )<correct_missing_values>
sub['Survived'] = svc_pred
sub.to_csv('svc_submission.csv', index=False)
 sub.head(10 )
train['first_affiliate_tracked'] = train['first_affiliate_tracked'].fillna('Unknown')
 test['first_affiliate_tracked'] = test['first_affiliate_tracked'].fillna('Unknown' )<feature_engineering>
sub['Survived'] = rfc_pred
sub.to_csv('rfc_submission.csv', index=False)
 sub.head(10 )
<data_type_conversions><EOS> 
sub['Survived'] = max_pred
sub.to_csv('max_submission.csv', index=False)
 sub.head(10 )