-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
94 lines (74 loc) · 3.12 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn.neighbors import KNeighborsClassifier
train_df = pd.read_excel('Data_Train.xlsx')
test_df = pd.read_excel('Data_Test.xlsx')
train_df['Rating'] = train_df['Rating'].replace(['NEW','Opening Soon','Temporarily Closed','-'],value=0)
test_df['Rating'] = test_df['Rating'].replace(['NEW','Opening Soon','Temporarily Closed','-'],value=0)
train_df['Rating'] = pd.to_numeric(train_df['Rating'])
test_df['Rating'] = pd.to_numeric(test_df['Rating'])
train_df['Votes'] = train_df['Votes'].replace(['-'],value=0)
test_df['Votes'] = test_df['Votes'].replace(['-'],value=0)
train_df['Votes'] = pd.to_numeric(train_df['Votes'])
test_df['Votes'] = pd.to_numeric(test_df['Votes'])
train_df['Reviews'] = train_df['Reviews'].replace(['-'],value=0)
test_df['Reviews'] = test_df['Reviews'].replace(['-'],value=0)
train_df['Reviews'] = pd.to_numeric(train_df['Reviews'])
test_df['Reviews'] = pd.to_numeric(test_df['Reviews'])
cuisines = []
for i in train_df['Cuisines']:
if i not in cuisines:
cuisines.append(i)
locations = []
for i in train_df['Location']:
if i not in locations:
locations.append(i)
c,l = [],[]
for x in train_df['Location']:
y = locations.index(x)
l.append(y)
for x in train_df['Cuisines']:
y = cuisines.index(x)
c.append(y)
ct,lt = [],[]
for x in test_df['Location']:
if x in locations:
y = locations.index(x)
lt.append(y)
else:
locations.append(x)
y = locations.index(x)
lt.append(y)
for x in test_df['Cuisines']:
if x in cuisines:
y = cuisines.index(x)
ct.append(y)
else:
cuisines.append(x)
y = cuisines.index(x)
ct.append(y)
train_df['Minimum_Order'] = train_df['Minimum_Order'].apply(lambda x:int(x.strip('₹')))
train_df['Average_Cost'] = train_df['Average_Cost'].apply(lambda x:(x.strip('₹')))
train_df['Average_Cost'] = train_df['Average_Cost'].apply(lambda x:int(x.replace(',','')) if x != 'for' else -99)
median = int(train_df['Average_Cost'].median())
train_df["Average_Cost"] = np.where(train_df["Average_Cost"] == 0, median,train_df['Average_Cost'])
train_df['Delivery_Time'] = train_df['Delivery_Time'].apply(lambda x:int(x.strip(' minutes')))
test_df['Minimum_Order'] = test_df['Minimum_Order'].apply(lambda x:int(x.strip('₹')))
test_df['Average_Cost'] = test_df['Average_Cost'].apply(lambda x:(x.strip('₹')))
test_df['Average_Cost'] = test_df['Average_Cost'].apply(lambda x:int(x.replace(',','')) if x != 'for' else -99)
median = int(test_df['Average_Cost'].median())
test_df["Average_Cost"] = np.where(test_df["Average_Cost"] == 0, median,test_df['Average_Cost'])
knn = KNeighborsClassifier(n_neighbors = 2)
lm = linear_model.LinearRegression()
X_train = train_df.drop(['Restaurant','Delivery_Time','Location','Cuisines'],axis=1)
X_train['Location'] = l
X_train['Cuisines'] = c
X_test = test_df.drop(['Restaurant','Location','Cuisines'],axis=1)
X_test['Location'] = lt
X_test['Cuisines'] = ct
Y_train = train_df['Delivery_Time'].values
knn.fit(X_train,Y_train)
lm.fit(X_train,Y_train)
print(knn.predict(X_test)[0:5]) #[30 30 30 45 45]
print(lm.predict(X_test)[0:5]) #[39.39377194 31.04529288 36.38719694 36.96162778 35.73898515]