Kaggle competition: ML in biology
train_data[(train_data.distance>100)&(train_data.fare_amount<50)]<groupby>
sample = pd.read_csv('/kaggle/input/mlbio1/sample_submission.csv')
 test = pd.read_csv('/kaggle/input/mlbio1/test.csv')
 train = pd.read_csv('/kaggle/input/mlbio1/train.csv')
train_data.groupby(['Month','Year'] ).count() ['fare_amount']<filter>
%matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white')
 flatui = ["
pl = sns.color_palette(flatui )
train_data = train_data.loc[train_data.pickup_latitude != 0]
train_data = train_data.loc[train_data.pickup_longitude != 0]
train_data = train_data.loc[train_data.dropoff_latitude != 0]
train_data = train_data.loc[train_data.dropoff_longitude != 0]<filter>
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
test_data = test_data.loc[test_data.pickup_latitude != 0]
test_data = test_data.loc[test_data.pickup_longitude != 0]
test_data = test_data.loc[test_data.dropoff_latitude != 0]
test_data = test_data.loc[test_data.dropoff_longitude != 0]<drop_column>
train = train.drop('smoking_status', axis=1)
 test = test.drop('smoking_status', axis=1 )
train_data = train_data.drop(columns='pickup_datetime',axis=1)
 test_data = test_data.drop(columns='pickup_datetime',axis=1 )<prepare_x_and_y>
mean_bmi = train['bmi'].mean()
train['bmi'] = train['bmi'].fillna(mean_bmi)
 test['bmi'] = test['bmi'].fillna(mean_bmi )
X = train_data.loc[:,train_data.columns != 'fare_amount']
y = train_data['fare_amount']<split>
train.groupby('stroke' ).size().reset_index(name='counts' )
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0)
 y_train = np.log(y_train)
 y_test = np.log(y_test )<compute_train_metric>
train[['gender', 'stroke']].groupby(['gender'], as_index=False ).mean().sort_values(by='stroke', ascending=False )
ran_for_reg = RandomForestRegressor(max_depth=400)
 ran_for_reg.fit(X_train,y_train)
 y_ranfor_pred = ran_for_reg.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_ranfor_pred))
error<compute_train_metric>
train['gender'].value_counts()
bagreg = BaggingRegressor(base_estimator=DecisionTreeRegressor() ,n_estimators=10,bootstrap=True,random_state=0)
 bagreg.fit(X_train,y_train)
 y_bagg_pred = bagreg.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_bagg_pred))
error<compute_train_metric>
gender_target_dict =(train.groupby(['gender'])['stroke'].agg(['mean'])).to_dict() ['mean']
gender_target_dict['Other'] = global_mean_stroke
train['gender_target_enc'] = train['gender'].replace(gender_target_dict)
 test['gender_target_enc'] = test['gender'].replace(gender_target_dict )
adareg = AdaBoostRegressor(DecisionTreeRegressor())
 adareg.fit(X_train,y_train)
 y_adareg_pred = adareg.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_adareg_pred))
error<compute_train_metric>
work_type_data = train[['work_type', 'stroke']].groupby(['work_type'], as_index=False ).mean().sort_values(by='stroke', ascending=False)
 work_type_data
gradient_reg = GradientBoostingRegressor()
gradient_reg.fit(X_train,y_train)
 y_gradient_pred = gradient_reg.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_gradient_pred))
error<compute_train_metric>
train['work_type'].value_counts()
xgreg = XGBRegressor()
xgreg.fit(X_train,y_train)
 y_xgreg_pred = xgreg.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_xgreg_pred))
error<compute_train_metric>
n = train.groupby('work_type' ).size()
mean_by_work_type = train.groupby('work_type')['stroke'].mean()
m = 10
work_type_target_dict =(( mean_by_work_type*n + global_mean_stroke*m)/(n+m)).to_dict()
train['work_type_smoothed_target_enc'] = train['work_type'].replace(work_type_target_dict)
 test['work_type_smoothed_target_enc'] = test['work_type'].replace(work_type_target_dict)
model_lgb = lgb.LGBMRegressor()
model_lgb.fit(X_train,y_train)
 y_lgb_pred = model_lgb.predict(X_test)
 error = np.sqrt(mean_squared_error(y_test,y_lgb_pred))
error<save_to_csv>
for work_type in train['work_type'].unique() :
train['work_type_is_{}'.format(work_type)] =(train['work_type'] == work_type)*1
test['work_type_is_{}'.format(work_type)] =(test['work_type'] == work_type)*1
<import_modules>
train = train.drop('Residence_type', axis=1)
 test = test.drop('Residence_type', axis=1 )
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold<compute_train_metric>
train['ever_married'] = train['ever_married'].replace({'Yes':1, 'No':0 })
 test['ever_married'] = test['ever_married'].replace({'Yes':1, 'No':0 } )
