Kaggle competition: Mercedes-Benz Greener Manufacturing
def GetTitle_temp(name):
fname_title = name.split(',')[1]
title = fname_title.split('.')[0]
title = title.strip().lower()
return title
df.Name.map(GetTitle_temp ).value_counts()<count_values>
warnings.simplefilter(action='ignore', category=FutureWarning)
 pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500 )
df['Parch'].value_counts()<count_values>
train = pd.read_csv(".. /input/mercedes-benz-greener-manufacturing/train.csv")
 test = pd.read_csv(".. /input/mercedes-benz-greener-manufacturing/test.csv")
 print("Training Dataset Shape {}".format(train.shape))
print("Testing Dataset Shape {}".format(test.shape))
df['SibSp'].value_counts()<drop_column>
Submission = test[['ID']]
test.drop('ID',axis=1,inplace=True)
 train.drop('ID',axis=1,inplace=True )
df['Accomp'] = df['SibSp'] + df['Parch']
df.drop(columns = ['SibSp' , 'Parch'] , inplace = True )<count_values>
train.dtypes.value_counts()
df['Sex'].value_counts()<categorify>
train.isna().sum() [train.isna().sum() > 0]
df['Sex'] = df['Sex'].map({'female':0 , 'male':1 } )<drop_column>
test.isna().sum() [test.isna().sum() > 0]
df.drop(columns = ['Ticket'] , inplace = True )<categorify>
train.drop(train[train['y'] > 250].index[0],axis=0,inplace=True )
cat = pd.get_dummies(df[['Embarked' , 'Name']] , drop_first=True )<concatenate>
lst_train = []
for col in train.columns:
if train[col].nunique() == 1:
lst_train.append(col)
 print("Column {} has Single Unique Value".format(col))
df = pd.concat([df,cat] , axis = 1)
 df.drop(columns = ['Embarked' , 'Name'] , inplace = True )<count_missing_values>
lst_test = []
for col in test.columns:
if test[col].nunique() == 1:
lst_test.append(col)
 print("Column {} has Single Unique Value".format(col))
df.isnull().mean() *100<categorify>
for col in train.columns:
if train[col].dtype == "object":
print("Unique Values in {} column".format(col),train[col].nunique() )
knn_imputer = KNN()
df_knn = df.copy()
df_knn.iloc[:,:] = knn_imputer.fit_transform(df_knn )<categorify>
for col in test.columns:
if test[col].dtype == "object":
print("Unique Values in {} column".format(col),test[col].nunique() )
MICE_imputer = IterativeImputer()
df_mice = df.copy()
df_mice.iloc[:,:] = knn_imputer.fit_transform(df_mice )<train_model>
test['X4'].value_counts()
X, y = dfm_train.drop(columns = ['Survived','PassengerId']), dfm_train['Survived']
X_train, X_test, y_train, y_test= train_test_split(X, y,test_size=0.2, random_state=123)
 xg_cl = xgb.XGBClassifier(objective='binary:logistic',
n_estimators=20, seed=123)
 xg_cl.fit(X_train, y_train)
 preds = xg_cl.predict(X_test)
 accuracy = float(np.sum(preds==y_test)) /y_test.shape[0]
print("accuracy: %f" %(accuracy))<compute_test_metric>
train.groupby('X4')['y'].agg({"count","min",'max','mean','median'} )
confusion_matrix(y_test , preds )<save_to_csv>
df = train[[col for col in train.columns if(( train[col].dtype == 'int')&(col not in ['ID','y'])) ]].columns
lst_train = []
for col in train.columns:
if(( train[col].dtype == 'int')&(col not in ['ID','y'])) :
lst_train.append(sum(train[col])/len(train))
df_new = pd.DataFrame(lst_train,df,columns=['Ratio_Train'] ).sort_index()
lst_test = []
for col in test.columns:
if(( test[col].dtype == 'int')&(col !='ID')) :
lst_test.append(sum(test[col])/len(test))
df_new['Ratio_Test'] = lst_test
df_new.sort_index()
y_pred = xg_cl.predict(dfm_test.drop(columns='PassengerId')).astype('int')
 results = pd.DataFrame(data={'PassengerId':dfm_test['PassengerId'].astype('int'), 'Survived':y_pred})
 results.to_csv('Titanic Prediction_XGB.csv', index=False )<split>
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper.head()
X, y = dfm_train.drop(columns = ['Survived','PassengerId']), dfm_train['Survived']
X_train, X_test, y_train, y_test= train_test_split(X, y,test_size=0.2, random_state=123)
 params = {
'min_child_weight': [1, 3,5,7 , 10],
'gamma': [0.5, 1, 1.5, 2,3,4, 5],
'subsample': [0.6,0.7, 0.8,0.9, 1.0],
'colsample_bytree': [0.6,0.7, 0.8,0.9, 1.0],
'max_depth': [3, 4, 5 , 6]
}
xgb = xgb.XGBClassifier(learning_rate=0.02, n_estimators=600, objective='binary:logistic',
silent=True, nthread=1)
 folds = 3
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(xgb, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X,y), verbose=3, random_state=1001)
 random_search.fit(X, y)
