<jupyter_start><jupyter_text>This notebook is Final submission for the hackathon https://www.machinehack.com/hackathons/predicting_food_delivery_time_hackathon_by_ims_proschool/dataAlert: This notebook is an old notebook so I couldn't structure the preprocessing and model code.<jupyter_code>import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer
import numpy as np
from sklearn.model_selection import train_test_split
train_data = pd.read_excel("Data_Train.xlsx")
test_data =pd.read_excel("Data_Test.xlsx")
train_data.head(5)
test_data.head(5)
test_data.shape
train_data.shape
print(set(test_data.Location.unique())- set(train_data.Location.unique()))
print(set(train_data.Location.unique())- set(test_data.Location.unique()))
test_data.Location.nunique()
train_data.Location.nunique()
train_data.Restaurant.nunique()
test_data.Restaurant.nunique()
print(len(set(train_data.Restaurant.unique())-set(test_data.Restaurant.unique())))
print(len(set(test_data.Restaurant.unique())-set(train_data.Restaurant.unique())))
test_data.info()
train_data.info()
train_data.loc[train_data['Reviews'].str.contains("-")]
test_data.loc[test_data['Reviews'].str.contains("-")]
all_data = train_data.drop("Delivery_Time",axis=1).append(test_data,ignore_index=True)
all_data[:11094].tail(5)
train_data.tail(5)
all_data[11094:].head()
test_data.head(5)
all_data['Cuisines'] = all_data['Cuisines'].apply(lambda x:x.split(','))
all_data['Cuisines']= all_data['Cuisines'].apply(lambda x :[i.strip() for i in x] )
location = all_data['Location'].apply(lambda x:x.split(',')).apply(lambda x :[i.strip() for i in x])
mlb = MultiLabelBinarizer()
s= all_data['Cuisines']
cuisines = pd.DataFrame(mlb.fit_transform(s),columns=mlb.classes_)
mlb1 = MultiLabelBinarizer()
location_encoded = pd.DataFrame(mlb1.fit_transform(location),columns=mlb1.classes_)
cuisines['Chinese'].sum()
pd.get_dummies(pd.DataFrame(all_data.Cuisines.values.tolist()),prefix_sep='',prefix='').sum(level=0,axis=1)['Chinese'].sum()
train_data[train_data['Cuisines'].str.contains('Chinese')].count()
location_encoded.head()
all_data.info()
all_data.loc[~all_data['Average_Cost'].str.contains('₹'),'Average_Cost']='₹50'
all_data.loc[~all_data['Minimum_Order'].str.contains('₹')]
all_data['Average_Cost'] = all_data['Average_Cost'].str.replace('₹','').str.replace(',','').astype(int)
all_data['Minimum_Order'] = all_data['Minimum_Order'].str.replace('₹','').str.replace(',','').astype(int)
all_data.Reviews.value_counts()
all_data.Reviews = all_data.Reviews.str.replace('-','0').astype(int)
all_data.Votes = all_data.Votes.str.replace('-','0').astype(int)
all_data.info()
all_data.Rating.value_counts()
all_data.Rating = all_data.Rating.str.replace('-','0.0').str.replace('Opening Soon','0.0'
                                                  ).str.replace('Temporarily Closed','0.0'
                                                               ).str.replace('NEW','0.0')
all_data.Rating = all_data.Rating.astype(float)
all_data.info()
all_data_encoded = pd.concat([pd.concat([all_data,location_encoded],axis=1),cuisines],axis=1)
all_data_encoded.info()
all_data_restraunt_encoded = pd.concat([all_data_encoded,pd.get_dummies(all_data_encoded.Restaurant)],axis=1)
all_data_encoded = all_data_encoded.drop(['Restaurant','Location','Cuisines'],axis=1)
all_data_restraunt_encoded.info()
all_data_restraunt_encoded = all_data_restraunt_encoded.drop(['Restaurant','Location','Cuisines'],axis=1)
pd.read_excel("Sample_Submission.xlsx").head(5)
train_data['Delivery_Time'].value_counts()
#target = train_data['Delivery_Time']
factor = pd.factorize(train_data['Delivery_Time'])
definitions = factor[1]
target = pd.Series(factor[0])

all_data_encoded['cuisines_count']=all_data.Cuisines.apply(lambda x:len(x))
all_data_encoded.to_csv('All_data_encoded.csv',index=False)<jupyter_output><empty_output><jupyter_text>Model Building<jupyter_code>from sklearn.model_selection import KFold,cross_val_score
from sklearn.ensemble import RandomForestClassifier

X_train, X_test, y_train, y_test = train_test_split(all_data_encoded[0:11094], target, test_size = 0.25, random_state = 21)
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 42)
classifier.fit(X_train, y_train)
y_submission = classifier.predict(all_data_encoded[11094:])
pd.DataFrame({'Delivery_Time':np.vectorize(reversefactor.get)(y_submission)}).to_excel(
    "Submissions/First_submission_rfc.xlsx",index=False)
def plot_confusion_matrix(classifier,X,y):
    X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25, random_state = 21)
    classifier.fit(X_train, y_train)
    # Predicting the Test set results
    y_pred = classifier.predict(X_test)
    #Reverse factorize (converting y_pred from 0s,1s and 2s to Iris-setosa, Iris-versicolor and Iris-virginica
    reversefactor = dict(zip(range(7),definitions))
    y_test = np.vectorize(reversefactor.get)(y_test)
    y_pred = np.vectorize(reversefactor.get)(y_pred)
    # Making the Confusion Matrix
    print(pd.crosstab(y_test, y_pred, rownames=['Actual duration'], colnames=['Predicted duration']))
# Predicting the Test set results
y_pred = classifier.predict(X_test)
#Reverse factorize (converting y_pred from 0s,1s and 2s to Iris-setosa, Iris-versicolor and Iris-virginica
reversefactor = dict(zip(range(7),definitions))
y_test = np.vectorize(reversefactor.get)(y_test)
y_pred = np.vectorize(reversefactor.get)(y_pred)
# Making the Confusion Matrix
print(pd.crosstab(y_test, y_pred, rownames=['Actual duration'], colnames=['Predicted duration']))
import lightgbm as lgbm
from sklearn.ensemble import AdaBoostClassifier,ExtraTreesClassifier,BaggingClassifier,GradientBoostingClassifier
gbc = GradientBoostingClassifier(n_estimators=500)
plot_confusion_matrix(gbc,all_data_encoded[0:11094], target)
lgbc = lgbm.LGBMClassifier(num_leaves=100,    learning_rate=0.15,
    n_estimators=900)
#great
plot_confusion_matrix(lgbc,all_data_encoded[0:11094], target)
#okayish
plot_confusion_matrix(bgc,all_data_encoded[0:11094], target)
pd.DataFrame({'Delivery_Time':np.vectorize(reversefactor.get)(lgbc.predict(all_data_encoded[11094:]))}).to_excel(
    "Submissions/Second_submission_lgbc_est_900_lr_15.xlsx",index=False)
abc = AdaBoostClassifier()
etc = ExtraTreesClassifier()
bgc = BaggingClassifier()

plot_confusion_matrix(abc,all_data_encoded[0:11094], target)
plot_confusion_matrix(etc,all_data_encoded[0:11094], target)
pd.DataFrame({'Delivery_Time':np.vectorize(reversefactor.get)(lgbc.predict(all_data_encoded[11094:]))}).to_excel(
    "Submissions/Second_submission_rfc.xlsx",index=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup