This article was published as a part of the Data Science Blogathon.
Deep learning is the subfield of machine learning which uses a set of neurons organized in layers. A deep learning model consists of three layers: the input layer, the output layer, and the hidden layers. Deep learning offers several advantages over popular machine learning algorithms like k nearest neighbour, support vector machine, linear regression, etc. Unlike machine learning algorithms, deep learning models can create new features from a limited set of information and perform advanced analysis. A deep learning model can learn far more complex features than machine learning algorithms. However, despite its advantages, it also brings several challenges. These challenges include the need for a large amount of data and specialized hardware like GPUs and TPUs.
In this article, we will be creating a deep learning regression model to predict home prices using the famous Boston home price prediction dataset. Not limited to that, we will also compare its results with some renowned machine learning algorithms in various terms.
The dataset consists of 506 rows with 13 features and a target column, the price column. The dataset is readily available on the internet, and you can use this link to download it. Or it can also be loaded using Keras, as you will see in the below steps.
import pandas as pd import numpy as np # import tensorflow as tf from tensorflow import keras from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
(train_features, train_labels), (test_features, test_labels) = keras.datasets.boston_housing.load_data() pd.DataFrame(train_features).head()
If we load the data from Keras, we will get data in a NumPy array. So, to better look at it, we will convert it to a pandas data frame and see the head of the data frame.
Now we have seen what the dataset looks like; we can start building the deep learning regression model. We will use the TensorFlow library to create the model. As the dataset is not very huge, we can limit the number of layers in the deep learning model and save time. We will use two fully connected layers with the relu activation. The relu activation outputs the input directly if it is greater than 0; otherwise returns zero.
model = keras.Sequential() model.add(Dense(20, activation='relu', input_shape=[len(train_features[0])])) model.add(Dense(1)) # model.compile(optimizer= 'adam', loss='mse', metrics=['mse']) history = model.fit(train_features, train_labels, epochs=20, verbose=0)
In the above code, we have created a feed-forward neural network. We trained the model for 20 epochs and used mean squared error as the loss function with adam optimizer. Now we can predict the results on the test data and compare it with actual values.
from sklearn.metrics import mean_squared_error as mse pred = model.predict(test_features) mse(pred, test_labels)
You can use other values sets like mean absolute error instead of mean squared error or RMS optimizer instead of adam optimizer. You can also tweak the learning rate and number of epochs to achieve better results.
Our deep learning regression model is completed, and as promised, we will compare its results with some popular machine learning algorithms.
Firstly, we will train the famous K nearest neighbour regressor data. The algorithm is popular for classification problems but also gives fair results on regression tasks. It calculates the distance between examples present in the training and test sets. And then makes a prediction based on K examples, closest to the problem.
from sklearn.neighbors import KNeighborsRegressor as KNN model_k = KNN(n_neighbors=3) model_k.fit(train_features,train_labels)
pred_k = model_k.predict(test_features) mse(pred_k, test_labels)
A supervised learning algorithm expects a linear relationship between the input and the output variable. It works on the formula Y = a +bX. Here, X is the explanatory variable, Y is the dependent variable, and b is the slope of the best fit line.
from sklearn.linear_model import LinearRegression model_l = LinearRegression() model_l.fit(train_features, train_labels)
pred_l = model_l.predict(test_features) mse(pred_l, test_labels)
It is a supervised learning algorithm that finds a hyperplane in N-dimensional space and classifies the data points distinctly. A hyperplane is the best line to separate classes and segregate them into N-dimensional space. SVM can also be used with data that is not linearly separable.
from sklearn.svm import SVR model_s = SVR(C=1.0) model_s.fit(train_features, train_labels)
pred_s = model_s.predict(test_features) mse(pred_s, test_labels)
Sklearn also provides a regularization parameter C along with SVM. Regularization prevents the model from learning many complicated features that can result in overfitting the data.
Now comes the exciting part where we will compare every model we have created so far. We will compare the models based on root mean square error.
Model | Algorithm | MSE(Mean Squared Error) | Time-taken to train(s) |
model | Feed Forward Neural Network | 119.768 | 2.929 |
model_k | KNN | 41.428 | 0.008 |
model_l | Linear Regression | 23.195 | 0.063 |
model_s | SVM | 66.345 | 0.305 |
From the table, we can derive several conclusions. With a more significant error, the deep learning model took more time to train than the machine learning algorithm. This might be due to the simplicity of the architecture or the lack of training data. The linear regression model gives the slightest mistake, which means a perfect linear relationship between the input and the target variable. I also trained SVM without the regularisation parameter, showing almost the same result. This means all the features in the dataset correlated with the target variable.
Deep learning offers several advantages over machine learning but can’t replace it with simple problems. This article created regression models using both deep learning and simple machine learning algorithms. We saw that training a deep learning model might not be the best choice every time from the results. For the dataset we chose, even simpler machine learning algorithms outperformed. Hence, we can conclude that deep learning should be used only when the simple machine learning algorithms fail to provide satisfactory results.
I hope you enjoyed reading this article!
For more articles – click here.
Please find me on LinkedIn.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.
Hi there. Writing and exploring are some of my hobbies. I love Machine learning because of its endless applications and scope for improvement. I enjoy problem-solving and learning about new things. I believe to learn any new skill one should have the will to learn it. Ask the right questions and the rest, Google search will take care of it. In my free time, I like listening to music and jamming on my guitar.
You can connect with me on LinkedIn, and send me any suggestions or questions. I'll be happy to reply.
Keep Learning
Optimizers in Deep Learning: A Detailed Guide
Stock Market Prediction Using Machine Learning
Machine Learning Algorithms
Approaching Regression with Neural Networks Usi...
Car Price Prediction – Machine Learning v...
A Walk-through of Regression Analysis Using Art...
Neural Network for Regression with Tensorflow
A Quick Overview of Regression Algorithms in Ma...
Deep Learning vs Machine Learning – the e...
Getting into Deep Learning? Here are 5 Things y...
We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.
Show details
This site uses cookies to ensure that you get the best experience possible. To learn more about how we use cookies, please refer to our Privacy Policy & Cookies Policy.
It is needed for personalizing the website.
Expiry: Session
Type: HTTP
This cookie is used to prevent Cross-site request forgery (often abbreviated as CSRF) attacks of the website
Expiry: Session
Type: HTTPS
Preserves the login/logout state of users across the whole site.
Expiry: Session
Type: HTTPS
Preserves users' states across page requests.
Expiry: Session
Type: HTTPS
Google One-Tap login adds this g_state cookie to set the user status on how they interact with the One-Tap modal.
Expiry: 365 days
Type: HTTP
Used by Microsoft Clarity, to store and track visits across websites.
Expiry: 1 Year
Type: HTTP
Used by Microsoft Clarity, Persists the Clarity User ID and preferences, unique to that site, on the browser. This ensures that behavior in subsequent visits to the same site will be attributed to the same user ID.
Expiry: 1 Year
Type: HTTP
Used by Microsoft Clarity, Connects multiple page views by a user into a single Clarity session recording.
Expiry: 1 Day
Type: HTTP
Collects user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
Expiry: 2 Years
Type: HTTP
Use to measure the use of the website for internal analytics
Expiry: 1 Years
Type: HTTP
The cookie is set by embedded Microsoft Clarity scripts. The purpose of this cookie is for heatmap and session recording.
Expiry: 1 Year
Type: HTTP
Collected user data is specifically adapted to the user or device. The user can also be followed outside of the loaded website, creating a picture of the visitor's behavior.
Expiry: 2 Months
Type: HTTP
This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the website is doing. The data collected includes the number of visitors, the source where they have come from, and the pages visited in an anonymous form.
Expiry: 399 Days
Type: HTTP
Used by Google Analytics, to store and count pageviews.
Expiry: 399 Days
Type: HTTP
Used by Google Analytics to collect data on the number of times a user has visited the website as well as dates for the first and most recent visit.
Expiry: 1 Day
Type: HTTP
Used to send data to Google Analytics about the visitor's device and behavior. Tracks the visitor across devices and marketing channels.
Expiry: Session
Type: PIXEL
cookies ensure that requests within a browsing session are made by the user, and not by other sites.
Expiry: 6 Months
Type: HTTP
use the cookie when customers want to make a referral from their gmail contacts; it helps auth the gmail account.
Expiry: 2 Years
Type: HTTP
This cookie is set by DoubleClick (which is owned by Google) to determine if the website visitor's browser supports cookies.
Expiry: 1 Year
Type: HTTP
this is used to send push notification using webengage.
Expiry: 1 Year
Type: HTTP
used by webenage to track auth of webenagage.
Expiry: Session
Type: HTTP
Linkedin sets this cookie to registers statistical data on users' behavior on the website for internal analytics.
Expiry: 1 Day
Type: HTTP
Use to maintain an anonymous user session by the server.
Expiry: 1 Year
Type: HTTP
Used as part of the LinkedIn Remember Me feature and is set when a user clicks Remember Me on the device to make it easier for him or her to sign in to that device.
Expiry: 1 Year
Type: HTTP
Used to store information about the time a sync with the lms_analytics cookie took place for users in the Designated Countries.
Expiry: 6 Months
Type: HTTP
Used to store information about the time a sync with the AnalyticsSyncHistory cookie took place for users in the Designated Countries.
Expiry: 6 Months
Type: HTTP
Cookie used for Sign-in with Linkedin and/or to allow for the Linkedin follow feature.
Expiry: 6 Months
Type: HTTP
allow for the Linkedin follow feature.
Expiry: 1 Year
Type: HTTP
often used to identify you, including your name, interests, and previous activity.
Expiry: 2 Months
Type: HTTP
Tracks the time that the previous page took to load
Expiry: Session
Type: HTTP
Used to remember a user's language setting to ensure LinkedIn.com displays in the language selected by the user in their settings
Expiry: Session
Type: HTTP
Tracks percent of page viewed
Expiry: Session
Type: HTTP
Indicates the start of a session for Adobe Experience Cloud
Expiry: Session
Type: HTTP
Provides page name value (URL) for use by Adobe Analytics
Expiry: Session
Type: HTTP
Used to retain and fetch time since last visit in Adobe Analytics
Expiry: 6 Months
Type: HTTP
Remembers a user's display preference/theme setting
Expiry: 6 Months
Type: HTTP
Remembers which users have updated their display / theme preferences
Expiry: 6 Months
Type: HTTP
Used by Google Adsense, to store and track conversions.
Expiry: 3 Months
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
Save certain preferences, for example the number of search results per page or activation of the SafeSearch Filter. Adjusts the ads that appear in Google Search.
Expiry: 2 Years
Type: HTTP
These cookies are used for the purpose of targeted advertising.
Expiry: 6 Hours
Type: HTTP
These cookies are used for the purpose of targeted advertising.
Expiry: 1 Month
Type: HTTP
These cookies are used to gather website statistics, and track conversion rates.
Expiry: 1 Month
Type: HTTP
Aggregate analysis of website visitors
Expiry: 6 Months
Type: HTTP
This cookie is set by Facebook to deliver advertisements when they are on Facebook or a digital platform powered by Facebook advertising after visiting this website.
Expiry: 4 Months
Type: HTTP
Contains a unique browser and user ID, used for targeted advertising.
Expiry: 2 Months
Type: HTTP
Used by LinkedIn to track the use of embedded services.
Expiry: 1 Year
Type: HTTP
Used by LinkedIn for tracking the use of embedded services.
Expiry: 1 Day
Type: HTTP
Used by LinkedIn to track the use of embedded services.
Expiry: 6 Months
Type: HTTP
Use these cookies to assign a unique ID when users visit a website.
Expiry: 6 Months
Type: HTTP
These cookies are set by LinkedIn for advertising purposes, including: tracking visitors so that more relevant ads can be presented, allowing users to use the 'Apply with LinkedIn' or the 'Sign-in with LinkedIn' functions, collecting information about how visitors use the site, etc.
Expiry: 6 Months
Type: HTTP
Used to make a probabilistic match of a user's identity outside the Designated Countries
Expiry: 90 Days
Type: HTTP
Used to collect information for analytics purposes.
Expiry: 1 year
Type: HTTP
Used to store session ID for a users session to ensure that clicks from adverts on the Bing search engine are verified for reporting purposes and for personalisation
Expiry: 1 Day
Type: HTTP
Cookie declaration last updated on 24/03/2023 by Analytics Vidhya.
Cookies are small text files that can be used by websites to make a user's experience more efficient. The law states that we can store cookies on your device if they are strictly necessary for the operation of this site. For all other types of cookies, we need your permission. This site uses different types of cookies. Some cookies are placed by third-party services that appear on our pages. Learn more about who we are, how you can contact us, and how we process personal data in our Privacy Policy.
Edit
Resend OTP
Resend OTP in 45s