<find_best_params>
to_drop = [column for column in upper.columns if any(upper[column] > threshold)]
print('There are %d columns to remove.' %(len(to_drop)))
 print("Following columns can be dropped {}".format(to_drop))
random_search.best_params_<save_to_csv>
train = train.drop(columns = to_drop)
 test = test.drop(columns = to_drop)
 print('Training shape: ', train.shape)
 print('Testing shape: ', test.shape )
y_test = random_search.predict(dfm_test.drop(columns='PassengerId')).astype('int')
 results = pd.DataFrame(data={'PassengerId':dfm_test['PassengerId'].astype('int'), 'Survived':y_test})
 results.to_csv('Titanic Prediction_XGB_hp.csv', index=False)
<split>
train.drop('X4',axis=1,inplace=True)
 test.drop('X4',axis=1,inplace=True )
X, y = dfm_train.drop(columns = ['Survived','PassengerId']), dfm_train['Survived']
X_train, X_test, y_train, y_test= train_test_split(X, y,test_size=0.2, random_state=123)
 params = {
'min_child_weight': [1, 3,5,7 , 10],
'gamma': [0.5, 1, 1.5, 2,3,4, 5],
'subsample': [0.6,0.7, 0.8,0.9, 1.0],
'colsample_bytree': [0.6,0.7, 0.8,0.9, 1.0],
'max_depth': [3, 4, 5 , 6]
}
xgb = xgb.XGBClassifier(learning_rate=0.02, n_estimators=600, objective='binary:logistic',
silent=True, nthread=1)
 folds = 3
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(xgb, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X,y), verbose=3, random_state=1001)
 random_search.fit(X, y)
<save_to_csv>
for col in train.columns:
if train[col].dtype == "object":
train[col] = train[col].astype('category')
 train[col] = train[col].cat.codes
y_test = random_search.predict(dfm_test.drop(columns='PassengerId')).astype('int')
 results = pd.DataFrame(data={'PassengerId':dfm_test['PassengerId'].astype('int'), 'Survived':y_test})
 results.to_csv('Titanic Predictionkn.csv', index=False)
<train_on_grid>
for col in test.columns:
if test[col].dtype == "object":
test[col] = test[col].astype('category')
 test[col] = test[col].cat.codes
knn = KNeighborsClassifier()
params = {
'n_neighbors' : sp_randint(1 , 20),
'p' : sp_randint(1 , 5),
}
rsearch_knn = RandomizedSearchCV(knn , param_distributions = params , cv = 3 , random_state= 3 , n_jobs = -1 , return_train_score=True)
 rsearch_knn.fit(X , y )<find_best_params>
X = train[[col for col in train.columns if col!='y']]
y = train['y']
rsearch_knn.best_params_<choose_model_class>
rf = RandomForestRegressor(n_jobs=-1,random_state=42)
 rf.fit(X,y )
rfc = RandomForestClassifier(random_state=3)
 params = { 'n_estimators' : sp_randint(50 , 200),
'max_features' : sp_randint(1 , 12),
'max_depth' : sp_randint(2,10),
'min_samples_split' : sp_randint(2,20),
'min_samples_leaf' : sp_randint(1,20),
'criterion' : ['gini' , 'entropy']
}
rsearch_rfc = RandomizedSearchCV(rfc , param_distributions= params , n_iter= 200 , cv = 3 , scoring='roc_auc' , random_state= 3 , return_train_score=True , n_jobs=-1)
 rsearch_rfc.fit(X,y )<find_best_params>
