As ride-hailing services become more popular, being able to reliably estimate demand can help operators allocate drivers to clients more efficiently, reducing idle time, improving traffic congestion, and improving the passenger experience.
In this Blog, we are making a Flask based web application that will predict the Number of Weekly Rides using machine learning model.
For making this Appliation we mainly divide the project in two steps
- Training Model
- Deploy the trained model In the Flask app
Before discussed more make sure you have the following prerequisites installed in your personal machine (Computer/Laptop).
Flask
numpy
scikit-learn
pandas, pickle
For Model Traning, we import some necessary libraries. Read the dataset using the pandas library. after reading the dataset we generally divide the dataset into training and test split by using sklearn train_test_split.
Then trained a most popular machine learning model name Linear regression. Basically for uber ride prediction, the target labels Number of weekly riders are continuous values. So we need a regression-based model to predict the unknown outcomes.
After training the linear regression model we save the trained model in a pickle file. The code for the training model is given below.
Training Model Code
import pandas as pd # import numpy as np import pickle from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression data = pd.read_csv('uber_dataset.csv') # print(data.head()) data_x = data.iloc[:,0:-1].values data_y = data.iloc[:,-1].values print(data_y) X_train,X_test,y_train,y_test = train_test_split(data_x,data_y,test_size=0.3,random_state=0) reg = LinearRegression() reg.fit(X_train,y_train) print("Train Score:", reg.score(X_train,y_train)) print("Test Score:", reg.score(X_test,y_test)) pickle.dump(reg, open('model.pkl','wb')) model = pickle.load(open('model.pkl','rb')) print(model.predict([[80, 1770000, 6000, 85]]))
After training a model. It’s time to make the complete user-friendly web application to test our trained model. First, we import some necessary libraries then load the model using pickle again. We make a prediction function that takes input from the user and predicts the output
The code for testing model uisng flask app is shown below.
Flask code
import numpy as np from flask import Flask, request, jsonify, render_template import pickle import math app = Flask(__name__) model = pickle.load(open('model.pkl','rb')) @app.route('/') def home(): return render_template('index.html') @app.route('/predict', methods=['POST']) def predict(): int_features = [int(x) for x in request.form.values()] final_features = [np.array(int_features)] prediction = model.predict(final_features) output = round(prediction[0],2) return render_template('index.html',prediction_text="Number of Weekly Rides Should be {}".format(math.floor(output))) if __name__ == '__main__': app.run()
Outputs
Complete Project Download Link:
Thank you for the auspicious writeup. It in fact was a amusement account
it. Look advanced to far added agreeable
from you! By the way, how can we communicate?
Hey! Do you use Twitter? I’d like to follow you if that would be okay.
I’m definitely enjoying your blog and look forward to
new posts.
Your style is really unique compared to other
people I have read stuff from. Thanks for posting
when you have the opportunity, Guess I’ll just book mark this site.
What’s Happening i am new to this, I stumbled upon this
I have discovered It absolutely helpful and it has aided me out
loads. I am hoping to contribute & assist other customers like its aided me.
Great job.
Saved as a favorite, I love your blog!
Yes! Finally something about website.
I’m truly enjoying the design and layout of your blog.
It’s a very easy on the eyes which makes it much more enjoyable for me
to come here and visit more often. Did you hire out a developer to create
your theme? Exceptional work!
When I originally commented I appear to have clicked the -Notify me when new comments are
added- checkbox and now every time a comment is added I get 4 emails with the same comment.
Is there a means you are able to remove me from that service?
Thank you!
Hi there, everything is going nicely here and ofcourse every one is sharing facts,
that’s actually fine, keep up writing.
I don’t know if it’s just me or if perhaps everyone else
encountering problems with your website. It appears as though
some of the text within your content are running off the
screen. Can someone else please provide
feedback and let me know if this is happening to them too?
This might be a issue with my internet browser because
I’ve had this happen before. Thanks
You got a very great website, Gladiolus I discovered it through yahoo.