# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# #### - IMPORTING THE NEEDED PYTHON MODULES AND DATA SETS INTO THE WORKSPACE

# +
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score, KFold, GridSearchCV

train = pd.read_csv('train_copy.csv')
test = pd.read_csv('test_copy.csv')
test_ID = pd.read_csv('test.csv')


print(train.shape)
print(test.shape)
# -

# #### - SPLITTING THE DATASET INTO X AND y FOR TRAINING OUR ML ALGORITHMS

# +
# WE ARE DROPPING THE 'target' pd series(column) FROM X 
X = train.drop('target', axis=1)

y = train['target']

ID = test_ID['ward']

print("Shape of X", X.shape)
print("Shape of y", y.shape)
print("Shape of ID", ID.shape)


# -

# #### - IMPORTING OUR MODELS (ML ALGORITHMS) I WANT TO TRAIN WITH THE DATASET

from xgboost import XGBRegressor
from catboost import CatBoostRegressor
from mlxtend.regressor import StackingRegressor

# #### -LETS IMPLEMENT SOME FORM OF STACKING USING THE 'StackingRegressor' ALGORITHM

# +
# CREATING THE ML ALGORITHMS WE WANT TO STACK TOGETHER
CAT1 = CatBoostRegressor(metric_period=50)
CAT2 = CatBoostRegressor(metric_period=20, eval_metric='RMSE')

# CREATING A XGB ALGORITM TO SERVE AS OUT META-REGRESSOR (it aggregates prediction from the stacked models)
XG = XGBRegressor()

# CREATING PUT STACKED MODEL USING "StackingRegressor"
model = StackingRegressor(regressors= [CAT1, CAT2], meta_regressor= XG)


# -

# #### - TO MAKE THIS FANCY, I DEFINE A FUNCTION TO OUTPUT OUR PREDICTED VALUES INTO A CSV FILE

def output_csv(file_name, algorithm = model, X_data = X, y_data= y, ID_column = ID, test_data = test):
    algorithm.fit(X_data, y_data)

    pred = algorithm.predict(test_data)
    

    output = pd.DataFrame({'ward' : ID_column,
                           'target' : pred})

    output.to_csv(file_name, index=False)

# #### - OKAY LET US GENERATE SOME PREDICTIONS

# NOTE WE ONLY SUPPLIED ONE ARGUMENT...BECAUSE THE OTHER ARGUMENTS HAVE DEFAULT VALUES ATTACHED TO THEM
output_csv("Stacking_Prediction.csv")

# ## - THAT ALL FOR NOW
# ## - FEEL FREE TO MAKE PULL REQUESTS, FORK, CLONE AND DOWNLOAD THIS REPO AND IMPROVE ON THIS MEAGRE MODEL OF MINE...vielen dank (THANK YOU!!!)



StarcoderdataJupyterScriptsDedupFiltered