n_folds=5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42)
 rmse = np.sqrt(-cross_val_score(model, X_train,y_train,scoring='neg_mean_squared_error',cv=kf))
return rmse 
<choose_model_class>
features = [ 'age', 'hypertension', 'heart_disease', 'ever_married',
'avg_glucose_level',
'gender_target_enc', 'work_type_smoothed_target_enc']
X_embedded = TSNE(n_components=2, random_state=21 ).fit_transform(train[features] )
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5 )<choose_model_class>
def your_cross_validation_for_roc_auc(clf, X, y ,cv=5):
X = np.array(X.copy())
 y = np.array(y.copy())
 kf = KFold(n_splits=cv)
 kf.get_n_splits(X)
 scores = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
 prediction_on_this_fold = clf.predict_proba(X_test)[:,1]
score = roc_auc_score(y_score=prediction_on_this_fold, y_true=y_test)
 scores.append(score)
 return scores
model_xgb = XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )<choose_model_class>
models = [
SGDClassifier(loss='log', penalty = 'elasticnet', max_iter=50),
XGBClassifier(n_estimators=45, max_depth=3),
RandomForestClassifier() ,
]
models_names = dict(zip(models, ['svc', 'xgb', 'gnb']))
model_lgb1 = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 )<train_model>
all_features = [ 'age', 'hypertension', 'heart_disease', 'ever_married',
'avg_glucose_level', 'bmi',
'work_type_is_children',
'work_type_is_Private', 'work_type_is_Never_worked',
'work_type_is_Self-employed', 'work_type_is_Govt_job',
'gender_target_enc', 'work_type_smoothed_target_enc']
class AverageModel(BaseEstimator):
def __init__(self,models):
self.models = models
def fit(self, X, y):
for model in self.models:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([model.predict(X)for model in self.models])
 return np.mean(predictions, axis=1 )<compute_train_metric>
scores = {}
for model in models:
features_scores = {}
print(models_names[model])
 for f in all_features:
scores = your_cross_validation_for_roc_auc(model, train[[f]] , train['stroke'])
 print(f, np.mean(scores))
features_scores[f] = np.mean(scores)
average_model = AverageModel(models=(GBoost,model_xgb, model_lgb1))
score = rmsle_cv(average_model)
 score<train_model>
features_target = ['age', 'hypertension', 'heart_disease', 'ever_married', 'avg_glucose_level', 'bmi',
'work_type_smoothed_target_enc', 'gender_target_enc']
class stackingModel(BaseEstimator):
def __init__(self,base_model, meta_model, k_fold=5):
self.base_model = base_model
self.meta_model = meta_model
self.k_fold = k_fold
def fit(self,X,y):
kfold = KFold(n_splits=self.k_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0],len(self.base_models)))
 for i, model in enumerate(self.base_model):
for train_index,holdout_index in kfold.split(X,y):
model.fit(X[train_index],y[train_index])
 y_pred = model.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
return self
def predict(self,X):
meta_feature = np.column_stack([np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_model])
 return self.meta_model.predict(meta_features)
<train_model>
features_onehot = [ 'age', 'hypertension', 'heart_disease', 'ever_married',
'avg_glucose_level', 'bmi',
'work_type_is_children',
'work_type_is_Private', 'work_type_is_Never_worked',
'work_type_is_Self-employed', 'work_type_is_Govt_job',
'gender_target_enc']
base_model = [xgreg,gradient_reg,model_lgb]
def test1(X,y):
kfold = KFold(n_splits=5,shuffle=True)
 out_of_fold_predictions = np.zeros(( X.shape[0],len(base_model)))
 for i, model in enumerate(base_model):
for train_index,holdout_index in kfold.split(X,y):
model.fit(X.iloc[train_index],y.iloc[train_index])
 y_pred = model.predict(X.iloc[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
return out_of_fold_predictions
out_of_fold_predictions = test1(X_train,y_train)
 out_of_fold_predictions<train_model>
models = [
SGDClassifier(loss='log', penalty = 'elasticnet', max_iter=100),
XGBClassifier(n_estimators=75, max_depth=3),
RandomForestClassifier(n_estimators=75, max_depth=3),
]
models_names = dict(zip(models, ['svc', 'xgb', 'rfc']))
for model in models:
print(models_names[model])
 scores_target = your_cross_validation_for_roc_auc(model, train[features_target] , train['stroke'])
 scores_onehot = your_cross_validation_for_roc_auc(model, train[features_onehot] , train['stroke'])
 print(np.mean(scores_target), np.mean(scores_onehot))
<predict_on_test><EOS> 
best_model = XGBClassifier(n_estimators=75, max_depth=3)
 best_model.fit(train[features_target], train['stroke'])
 Y_pred = pd.DataFrame(best_model.predict_proba(test[features_target])) [1]
submission = pd.DataFrame({
"id": test["id"],
"stroke": Y_pred
})
 submission.to_csv(".. /working/submit.csv", index=False)
 submission.sample(10 )