Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<train_on_grid>
warnings.filterwarnings('ignore' )
def modelfit(alg, dtrain, predictors, useTrainCV=True, cv_folds=5, early_stopping_rounds=50):
if useTrainCV:
xgb_param = alg.get_xgb_params()
xgtrain = xgb.DMatrix(dtrain[predictors].values, label=dtrain[target].values)
 cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=alg.get_params() ['n_estimators'], nfold=cv_folds,
early_stopping_rounds=early_stopping_rounds, metrics=['logloss'])
 alg.set_params(n_estimators=cvresult.shape[0])
 X_train, X_valid, y_train, y_valid = train_test_split(dtrain[predictors], dtrain[target], test_size=0.2)
 alg.fit(X_train, y_train, eval_metric='logloss')
 y_pred = alg.predict_proba(X_valid)[:,1]
result = log_loss(y_valid, y_pred)
 print(result)
 return result<save_to_csv>
train_data = pd.read_csv('.. /input/train.csv',nrows=20000)
 test_data = pd.read_csv('.. /input/test.csv')
 train_data.head()
test_data.head()
params = {
'base_score': 0.5,
'colsample_bylevel': 1,
'colsample_bytree': 0.8,
'learning_rate': 0.05,
'max_depth': 7,
'min_child_weight': 1,
'n_estimators': 200,
'nthread': -1,
'objective': 'binary:logistic',
'seed': 27,
'silent': True,
'subsample': 0.8
}
clf = xgb.XGBClassifier()
clf.set_params(**params)
 clf.fit(X_train, y_train)
 test.shot_made_flag = [i[1] for i in clf.predict_proba(X_test)]
test[['shot_id', 'shot_made_flag']].to_csv('sub.csv', index=False)
 predictions_train = clf.predict_proba(X_train)
 features_train = pd.DataFrame({'shot_made_flag': predictions_train[:, 1]})
 features_train[['shot_made_flag']].to_csv('features_train.csv', index=False )<load_from_csv>
def changeDataType(dataset):
dataset['passenger_count'] = dataset.passenger_count.astype('uint8')
 dataset['pickup_longitude'] = dataset.pickup_longitude.astype('float32')
 dataset['pickup_latitude'] = dataset.pickup_latitude.astype('float32')
 dataset['dropoff_longitude'] = dataset.dropoff_longitude.astype('float32')
 dataset['dropoff_latitude'] = dataset.dropoff_latitude.astype('float32')
 dataset['pickup_datetime'] = pd.to_datetime(arg=dataset['pickup_datetime'],format='%Y-%m-%d %H:%M:%S UTC')
 dataset.info()
changeDataType(train_data)
 print("--"*40)
 changeDataType(test_data)
 train_data['fare_amount'] = train_data.fare_amount.astype('float32')
data = pd.read_csv(".. /input/data.csv")
 print(check_output(["ls", ".. /input"] ).decode("utf8"))<compute_train_metric>
train_data.isnull().sum()
train_data = train_data.dropna(axis=0)
 train_data.isnull().sum()
def test_it_RF(data_test):
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 return cross_val_score(clf, data_test.drop('shot_made_flag', 1), data_test.shot_made_flag,
scoring='log_loss', cv=10 )<categorify>
p = pd.cut(train_data.fare_amount,3)
 p.value_counts()
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort()
new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 return new_field<feature_engineering>
train_data = train_data[train_data.fare_amount<400]
data['angle'] = 99
data.loc[~(data.loc_x==0), 'angle'] = \
np.arctan(data.loc[~(data.loc_x==0), 'loc_y'] / data.loc[~(data.loc_x==0), 'loc_x'])
 data.loc[data.loc_x == 0, 'angle'] = np.pi / 2<sort_values>