zero_features = list(feature_importances[feature_importances['importance'] == 0.0]['feature'])
 print('There are %d features with 0.0 importance' % len(zero_features))
feature_importances
rsearch_rfc.best_params_<train_model>
train.drop(zero_features,axis=1,inplace=True)
 test.drop(zero_features,axis=1,inplace=True )
lr = LogisticRegression(solver = 'liblinear')
 knn = KNeighborsClassifier(**rsearch_knn.best_params_)
 rfc = RandomForestClassifier(**rsearch_rfc.best_params_)
 clf = VotingClassifier(estimators=[('lr' ,lr),('knn' , knn),('rfc' , rfc)] , voting = 'soft')
 clf.fit(X , y )<save_to_csv>
X = train[[col for col in train.columns if col!='y']]
y = train['y']
y_test = clf.predict(dfm_test.drop(columns='PassengerId')).astype('int')
 results = pd.DataFrame(data={'PassengerId':dfm_test['PassengerId'].astype('int'), 'Survived':y_test})
 results.to_csv('Titanic Prediction_Stack.csv', index=False)
<split>
rf_2 = RandomForestRegressor(n_jobs=-1,random_state=42)
 rf_2.fit(X,y )
X, y = dfm_train.drop(columns = ['Survived','PassengerId']), dfm_train['Survived']
X_train, X_test, y_train, y_test= train_test_split(X, y,test_size=0.2, random_state=123 )<choose_model_class>
rf_3 = RandomForestRegressor(n_jobs=-1,random_state=42)
 params = {"n_estimators":list(range(50,501,100)) ,
"max_features":[0.5,'sqrt','log2'],
"min_samples_leaf":[1,3,5,10,25],
"min_samples_split":[2,5,7,10,15],
"max_depth":[3,5,7,9]}
r_search = RandomizedSearchCV(estimator=rf_3,param_distributions=params,cv=5,scoring='r2')
 r_search.fit(X,y )
RFM = RandomForestClassifier(criterion='gini',
n_estimators=1750,
max_depth=7,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=123,
n_jobs=-1,
verbose=1)
 RFM.fit(X,y )<predict_on_test>
r_search.best_estimator_,r_search.best_params_,r_search.best_score_
y_pred = RFM.predict(X_test )<save_to_csv>
zero_features = list(feature_importances[feature_importances['importance'] == 0.0]['feature'])
 print('There are %d features with 0.0 importance' % len(zero_features))
feature_importances
y_test = RFM.predict(dfm_test.drop(columns='PassengerId')).astype('int')
 results = pd.DataFrame(data={'PassengerId':dfm_test['PassengerId'].astype('int'), 'Survived':y_test})
 results.to_csv('Titanic PredictionRFM.csv', index=False)
<split>
train.drop(zero_features,axis=1,inplace=True)
 test.drop(zero_features,axis=1,inplace=True )
X, y = dfm_train.drop(columns = ['Survived','PassengerId']), dfm_train['Survived']
X_train, X_test, y_train, y_test= train_test_split(X, y,test_size=0.2, random_state=123 )<train_on_grid>
X = train[[col for col in train.columns if col!='y']]
y = train['y']
rf_4 = RandomForestRegressor(n_jobs=-1,random_state=42)
 params = {"n_estimators":list(range(50,501,100)) ,
"max_features":[0.5,'sqrt','log2'],
"min_samples_leaf":[1,3,5,10,25],
"min_samples_split":[2,5,7,10,15],
"max_depth":[3,5,7,9]}
r_search2 = RandomizedSearchCV(estimator=rf_4,param_distributions=params,cv=5,scoring='r2')
 r_search2.fit(X,y)
 r_search2.best_estimator_,r_search2.best_params_,r_search2.best_score_
clf_ET = ExtraTreesClassifier(random_state=0, bootstrap=True, oob_score=True)
 sss = model_selection.StratifiedShuffleSplit(n_splits=10, test_size=0.33, random_state= 0)
 sss.get_n_splits(X, y)
 parameters = {'n_estimators' : np.r_[10:210:10],
'max_depth': np.r_[1:6]
}
grid = model_selection.GridSearchCV(clf_ET, param_grid=parameters, scoring = 'accuracy', cv = sss, return_train_score=True, n_jobs=4, verbose=2)
 grid.fit(X,y )<save_to_csv>
Submission['y'] = r_search2.predict(test)
 Submission.to_csv("Submission_2.csv",index=None )