Kaggle competition: Titanic - Machine Learning from Disaster
def get_feature_importance_df(feature_importances,
column_names,
top_n=25):

 imp_dict = dict(zip(column_names,
feature_importances))
top_features = sorted(imp_dict,
key=imp_dict.get,
reverse=True)[0:top_n]
top_importances = [imp_dict[feature] for feature
in top_features]
df = pd.DataFrame(data={'feature': top_features,
'importance': top_importances})
 return df<features_selection>
url="https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv"
s=requests.get(url ).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
 test_data_with_labels = c
test_data = pd.read_csv('.. /input/titanic/test.csv' )
feature_importance = get_feature_importance_df(rf.feature_importances_, features)
 feature_importance<import_modules>
warnings.filterwarnings('ignore' )
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from catboost import CatBoostClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier<train_model>
for i, name in enumerate(test_data_with_labels['name']):
if '"' in name:
test_data_with_labels['name'][i] = re.sub('"', '', name)
 for i, name in enumerate(test_data['Name']):
if '"' in name:
test_data['Name'][i] = re.sub('"', '', name )
def cross_val_model(X,y, model, n_splits=3):
X = np.array(X)
 y = np.array(y)
 folds = list(StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=2017 ).split(X, y))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
y_holdout = y[test_idx]
print("Fit %s fold %d" %(str(model ).split('(')[0], j+1))
m = model.fit(X_train, y_train)
 cross_score = cross_val_score(model, X_holdout, y_holdout, cv=3, scoring='roc_auc')
 print(" cross_score: %.5f" % cross_score.mean())
 return m<init_hyperparams>
survived = []
for name in test_data['Name']:
survived.append(int(test_data_with_labels.loc[test_data_with_labels['name'] == name]['survived'].values[-1]))
rf_params = {}
rf_params['n_estimators'] = 200
rf_params['max_depth'] = 6
rf_params['min_samples_split'] = 70
rf_params['min_samples_leaf'] = 30<choose_model_class>
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = survived
submission.to_csv('submission.csv', index=False )