train_data = train_data.drop(( train_data[train_data['pickup_latitude']<-90] |(train_data[train_data['pickup_latitude']>90])).index,axis=0)
 train_data = train_data.drop(( train_data[train_data['pickup_longitude']<-180] |(train_data[train_data['pickup_longitude']>180])).index,axis=0)
 train_data = train_data.drop(( train_data[train_data['dropoff_longitude']<-180] |(train_data[train_data['dropoff_longitude']>180])).index,axis=0)
 train_data = train_data.drop(( train_data[train_data['dropoff_latitude']<-90] |(train_data[train_data['dropoff_latitude']>90])).index,axis=0 )
sort_encode(data, 'action_type' )<sort_values>
def degree_to_radion(degree):
return degree*(np.pi/180)
 def calculate_distance(pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude):
from_lat = degree_to_radion(pickup_latitude)
 from_long = degree_to_radion(pickup_longitude)
 to_lat = degree_to_radion(dropoff_latitude)
 to_long = degree_to_radion(dropoff_longitude)
 radius = 6371.01
lat_diff = to_lat - from_lat
long_diff = to_long - from_long
a = np.sin(lat_diff / 2)**2 + np.cos(degree_to_radion(from_lat)) * np.cos(degree_to_radion(to_lat)) * np.sin(long_diff / 2)**2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
return radius * c
sort_encode(data, 'angle_bin' )<set_options>
train_data['distance'] = calculate_distance(train_data.pickup_latitude, train_data.pickup_longitude, train_data.dropoff_latitude, train_data.dropoff_longitude )
mode = data.action_type_sort_enumerated.mode() [0]
data.action_type_sort_enumerated.fillna(mode, inplace=True)
 mode1 = data.angle_bin_sort_enumerated.mode() [0]
data.angle_bin_sort_enumerated.fillna(mode1, inplace=True )<define_variables>
test_data['distance'] = calculate_distance(test_data.pickup_latitude, test_data.pickup_longitude, test_data.dropoff_latitude, test_data.dropoff_longitude )
feature = ['action_type_sort_enumerated', 'shot_distance', 'angle_bin_sort_enumerated', \
'shot_made_flag']<filter>
p = pd.cut(train_data.distance,10)
 p.value_counts()
data_cv = data.loc[~data.shot_made_flag.isnull() , feature]<define_variables>
train_data = train_data.loc[train_data.distance<200]
[x for x in feature]<split>
train_data = train_data.drop(columns='key' )
feature1 = feature
feature2 = ['action_type_sort_enumerated','shot_distance','angle_bin_sort_enumerated','shot_id']
train = data.loc[~data.shot_made_flag.isnull() , feature1]
test = data.loc[data.shot_made_flag.isnull() , feature2]<save_to_csv>
test_data_key = test_data['key']
test_data = test_data.drop(columns='key' )
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 clf.fit(train.drop('shot_made_flag',axis=1), train.shot_made_flag)
 predictions = clf.predict_proba(test.drop('shot_id',axis=1))
submission = pd.DataFrame({'shot_id': test.shot_id, 'shot_made_flag': predictions[:, 1]})
 submission[['shot_id', 'shot_made_flag']].to_csv('submission_RF.csv', index=False )<set_options>
data = [train_data,test_data]
for i in data:
i['Year'] = i['pickup_datetime'].dt.year
i['Month'] = i['pickup_datetime'].dt.month
i['Date'] = i['pickup_datetime'].dt.day
i['Day of Week'] = i['pickup_datetime'].dt.dayofweek
i['Hour'] = i['pickup_datetime'].dt.hour
plt.style.use('seaborn')
<load_from_csv>
train_data[(train_data.distance>100)&(train_data.fare_amount<50)]
train_df=pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df=pd.read_csv('/kaggle/input/titanic/test.csv')
 print(train_df.head(5))
