<filename>main.py
#!/usr/bin/env python
import matplotlib
matplotlib.use("TkAgg")
from utils import preprocess, feature_engineer
import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import BatchNormalization
from sklearn.metrics import mean_squared_error


try:
  print("Reading in the dataset. This will take some time..")
  df = pd.read_csv('NYC_taxi.csv', parse_dates=['pickup_datetime'], nrows=500000)

except:
  print("""
    Dataset not found in your computer.
    """)
  quit()

df = preprocess(df)
df = feature_engineer(df)

df_prescaled = df.copy()
df_scaled = df.drop(['fare_amount'], axis=1)
df_scaled = scale(df_scaled)
cols = df.columns.tolist()
cols.remove('fare_amount')
df_scaled = pd.DataFrame(df_scaled, columns=cols, index=df.index)
df_scaled = pd.concat([df_scaled, df['fare_amount']], axis=1)
df = df_scaled.copy()

X = df.loc[:, df.columns != 'fare_amount']
y = df.fare_amount
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)


model=Sequential()
model.add(Dense(128, activation= 'relu', input_dim=X_train.shape[1]))
model.add(Dense(64, activation= 'relu'))
model.add(Dense(32, activation= 'relu'))
model.add(Dense(8, activation= 'relu'))
model.add(Dense(1))

#model.summary()
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
model.fit(X_train, y_train, epochs=1)

#train_pred = model.predict(X_train)
#train_rmse = np.sqrt(mean_squared_error(y_train, train_pred))
#test_pred = model.predict(X_test)
#test_rmse = np.sqrt(mean_squared_error(y_test, test_pred))
#print("Train RMSE: {:0.2f}".format(train_rmse))
#print("Test RMSE: {:0.2f}".format(test_rmse))
#print('------------------------')

def predict_random(df_prescaled, X_test, model):
  sample = X_test.sample(n=1, random_state=np.random.randint(low=0, high=10000))
  idx = sample.index[0]

  actual_fare = df_prescaled.loc[idx,'fare_amount']
  day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  day_of_week = day_names[df_prescaled.loc[idx,'day_of_week']]
  hour = df_prescaled.loc[idx,'hour']
  predicted_fare = model.predict(sample)[0][0]
  rmse = np.sqrt(np.square(predicted_fare-actual_fare))

  print("Trip Details: {}, {}:00hrs".format(day_of_week, hour))  
  print("Actual fare: ${:0.2f}".format(actual_fare))
  print("Predicted fare: ${:0.2f}".format(predicted_fare))
  print("RMSE: ${:0.2f}".format(rmse))

predict_random(df_prescaled, X_test, model)

<filename>main.py
#!/usr/bin/env python
import matplotlib
matplotlib.use("TkAgg")
from utils import preprocess, feature_engineer
import pandas as pd
import numpy as np
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import BatchNormalization
from sklearn.metrics import mean_squared_error


try:
  print("Reading in the dataset. This will take some time..")
  df = pd.read_csv('NYC_taxi.csv', parse_dates=['pickup_datetime'], nrows=500000)

except:
  print("""
    Dataset not found in your computer.
    """)
  quit()

df = preprocess(df)
df = feature_engineer(df)

df_prescaled = df.copy()
df_scaled = df.drop(['fare_amount'], axis=1)
df_scaled = scale(df_scaled)
cols = df.columns.tolist()
cols.remove('fare_amount')
df_scaled = pd.DataFrame(df_scaled, columns=cols, index=df.index)
df_scaled = pd.concat([df_scaled, df['fare_amount']], axis=1)
df = df_scaled.copy()

X = df.loc[:, df.columns != 'fare_amount']
y = df.fare_amount
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)


model=Sequential()
model.add(Dense(128, activation= 'relu', input_dim=X_train.shape[1]))
model.add(Dense(64, activation= 'relu'))
model.add(Dense(32, activation= 'relu'))
model.add(Dense(8, activation= 'relu'))
model.add(Dense(1))

#model.summary()
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
model.fit(X_train, y_train, epochs=1)

#train_pred = model.predict(X_train)
#train_rmse = np.sqrt(mean_squared_error(y_train, train_pred))
#test_pred = model.predict(X_test)
#test_rmse = np.sqrt(mean_squared_error(y_test, test_pred))
#print("Train RMSE: {:0.2f}".format(train_rmse))
#print("Test RMSE: {:0.2f}".format(test_rmse))
#print('------------------------')

def predict_random(df_prescaled, X_test, model):
  sample = X_test.sample(n=1, random_state=np.random.randint(low=0, high=10000))
  idx = sample.index[0]

  actual_fare = df_prescaled.loc[idx,'fare_amount']
  day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  day_of_week = day_names[df_prescaled.loc[idx,'day_of_week']]
  hour = df_prescaled.loc[idx,'hour']
  predicted_fare = model.predict(sample)[0][0]
  rmse = np.sqrt(np.square(predicted_fare-actual_fare))

  print("Trip Details: {}, {}:00hrs".format(day_of_week, hour))  
  print("Actual fare: ${:0.2f}".format(actual_fare))
  print("Predicted fare: ${:0.2f}".format(predicted_fare))
  print("RMSE: ${:0.2f}".format(rmse))

predict_random(df_prescaled, X_test, model)

#!/usr/bin/env python Dataset not found in your computer. #model.summary() #train_pred = model.predict(X_train) #train_rmse = np.sqrt(mean_squared_error(y_train, train_pred)) #test_pred = model.predict(X_test) #test_rmse = np.sqrt(mean_squared_error(y_test, test_pred)) #print("Train RMSE: {:0.2f}".format(train_rmse)) #print("Test RMSE: {:0.2f}".format(test_rmse)) #print('------------------------')