# ---
# 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
# ---

# # Author: <NAME>
#
# Department of Computer Science & Engineering, <br/>
# Rajshahi University of Engineering & Technology (RUET) <br/>
# Website: https://amithasanshuvo.github.io/ <br/>
# ResearchGate: https://www.researchgate.net/profile/Kazi_Amit_Hasan <br/>
# Linkedin: hhttps://www.linkedin.com/in/kazi-amit-hasan/<br/>
# Email: <EMAIL> <hr>

# ## Competition: HackerEarth Machine Learning challenge: Slashing prices for the biggest sale day
#
# ### Task: Predict the lowest price
#
# A leading global leader of e-commerce has over 150 million paid subscription users. One of the many perks of the subscription is the privilege of buying products at lower prices. For an upcoming sale, the organization has decided to promote local artisans and their products, to help them through these tough times. However, slashed prices may impact local artists.
#
# To not let discounts affect local artists, the company has decided to determine the lowest price at which a particular good can be sold. Your task is to build a predictive model using Machine Learning that helps them set up a lowest-pricing model for these products.
#
# You have to predict the Low_Cap_Price column.
#
# The dataset folder consists of the following files:
#
# - Train.csv: Contains training data [9798 x 9] that must be used to build the model
# - Test.csv: Contains test data [5763 x 8] to be predicted on
# - sample_submission.csv: Contains sample submission format with dummy values filled for test data

# Importing all the libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Loading the files
train = pd.read_csv("Dataset/Train.csv")
test = pd.read_csv("Dataset/Test.csv")

# Printing them
train.head()

# Changing the type
test['Demand'] = test['Demand'].astype('int64')


# # Changing the type
train['Demand'] = train['Demand'].astype('int64')

# Fixing the X_train and dropping columns
X_train = train.drop(['Item_Id', 'Date', 'Low_Cap_Price'],axis = 1)
X_train.columns

# Same process like above
X_test = test.drop(['Item_Id', 'Date'], axis = 1)
X_test.columns

# Setting the predict variabkle here and the shape
y_train = train['Low_Cap_Price']

X_train.shape

# Trying LR
from sklearn.linear_model import LinearRegression
clf = LinearRegression()
clf.fit(X_train, y_train)

preds = clf.predict(X_test)
preds

submission = pd.DataFrame({'Item_Id':test['Item_Id'], 'Low_Cap_Price':preds})
submission.to_csv('SumissionFile.csv', index=False)

# +
# Trying RF

from sklearn.ensemble import RandomForestRegressor
clf2 = RandomForestRegressor(bootstrap= True,max_depth=10, max_features=6, n_estimators=400,min_samples_leaf=6
                             ,min_samples_split=12, random_state=10,ccp_alpha=0,n_jobs=-1) 
clf2.fit(X_train,y_train)
# -

preds2 = clf2.predict(X_test)
preds2

submission = pd.DataFrame({'Item_Id':test['Item_Id'], 'Low_Cap_Price':preds2})
submission.to_csv('SubmissionFile.csv', index=False)

# +
# Trying Standart scaler
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler().fit(X_train)
rescaled_X_train = scaler.transform(X_train)

# +
# Trying Knn
from sklearn.neighbors import KNeighborsRegressor
clf3 = KNeighborsRegressor(algorithm='brute')

clf3.fit(X_train,y_train)
#from sklearn.neighbors import KNeighborsRegressor


# -

preds3 = clf3.predict(X_test)
preds3

submission = pd.DataFrame({'Item_Id':test['Item_Id'], 'Low_Cap_Price':preds3})
submission.to_csv('Submission Files.csv', index=False)

# trying svr
from sklearn.svm import SVR
clf4 = SVR(kernel = 'linear')
clf4.fit(X_train,y_train)


preds4 = clf4.predict(X_test)
preds4

submission = pd.DataFrame({'Item_Id':test['Item_Id'], 'Low_Cap_Price':preds4})
submission.to_csv('Submission Files.csv', index=False)

submission = pd.DataFrame({'Item_Id':test['Item_Id'], 'Low_Cap_Price':preds5})
submission.to_csv('Submission Files.csv', index=False)

# +
# Selecting the best params

from sklearn.model_selection import GridSearchCV
# Create the parameter grid based on the results of random search 
param_grid = {
    'bootstrap': [True],
    'max_depth': [10,50,80, 90, 100, 110],
    'max_features': [2, 3,5,6],
    'min_samples_leaf': [3, 4, 5],
    'min_samples_split': [8, 10, 12],
    'n_estimators': [100, 200,400, 300, 1000]
}
# Create a based model
rf = RandomForestRegressor()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf, param_grid = param_grid, 
                          cv = 10, n_jobs = -1, verbose = 2)
# -

grid_search.fit(X_train, y_train)

grid_search.best_params_


# +
# The settings by which I got the best result and ranked 32 out of 487 participants

from sklearn.ensemble import RandomForestRegressor
clf2 = RandomForestRegressor(bootstrap= True,max_depth=10, max_features=6, n_estimators=400,min_samples_leaf=5
                             ,min_samples_split=12, random_state=10) 
clf2.fit(X_train,y_train)

#This got me 99.84988 

StarcoderdataJupyterScriptsDedupFiltered