print("
")
 print(test_df.head(5))<feature_engineering>
train_data.groupby(['Month','Year'] ).count() ['fare_amount']
train_df["Pclass"].replace(1, "Upper", inplace=True)
 train_df["Pclass"].replace(2, "Middle", inplace=True)
 train_df["Pclass"].replace(3, "Lower", inplace=True)
 train_df["Age"].fillna(np.nanmedian(train_df["Age"]), inplace=True)
 bins = [0,10,20,30,40,50,60,70,80,100]
train_df["Age_bin"] = pd.cut(train_df['Age'], bins)
 train_df["with_family"] =(train_df["SibSp"] + train_df["Parch"])>0
train_df["Embarked"].fillna("S", inplace=True)
 train_df["Cabin"].fillna("Unknown", inplace=True)
 train_df["Title"] = train_df.Name.apply(lambda x: x.split(",")[1].split(".")[0].strip())
 title_list = ["Mr", "Miss", "Mrs", "Master", "Dr", "Rev", "Col"]
train_df.loc[~train_df["Title"].isin(title_list), "Title"] = "NA"
for i in range(0, len(train_df)) :
train_df.at[i, "Deck"] = " ".join(re.findall("[a-zA-Z]+", train_df.at[i, "Cabin"]))
train_df["Deck"].replace("B B", "B", inplace=True)
 train_df["Deck"].replace("B B B", "B", inplace=True)
 train_df["Deck"].replace("B B B B", "B", inplace=True)
 train_df["Deck"].replace("C C", "C", inplace=True)
 train_df["Deck"].replace("D D", "D", inplace=True)
 train_df["Deck"].replace("C C C", "C", inplace=True)
 train_df["Deck"].replace("F G", "F", inplace=True)
 train_df["Deck"].replace("F E", "E", inplace=True)
 train_df["Deck"].replace("T", "Unknown", inplace=True )<categorify>
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]
train_df_pre = train_df.drop(columns=["Name", "Ticket", "Cabin"])
 test_df_pre = test_df.drop(columns=[ "Name", "Ticket", "Cabin"])
 num_attribs = ["Age", "SibSp", "Parch", "Fare"]
cat_attribs = ["Pclass", "Embarked", "Deck", "Sex", "Title", "with_family", "Age_bin"]
col_transformer = ColumnTransformer([ 
("num", StandardScaler() , num_attribs), 
("cat", OneHotEncoder() , cat_attribs),
],
remainder="passthrough")
 train_array_transformed = col_transformer.fit_transform(train_df_pre)
 train_df_transformed = pd.DataFrame(data=train_array_transformed)
 column_names = num_attribs + list(col_transformer.named_transformers_['cat'].get_feature_names())+ ["PassengerId"] + ["Survived"]
train_df_transformed.columns = column_names
test_array_transformed = col_transformer.fit_transform(test_df_pre)
 test_df_transformed = pd.DataFrame(data=test_array_transformed)
 column_names = num_attribs + list(col_transformer.named_transformers_['cat'].get_feature_names())+ ["PassengerId"]
test_df_transformed.columns = column_names<find_best_model_class>
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]
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 lr = LogisticRegression(max_iter=2000)
 cv_lr = cross_val_score(lr, X_train, y_train, cv=3)
 print(cv_lr)
 print("mean accuracy: " + str(cv_lr.mean()))
lr.fit(X_train, y_train)
 lr.score(X_train, y_train)
 y_test = lr.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )<prepare_x_and_y>
train_data = train_data.drop(columns='pickup_datetime',axis=1)
 test_data = test_data.drop(columns='pickup_datetime',axis=1 )
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 lr = LogisticRegression()
param_grid = {"max_iter" : [500,1000,2000],
"penalty" : ["l1", "l2"],
"C" : np.logspace(-4, 4, 20),
"solver" : ["liblinear"]}
lr_clf = GridSearchCV(lr, param_grid=param_grid, cv=3, verbose=True, n_jobs=-1)
 best_lr_clf = lr_clf.fit(X_train, y_train)
 print("Best Logistic Regression Score: " + str(best_lr_clf.best_score_))
print("Best Parameter: " + str(best_lr_clf.best_params_))
y_test = lr_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )<find_best_model_class>
X = train_data.loc[:,train_data.columns != 'fare_amount']
y = train_data['fare_amount']
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 rnd_clf = RandomForestClassifier(n_estimators=500, n_jobs=-1)
 cv_rnd_clf = cross_val_score(rnd_clf, X_train, y_train, cv=5)
 print(cv_rnd_clf)
 print("mean accuracy: " + str(cv_rnd_clf.mean()))
rnd_clf.fit(X_train, y_train)
 rnd_clf.score(X_train, y_train)
 y_test = rnd_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )<prepare_x_and_y>
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 )
X_train = train_df_transformed.drop(columns=["PassengerId","Survived"])
 y_train = train_df_transformed["Survived"]
X_test = test_df_transformed.drop(columns=["PassengerId"])
 rf = RandomForestClassifier()
param_grid = {'n_estimators': [200,300,400,600,800,1000,1500],
'bootstrap': [True,False],
'max_depth': [3,5,10,20,50,75,100,None],
'max_features': ['auto'],
'min_samples_leaf': [1,2,4,6,10],
'min_samples_split': [2,5,10,20]}
rnd_clf = RandomizedSearchCV(rf, param_distributions=param_grid, n_iter=500, cv=3, verbose=True, n_jobs=-1)
 best_rnd_clf = rnd_clf.fit(X_train, y_train)
 print("Best Random Forest Score: " + str(best_rnd_clf.best_score_))
print("Best Parameter: " + str(best_rnd_clf.best_params_))
best_rnd_clf.score(X_train, y_train)
 y_test = best_rnd_clf.predict(X_test)
 print("Number of survivors predicted: " + str(sum(y_test)) )<save_to_csv>
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
export_df = pd.DataFrame()
export_df["PassengerId"] = test_df_transformed["PassengerId"].astype(int)
 export_df["Survived"] = y_test.astype(int)
 now = datetime.datetime.now()
name_add = "date_"+str(now.year)+"-"+str(now.month)+"-"+str(now.day)+"_time_"+str(now.hour)+"-"+str(now.minute)
 export_df.to_csv(f"random_forest_tuned_{name_add}.csv", index=False )<import_modules>
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
from sklearn.ensemble import ExtraTreesClassifier
import seaborn as sns
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import GridSearchCV<load_from_csv>
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
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 test = pd.read_csv('/kaggle/input/titanic/test.csv')
 union = [train, test]
passenger_id = []<feature_engineering>
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
for i, df in enumerate(union):
name = df['Name'].str.split('.', n=1, expand = True)
 name = name[1].str.split(expand = True)[0]
name.replace(['(\() ','(\)) '],'',regex=True, inplace = True)
 df['Name'] = name
del name
mean_age = df[['Name','Age']].groupby(['Name'] ).mean()
df = df.merge(mean_age, on='Name')
 df['Age'] = df['Age_x'].fillna(df['Age_y'])
 df = df.drop(['Age_x', 'Age_y', 'Name'], axis=1)
 df['Age'] = df['Age'].fillna(df['Age'].mean())
 age_buckets= [0,2,10,18,60,200]
age_labels = [0,1,2,3,4]
df['AgeGroup'] = pd.cut(df['Age'], bins=age_buckets, labels=age_labels, right=False)
 parch_buckets= [0,1,200]
parch_labels = [0,1]
df['Parch'] = pd.cut(df['Parch'], bins=parch_buckets, labels=parch_labels, right=False)
 sibsp_buckets= [0,1,2,200]
sibsp_labels = [0,1,2]
df['SibSp'] = pd.cut(df['SibSp'], bins=sibsp_buckets, labels=sibsp_labels, right=False ).astype(np.int8)
 df['Fare'] = df['Fare'].fillna(df['Fare'].mean())
 df['Pclass'] = df['Pclass'].astype(np.int8)
 df['Ability'] = df['Fare'] / df['Pclass']
