DEPARTMENT OF COMPUTER ENGINEERING
Module-6 -Questions with Solution
Subject: Applied Data Science
Class: BE A/B/C Div: A/B/C Semester: VIII
Q.1 Explain how predictive modelling can be applied to the House Price Prediction
recommendation. (10 marks) May 2023.
1. Problem statement:
We can analyse existing real estate prices to predict the price of real estate. This can be very useful
in understanding the valuation of a property or a new development as many people get confused
about the prices while purchasing a property and often end up paying too much for a flat or a
house. The problem statement is to predict the sale price of a house, given the features of the
house. The features are the columns in the dataset, and the target variable is the SalePrice column.
The problem is a regression problem, as the target variable is continuous.
2. Description of dataset: We shall use some sample real estate data to predict real estate
prices. For e.g., the dataset may have attributes like category marking under construction or not,
RERA approved or not, Number of rooms, Type of property i.e.., 1 RK/1 BHK/2 BHK, Total area
of the house in square feet, Category marking ready to move or not, Category marking resale or
not, Address of the property, Longitude of the property, Latitude of the property etc.
3. Descriptive statistics and inferential statistics
It is important for us to get a good understanding of the characteristics of our data. The .info()
method can be used to get a quick snapshot of the current state of the dataset.
Descriptive statistics can be used to know more specifically what are the min, max, mean,
standard deviation, upper and lower bounds and count of all the variables within our dataset. This
information is important firstly for determining the general characteristics of our data, secondly,
for determining the outliers we have in our data and it will give also us a better idea as to how we
need to prepare our data for training later on.
4. Data Pre-processing
There could be some mistakes in the collected entries in the dataset like some null values, human
errors or some impractical values which we call as outliers. So to overcome these inaccuracies,
we need to Pre-process and clean the data from these clutter values. There is a high need of Data
Pre- processing because if the Data that we are providing to our model is accurate and faultless,
then only the model will be able to give precise estimations which are very close to the actual
value.
Handling missing values:
If there are missing values (NaN) in the dataset we can handle them by getting rid of them or
replacing them with some values like mean,median etc. Replacing Nan values with the median is
the best course of action especially when we have a smaller dataset and we don’t want to lose any
of our precious data. If we have a very large dataset it is fine to just drop the Nan values provided
there is only a small number of them. It is better to replace the Nan values with the median rather
then the mean as the median is less affected when the distribution of our dataset is skewed.
Handling outliers:
Outlier values can be removed from the dataset or replaced with less extreme values. There is
also one more way to deal with outliers that called capping! This is the optimal method of dealing
with outliers. Here we first identify the upper and lower bounds of our data. This is generally
done by calculating 1.5*IQR (Inter-quartile range) above and below the mean. All data points
that fall outside our upper and lower bounds are considered outliers and need to be replaced by the
upper or lower bound.
After the data has been cleaned and free from the outliers, Feature Engineering and Exploratory
Data Analysis have to be done.
Feature Scaling
One of the most important transformations you need to apply to your data is feature scaling. With
few exceptions, Machine Learning algorithms don’t perform well when the input numerical
attributes have very different scales.
There are two common ways to get all attributes to have the same scale: min-max
scaling and standardization.
5. Data visualization
It is quite useful to have a quick overview of different features distribution vs house price. Some
datasets have many features that could be a deterrent in prediction, by finding out which features
would give the best result and dropping those that wouldn't give the best results we can try to get
better accuracy. This could be done using scatter plots, correlation matrix etc.
6. Model Building
The features and target variable is defined.
Features (X): The columns that are inserted into our model will be used to make predictions.
Prediction (y): Target variable that will be predicted by the features.
Then the data is split into training and testing sets for the classification of the best fitting machine
learning model. The standard 80-20 split ratio is used, a typical ratio for this purpose; 80% of the
data is considered as a training set and 20% as a testing set. To allow the implementation of the
model, Scikit-Learn have to be imported. It is a Python Library which provides machine learning
algorithms for implementation and many more features for modeling. We are performing
Supervised Learning and to find out best model we will be implementing some regression
algorithms which are likely to do a precise estimation of prices. The model which gives least error
and most nearer value prediction will be our final model.
To test the results of different models and compare them, we will provide same input values for
all the models. Lets take an example of area Kasarvadavli in Thane and check price for 900 sqft.
house having 2 bedrooms and 2 baths and compare the price given by different algorithms.
Regression analysis is a type of predictive modeling technique that analyses the relation between
the target or dependent variable and independent variables in a dataset. It involves determining
the best fitting line that passes through all the data points in such a way that distance of the line
from each data point is minimal. For most accurate Predictions we are trying different Regression
techniques on given problem statement to find out best fitting model. This includes linear
regression, Support Vector Regressor and Decision Trees.
[Link] Linear Regression:
The main aim of Linear Regression model is to find the best fit linear line and the optimal
values of intercept and coefficients such that the error is minimized. Error is defined as the
difference between the actual value and Predicted value. The goal is to reduce this error or
difference. Linear Regression is of two types based on number of independent variables: Simple
and Multiple. Simple Linear Regression contains only one independent variable and the model
has to find the linear relationship between this and the dependent variable. Whereas, Multiple
Linear Regression contains more than one independent variables for the model to find the
relationship with the dependent variable.
Equation of Simple Linear Regression is, y=b0+b1*x
Where b0 is the intercept, b1 is coefficient or slope, x is the independent variable and y is the
dependent variable.
Equation of Multiple Linear Regression is, y =b0+b1*x1+b2*x2+b3*x3+….bn*xn
Where b0 is the intercept, b1,b2,b3,b4,bn are the coefficients or slopes of the independent
variables x1,x2,x3,x4,xn and y is the dependent variable.
Multiple Linear Regression is an extension of Simple Linear Regression and here we assume
that there is a linear relationship between a dependent variable Y and independent variables X
So now , we have train data , test data and labels for both we fit our train and test data into a
multiple linear regression model. We use sklearn (built in python library) and import linear
regression from it and then initialize Linear Regression to a variable reg.
After fitting our data to the model we can check the score of our data ie , prediction. in this case
say the prediction is 92%
[Link] Vector Regression:
Support Vector Regression (SVR) uses the same method as Support Vector Machine
(SVM) but for regression problems. In SVR, the straight line that is required to fit the data is
referred to as hyperplane. The objective of a SVR algorithm is to find a hyperplane in an n-
dimensional space that classifies the data points. The data points on either side of the hyperplane
that are closest to the hyperplane which are called Support Vectors.
The best fit line is the hyperplane that has the maximum number of points. Unlike other
Regression models that try to minimize the error between the real and predicted value, the SVR
tries to fit the best line within a threshold value. The threshold value is the distance between the
hyperplane and boundary line. The problem with SVR is that they are not suitable for large
datasets.
[Link] Tree Regression:
Decision Tree is a tree-structured algorithm with three types of nodes; Root Node, Interior node
and Leaf node. The Interior Nodes represent the features of a data set and the branches represent
the decision rules.
The Decision Tree Regressor observes features of an attribute and trains a model in the form of a
tree to predict data in the future to produce meaningful output. Decision tree Regressor learns
from the max depth, min depth of a graph and according to system analyses the data.
7. Model Evaluation
Cross-validation of different algorithms has proven to be a suitable method to find an acceptable
best fitting algorithm for the Model. After training the dataset on three different machine learning
model the outcome that has been extracted is as follows, the linear regression model performed
the best with the score of approximately 92%, followed by the SVM regression with an
approximate score of 91%, lastly the decision tree score almost 90% score when trained on a
dataset. Also, according to confusion matrix linear regression is giving nearly accurate predictions.
As the final decision to choose over these machine learning models the optimal choice had to be
the linear regression model and hence, that is the reason of using the same in the proposed
solution.
The Model has also proved that Location and square feet area plays an important role in deciding
the price of a property. This is helpful information for Sellers and buyers to act accordingly. The
GUI has provided Ease of access to the model, hence improving quality of accessibility.
8. Model Deployment
To deploy our machine learning model we need flask which is framework for deploying
functional webpage for our created model. There are more options for that but flask is one of the
effective and instant ways for creating UI for proposed machine learning model. It is also easier
to integrate Flask with the model. Flask allows us to create a UI for our model. Flask provides us
with tools, libraries and technologies that allow us to build a web application.
Once the Implementation is done the model is predicting us the price of the property (house) in
that particular location. We will deploy the model using Flask framework and create UI where
the user will enter the desired values and our model will predict the output. This is made possible
by using the python package for creating an API called Flask. For building the web application
and linking the model with the web application, first we need to extract our model into pickle and
json files and design webpage using HTML, CSS and JavaScript. With this the model is ready to
be displayed and make predictions on the web application.
In the future, the GUI can be made more attractive and interactive. It can also be turned into any
real estate sale website where sellers can give the details and house for sale and buyers can contact
according to the details given on the website.
To simplify it for the user, there can also be a recommending system to recommend real estate
properties to the user based on the predicted price. The current dataset only includes a few
locations of Thane city, expanding it to other cities and states of India is the future goal.
To make the system even more informative and user-friendly, Google maps can also be included.
This will show the neighbourhood amenities such as hospitals, schools surrounding a region of 1
km from the given location. This can also be included in making predictions since the presence
of such factors increases the price of real estate property.
Q.2 Explain how time series approach is used to forecast the demand of a product. (10
marks) May 2023.
1. Introduction to Time Series Forecasting
A time series is a sequence of data points measured at successive points in time, usually at equal
intervals (daily, monthly, quarterly, etc.). Demand forecasting using time series involves
analyzing past demand data to predict future product demand. The goal is to identify patterns
(like trends or seasonality) and build models to make accurate forecasts.
Applications
Inventory management
Production planning
Supply chain scheduling
Retail and e-commerce promotions
2. Components of Time Series
Component Description
Trend (T) Long-term upward or downward
movement in data
Seasonality (S) Repeating short-term cycles (e.g.,
increased sales in December)
Cyclic (C) Long-term fluctuations due to economic
or business cycles
Irregular (I) Random variations or noise
3. Time Series Forecasting Models
1. Naive Method
Assumes demand in the next period will be the same as the current period.
Formula: Ft+1 = Dt
2. Moving Average (MA)
Averages demand over a fixed number of past periods.
Formula (3-period MA): Ft+1 = (Dt + Dt-1 + Dt-2)/3
3. Exponential Smoothing
Weights recent observations more heavily.
Formula: Ft+1 = αDt + (1 - α)Ft
4. Trend Projection
Fits a straight line to data.
Formula: Yt = a + bt
5. ARIMA
Advanced model for data with trend, seasonality, and autocorrelation.
4. Steps in Time Series Demand Forecasting
Collect historical demand data
Plot the data to visualize patterns
Identify components (trend, seasonality, etc.)
Select an appropriate model based on data characteristics
Train the model using past data
Generate forecasts for future demand
Evaluate accuracy using metrics like MAPE, RMSE
5. Case study Example
1. Problem Definition
A retail company wants to predict the future demand of its best-selling product to improve
inventory management. The objective is to use historical sales data to forecast future
monthly demand accurately.
2. Data Collection
The company collects monthly demand data for the product over the last two years. For
example, the following table shows demand for four months:
Month | Demand
------|--------
Jan | 120
Feb | 130
Mar | 125
Apr | 135
3. Data Preparation
Data is cleaned to remove missing values and formatted for time series analysis. The
demand column is converted into a time-indexed series, and any seasonality or trend is
visualized through plotting.
4. Data Analysis and Modeling
The key components of time series are:
Component Description
Trend (T) Long-term upward or downward
movement in data
Seasonality (S) Repeating short-term patterns
Cyclic (C) Long-term economic or market cycles
Irregular (I) Random fluctuations
We use the 3-month Moving Average model:
Formula: Ft+1 = (Dt + Dt-1 + Dt-2)/3
Using the demand data for Feb, Mar, and Apr:
F_May = (130 + 125 + 135)/3 = 130
Forecasted demand for May = 130 units
5. Evaluation
The accuracy of the forecast is evaluated using error metrics such as Mean Absolute
Percentage Error (MAPE) or Root Mean Square Error (RMSE). The selected model
should minimize these errors for reliable forecasting.
6. Deployment
Once validated, the forecasting model is deployed into the company's inventory system. It
regularly updates forecasts based on new data and helps the supply chain team make
informed purchasing decisions.
7. Monitoring and Maintenance
The forecasting model is continuously monitored for accuracy and updated periodically to
adapt to changes in sales trends or seasonality.
Conclusion
Time series forecasting is a data-driven approach that helps businesses predict future product
demand using past patterns. By applying models like moving average, exponential smoothing, or
ARIMA, companies can make informed, timely decisions to optimize inventory, reduce
stockouts, and improve customer satisfaction.
Q.3 Write a note on fraud detection. (10 marks) May 2023.
1. Problem Definition
The primary goal is to detect fraudulent activities as early as possible—preferably in real time—
to prevent financial and reputational losses.
Example: In credit card fraud, unauthorized transactions must be flagged and blocked
immediately.
2. Data Collection
Data is collected from multiple sources, such as:
• Transaction logs (amount, time, location)
• Customer details and behavior history
• Device and browser fingerprints
• IP address and geolocation data
Example: A dataset with 1,00,000 credit card transactions, where only 1% are labeled as fraud.
3. Data Preparation
• Cleaning: Handle missing, incorrect, or duplicate values
• Balancing classes: Use techniques like SMOTE for class imbalance
• Feature engineering: Derive new variables like transaction frequency, average spend,
location shift
• Normalization: Especially helpful for distance-based models like LOF
4. Data Analysis and Modeling
Exploratory Data Analysis (EDA) is performed to discover fraud patterns.
Supervised Models (if labels are available):
• Logistic Regression
• Decision Trees / Random Forest
• SVM, XGBoost, Neural Networks
Unsupervised Models (when labels are unknown):
• Isolation Forest
• Local Outlier Factor (LOF)
here:
• reach-dist is the max of actual distance and k-distance of neighbor
• lrd is the local reachability density (inverse of average reach-distance)
High LOF score ⇒ higher chance of being fraud (outlier)
5. Evaluation
Metrics used to evaluate model performance:
• Precision: % of predicted frauds that are correct
• Recall: % of actual frauds that are detected (very important)
• F1-Score: Balance between precision and recall
• AUC-ROC: Measures how well the model separates fraud vs normal
In fraud detection, recall is crucial to minimize false negatives.
6. Deployment
The selected model is deployed into real-time systems such as banking or e-commerce platforms.
• Suspicious transactions are flagged instantly
• OTP, manual verification, or blocking can be triggered
Example: A transaction from a new country triggers an alert.
7. Monitoring and Maintenance
• Fraud tactics evolve constantly
• Models must be retrained on updated data
• Logs and human feedback help improve model accuracy and robustness
Conclusion
Fraud detection using the data science life cycle ensures a structured, scalable, and adaptable
approach.
It enhances security, minimizes financial loss, and increases customer trust by proactively
identifying threats.
Q.4 What are recommendation engines. Explain. (10 marks) Dec 2023.
Recommendation Engines, also known as Recommender Systems, are intelligent systems that
help users find relevant content by analyzing preferences, behaviors, or patterns. They are used
extensively in e-commerce, entertainment, social media, and online education to improve user
experience and engagement.
Examples:
Amazon recommending “Frequently Bought Together” products
Netflix suggesting shows similar to your recent views
Spotify generating daily playlists based on past listens
2. Types of Recommendation Systems
(i) Content-Based Filtering
Focuses on the attributes of items (genre, brand, price, etc.).
Recommends items that are similar to those the user has liked in the past.
Each item and user is described by a feature vector.
Example: If a user liked “Iron Man” (an action-sci-fi movie), the system recommends other
movies with similar genres or actors.
Techniques Used:
- Cosine similarity
- TF-IDF for textual content
- Classification models (like Naive Bayes, Decision Trees)
Formula for similarity between items A and B:
Sim(A,B) = (A·B) / (||A|| ||B||)
(ii) Collaborative Filtering
Based on the assumption: Users who agreed in the past will agree in the future.
Two main types:
- User-Based: Finds users similar to the target user and recommends what they liked.
- Item-Based: Recommends items that are liked by users who liked similar items.
Example: If User A and B both liked items 1 and 2, and User A liked item 3, then item 3 is
recommended to User B.
Approach:
- Build a User-Item Matrix (rows: users, columns: items)
- Fill missing values using:
- k-NN (k-nearest neighbors)
- Matrix factorization (e.g., Singular Value Decomposition - SVD)
Advantages:
- No need for item metadata
- Learns from actual user behavior
(iii) Hybrid Methods
Combine both content-based and collaborative filtering
Overcomes limitations like cold start (new user/item) and data sparsity
Example: Netflix uses both collaborative filtering (based on similar viewers) and content-based
filtering (based on genres and actors).
3. Architecture of a Recommendation Engine
Step-by-step workflow:
Data Collection – Gather user activity data: clicks, purchases, ratings
Data Storage – Store in databases or data warehouses
Data Processing – Clean and transform data (e.g., convert to numerical vectors)
Modeling – Apply similarity measures or machine learning models
Ranking – Score and rank items based on relevance
Presentation – Display personalized top-N recommendations
4. Evaluation of Recommendation Systems
Key Metrics:
Precision = (Recommended ∩ Relevant) / Recommended
Recall = (Recommended ∩ Relevant) / Relevant
F1-Score = Harmonic mean of Precision and Recall
RMSE / MAE = Errors between predicted and actual ratings
Coverage = % of items the system is able to recommend
Diversity = Variety in recommended items
Serendipity = How surprisingly useful the recommendations are
5. Challenges in Recommendation Systems
Challenge Description
Cold Start New users or items with no prior data
Data Sparsity Most users interact with only a few items
Scalability Must handle millions of users and
products
Bias Repeated exposure to the same type of
content
Privacy Need to protect user preferences and
behavior data
6. Applications of Recommendation Engines
Domain Use Case
E-commerce Product suggestions, upselling, cross-
selling
Entertainment Movie, music, and show
recommendations
Education Personalized learning paths and courses
News/Blogs Curated news feed based on reading
history
Social Media Friend suggestions, content feed
personalization
Conclusion
Recommendation engines play a critical role in personalizing the digital experience.
By leveraging user behavior, preferences, and content characteristics, they drive engagement,
increase sales, and improve customer satisfaction. They are a core application of machine learning
and data science, widely used across industries.