Kaggle competition: Titanic - Machine Learning from Disaster
train_transaction = pd.read_csv('.. /input/ieee-fraud-detection/train_transaction.csv', index_col='TransactionID')
 train_identity = pd.read_csv('.. /input/ieee-fraud-detection/train_identity.csv', index_col='TransactionID')
 train = train_transaction.merge(train_identity, how='left', left_index=True, right_index=True)
 del train_transaction, train_identity
test_transaction = pd.read_csv('.. /input/ieee-fraud-detection/test_transaction.csv', index_col='TransactionID')
 test_identity = pd.read_csv('.. /input/ieee-fraud-detection/test_identity.csv', index_col='TransactionID')
 test = test_transaction.merge(test_identity, how='left', left_index=True, right_index=True)
 del test_transaction, test_identity<count_missing_values>
%matplotlib inline
na_columns = train.isna().sum()
print(na_columns[na_columns>0]/train.shape[0] )<count_unique_values>
def init() :
test_df = pd.read_csv(".. /input/titanic/test.csv")
 train_df = pd.read_csv(".. /input/titanic/train.csv")
 test_df["Survived"]=-8888
Main_df=pd.concat(( train_df, test_df), axis=0, sort=False)
 print("Columns with null values {}".format(Main_df.columns[Main_df.isnull().any() ]))
global title_map
title_map={
"mr": "Mr","mrs": "Mrs","miss": "Miss","master": "Master","don": "Royal", "rev": "Officer", "dr": "Officer",
"mme": "Mrs", "dona": "Royal", "jonkheer" : "Royal", "the countess": "Royal", "capt": "Officer", "col": "Officer",
"ms": "Mrs", "mlle": "Miss", "major": "Officer", "lady" : "Royal", "sir" : "Royal"
}
return(Main_df )
one_value_cols = [col for col in train.columns if train[col].nunique() <= 1]
one_value_cols_test = [col for col in test.columns if test[col].nunique() <= 1]
one_value_cols == one_value_cols_test<feature_engineering>
def get_title(x):
x=x.split(",")[1].split(".")[0].strip().lower()
return(title_map[x])
 def is_in_group(df):
gdf=pd.DataFrame({"GroupSize": df.groupby("Ticket")["Ticket"].count() } ).reset_index()
df=pd.merge(df, gdf, how="left", on="Ticket" ).set_index(df.index)
 df["Fare"].fillna(8.0, inplace=True)
 df["ActualFare"]=df["Fare"].astype(int)/df["GroupSize"]
df["IsMother"]= np.where(( df["Sex"]=="female")&(df["Parch"] > 0)&(df["Title"] != "Miss"), 1, 0)
 df['ST_third_class'] = np.where(( df['Ticket'].str.match(pat='(SOTON)|(W./C.) |(A/5)')) &(df["Pclass"]==3)&(df["Cabin"].isnull()), 1, 0)
 df["PC"]= np.where(( df["Pclass"]==1)&(df['Ticket'].str.match(pat='(PC)')) &(df["Cabin"].isnull()), 1, 0)
 df['ST_WC'] = np.where(( df['Ticket'].str.match(pat='(W./C.) ')) &(df["Cabin"].isnull()), 1, 0)
 return(df)
 def adjust_columns(df):
columns=["Title", "Sex","Pclass", "Embarked"]
df=pd.get_dummies(df, columns=columns)
 df.drop(["Name", "Ticket", "Cabin", "Fare", "Cabin"], axis=1, inplace=True)
 df.drop(["Title_Royal"], axis=1, inplace=True)
 return(df)
 def age(df):
cond = df["IsMother"]==1
median = df.loc[cond, "Age"].median()
df.loc[cond, "Age"]=df.loc[cond, "Age"].fillna(median)
 cond=df["Parch"]==0
median = df.loc[cond, "Age"].median()
df.loc[cond, "Age"]=df.loc[cond, "Age"].fillna(median)
 df["Age"].fillna(df.groupby(['Sex','Title'])["Age"].transform("median"), inplace=True)
 fig,(axis1)= plt.subplots(1,1, figsize=(10,5))
sn.distplot(df["Age"], ax=axis1)
 return(df )
def datetime_trans(data,start_date='2017-11-30'):
startdate=datetime.datetime.strptime(start_date,"%Y-%m-%d")
 data['TransactionDT']=data['TransactionDT'].fillna(data['TransactionDT'].mean())
 data['date']=data['TransactionDT'].apply(lambda x : datetime.timedelta(seconds=x)+startdate)
 data['weekday']=data['date'].apply(lambda x :x.weekday())
 data['month']=(data['date'].dt.year-2017)*12+train['date'].dt.month
data['hour']=data['date'].apply(lambda x :x.hour)
 data['day']=(data['date'].dt.year-2017)*365+train['date'].dt.dayofyear
data['year_weekday']=data['date'].apply(lambda x : str(x.year)+'_'+str(x.weekday()))
data['weekday_hour']=data['date'].apply(lambda x :str(x.weekday())+'_'+str(x.hour))
date_col=['weekday','month','day','hour','year_weekday','weekday_hour']
datetime_trans(train)
 datetime_trans(test )<categorify>
warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings("ignore" )
def transaction_amount_details(data):
data['TransactionAmt_to_mean_card1'] = data['TransactionAmt'] / data.groupby(['card1'])['TransactionAmt'].transform('mean')
 data['TransactionAmt_to_mean_card4'] = data['TransactionAmt'] / data.groupby(['card4'])['TransactionAmt'].transform('mean')
 data['TransactionAmt_to_std_card1'] = data['TransactionAmt'] / data.groupby(['card1'])['TransactionAmt'].transform('std')
 data['TransactionAmt_to_std_card4'] = data['TransactionAmt'] / data.groupby(['card4'])['TransactionAmt'].transform('std')
 transaction_amount_details(train)
 transaction_amount_details(test )<feature_engineering>
train=df.loc[df["Survived"]!=-8888]
test=df.loc[df["Survived"]==-8888]
cols=list(train.columns)
 cols.remove("Survived")
 cols.remove("PassengerId")
 print(cols)
 X_train=train.drop(["Survived","PassengerId"], axis=1 ).values
Y_train=train["Survived"].values
X_test=test.drop(["Survived","PassengerId"], axis=1 ).values
print(df.shape, X_train.shape, Y_train.shape, X_test.shape )
def email_details(data):
data[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = data['P_emaildomain'].str.split('.', expand=True)
 data[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = data['R_emaildomain'].str.split('.', expand=True)
 email_details(train)
 email_details(test)
<data_type_conversions>
def grid_search_param(algo):
if algo == "Logistic":
clf=LogisticRegression()
c_space = np.logspace(-5, 8, 15)
 penalty=["l1", "l2"]
param_grid = {'C': c_space, 'penalty' : penalty}
elif algo=="RandomForest":
clf=RandomForestClassifier()
param_grid = { 'n_estimators': [700],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [6],
'criterion' :['gini', 'entropy'],
}
elif algo=="KNN":
clf=KNeighborsClassifier()
param_grid ={'n_neighbors' : [6,7,8,9,10,11,12,14,16,18,20,22],
'algorithm' : ['auto'],
'weights' : ['uniform', 'distance'],
'leaf_size' : list(range(1,50,5))
}
return(param_grid, clf)
 def get_best_param(clf, param):
grid = GridSearchCV(clf, param, cv = 10)
 grid.fit(X_train, Y_train)
 print("Tuned Parameters: {}".format(grid.best_params_))
print("Best score is {}".format(grid.best_score_))
return(grid)
 def run_clf(grid, clf, algo):
if algo == "Logistic":
c_value=grid.best_params_["C"]
penalty=grid.best_params_["penalty"]
print("Best parameter: C is {} and Penalty is {}".format(c_value, penalty))
clf=LogisticRegression(C=c_value, penalty=penalty)
 elif algo=="RandomForest":
n_estimators=grid.best_params_["n_estimators"]
max_features=grid.best_params_["max_features"]
max_depth=grid.best_params_["max_depth"]
criterion=grid.best_params_["criterion"]
clf=RandomForestClassifier(n_estimators=n_estimators, max_features=max_features, max_depth=max_depth, criterion=criterion)
 elif algo=="KNN":
n_neighbors=grid.best_params_["n_neighbors"]
algorithm=grid.best_params_["algorithm"]
weights=grid.best_params_["weights"]
leaf_size=grid.best_params_["leaf_size"]
clf=KNeighborsClassifier(n_neighbors=n_neighbors, algorithm=algorithm,weights=weights,leaf_size=leaf_size)
 clf.fit(X_train, Y_train)
 print(confusion_matrix(Y_train, clf.predict(X_train)))
 print("{} score is {}".format(algo, clf.score(X_train,Y_train)))
 return(clf)
def id_and_address(data):
data['uid'] = data['card1'].astype(str)+'_'+data['card2'].astype(str)
 data['uid2'] = data['uid'].astype(str)+'_'+data['card3'].astype(str)+'_'+data['card5'].astype(str)
 data['uid3'] = data['uid2'].astype(str)+'_'+data['addr1'].astype(str)+'_'+data['addr2'].astype(str)
 data['uid4'] = data['addr1'].astype(str)+'_'+data['addr2'].astype(str)
 data['D9'] = np.where(data['D9'].isna() ,0,1)
 id_and_address(train)
 id_and_address(test )<categorify>
print("Running Logistic regression")
 param, clf=grid_search_param("Logistic")
(grid)= get_best_param(clf, param)
 clfLR=run_clf(grid, clf, "Logistic")
 pred_values=clfLR.predict(X_test)
 test["Survived"]=pd.Series(pred_values, index=test.index)
 test[["PassengerId", "Survived"]].to_csv("LRPrediction.csv", index=False)
 print("Running Random Forest")
 param, clf=grid_search_param("RandomForest")
(grid)= get_best_param(clf, param)
 clfRF=run_clf(grid, clf, "RandomForest")
 importances = clfRF.feature_importances_
DF=pd.DataFrame({"Features": cols, "Importance": importances})
 print(DF.sort_values(by ='Importance', ascending = False))
pred_values=clfRF.predict(X_test)
 test["Survived"]=pd.Series(pred_values, index=test.index)
 test[["PassengerId", "Survived"]].to_csv("RFPrediction.csv", index=False)