df['Family'] = df['SibSp'].astype(np.int8)+ df['Parch'].astype(np.int8)+ 1
fare_buckets= [0,23,10000]
fare_labels = [0,1]
df['Fare'] = pd.cut(df['Fare'], bins=fare_buckets, labels=fare_labels, right=False)
 ab_buckets= [0,4,9,15,20,59,70,10000]
ab_labels = [0,1,2,3,4,5,6]
df['Ability'] = pd.cut(df['Ability'], bins=ab_buckets, labels=ab_labels, right=False)
 df = df.sort_values(by=['PassengerId'])
 passenger_id.append(df["PassengerId"])
 df['Sex'] = pd.get_dummies(df['Sex'])
 df['Fare'] = pd.get_dummies(df['Fare'])
 df['SibSp'] = pd.get_dummies(df['SibSp'])
 df['Parch'] = pd.get_dummies(df['Parch'])
 df = df.drop(['Embarked', 'PassengerId', 'Ticket', 'Age', 'Cabin'], axis=1)
 union[i] = df<prepare_x_and_y>
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
x_train = union[0].drop("Survived", axis=1)
 y_train = union[0]["Survived"]
x_test = union[1]
x_train.shape, y_train.shape, x_test.shape<compute_train_metric>
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
ex = ExtraTreesClassifier(random_state = 6, bootstrap=True, oob_score=True)
 ex.fit(x_train, y_train)
 y_pred = ex.predict(x_test)
 ex.score(x_train, y_train)
 score = round(ex.score(x_train, y_train)* 100, 2)
 print('Extremely Randomized Trees', score )<compute_test_metric>
for i,j in enumerate(x_train.head(1)) :
print('%s: %s' %(j, int(ex.feature_importances_[i]*100)) + '%' )<save_to_csv>
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
submission = pd.DataFrame({
"PassengerId": passenger_id[1],
"Survived": y_pred
})
 submission.to_csv('/kaggle/working/submission.csv', index=False)
<import_modules>
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
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 )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 train = train_data.copy()
test = test_data.copy()<drop_column>
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 )
train.drop(['PassengerId'],axis=1,inplace=True)
 test.drop(['PassengerId'],axis=1,inplace=True)
 pred = train_data['Survived']<count_missing_values>
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.isnull().sum()<count_missing_values>
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 )
train.isnull().sum()<count_missing_values>
average_model = AverageModel(models=(GBoost,model_xgb, model_lgb1))
score = rmsle_cv(average_model)
 score
test.isnull().sum()<count_missing_values>
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)
test.isnull().sum()<categorify>
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
sex1 = pd.get_dummies(train['Sex'])
 sex2 = pd.get_dummies(test['Sex'])
 train.drop(['Sex'],axis=1,inplace=True)
 test.drop(['Sex'],axis=1,inplace=True)
 train = pd.concat([train,sex1],axis=1)
 test = pd.concat([test,sex2],axis=1 )<drop_column>
meta_model = lgb.LGBMRegressor()
meta_model.fit(out_of_fold_predictions,y_train )
train.drop(['female'],axis=1,inplace=True)
 test.drop(['female'],axis=1,inplace=True )<set_options>
base_model = [xgreg,gradient_reg,model_lgb]
feature_data = np.column_stack([ np.column_stack([model.predict(test_data)for model in base_model] ).mean(axis=1)for base_models in base_model] )
plt.figure(figsize=(8, 5))
sns.set_style('whitegrid')
 sns.countplot(x = 'Pclass',hue='Survived',data=train_data,palette='deep' )<define_variables>
meta_y = meta_model.predict(feature_data)
 meta_y = np.exp(meta_y )
Fare_0 = []
Fare_1 = []
for i in range(0,891):
if train_data["Survived"][i] == 0:
Fare_0.append(train["Fare"][i])
 else:
Fare_1.append(train["Fare"][i] )<rename_columns>
submission = pd.DataFrame(
 {'key': test_data_key, 'fare_amount': meta_y},
columns = ['key', 'fare_amount'])
 submission.to_csv('submission.csv', index = False)
 meta_y