0% found this document useful (0 votes)
14 views8 pages

Abhishek Pandey - BI Lab - Exp 3

Uploaded by

Anant Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Abhishek Pandey - BI Lab - Exp 3

Uploaded by

Anant Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment:1.

Aim: Write a program to implement Sales Revenue Prediction using Linear Regression
Software Required: Jupyter Notebook (Anaconda)
Description:
● Linear Regression: Linear regression is a type of supervised machine learning algorithm
that computes the linear relationship between a dependent variable and one or more
independent features. When the number of the independent feature is 1 then it is known as
Univariate Linear regression, and in the case of more than one feature, it is known as
multivariate linear regression. The goal of the algorithm is to find the best linear equation
that can predict the value of the dependent variable based on the independent variables.
The equation provides a straight line that represents the relationship between the
dependent and independent variables. The slope of the line indicates how much the
dependent variable changes for a unit change in the independent variable(s).

Implementation:
#Importing Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import mpl_toolkits
from sklearn import linear_model
%matplotlib inline
plt.style.use('ggplot')
import warnings

Name: Abhishek Pandey UID: 20BCS9188


warnings.filterwarnings("ignore")
#Creating DataSet
data = {'Marketing Budget (X) in Thousands' :
[127.4,364.4,150,128.7,285.9,200,303.3,315.7,169.8,104.9,297.7,256.4,249.1,323.1,223,235,200]
,'Actual Sales(Y) in Millions' :
[10.5,21.4,10,9.6,17.4,12.5,20,21,14.7,10.1,21.5,16.6,17.1,20.7,15.5,13.5,12.5]}
df = pd.DataFrame(data, columns = {'Marketing Budget (X) in Thousands','Actual Sales(Y) in Millions'})
df

Output:

# Visualizing the data using heatmap


sns.heatmap(df.corr(), cmap="YlGnBu", annot = True)
plt.show()

Name: Abhishek Pandey UID: 20BCS9188


plt.figure(figsize=(12,6))
plt.scatter(df['Marketing Budget (X) in Thousands'], df['Actual Sales(Y) in Millions'], color='red')
plt.title('Sales Vs Budget', fontsize=14)
plt.xlabel('Marketing Budget (k)', fontsize=14)
plt.ylabel('Actual Sales (m)', fontsize=14)
plt.grid(True)

plt.show()

Name: Abhishek Pandey UID: 20BCS9188


n_df = df.drop('Actual Sales(Y) in Millions',axis='columns')
n_df

sales = df['Actual Sales(Y) in Millions']

sales

Name: Abhishek Pandey UID: 20BCS9188


# Create linear regression object

lr = linear_model.LinearRegression()

lr.fit(n_df,sales)

#let's predict Sales for a given budget amount

lr.predict([[150]])

#let's generate model prediction for all budget amounts in our dataset

y_predict = lr.predict(n_df)

# Visualize the predicted amount as a line on the test set

#The scatter-plot with best-fit line looks like

plt.figure(figsize=(12,6))

plt.scatter(df['Marketing Budget (X) in Thousands'], df['Actual Sales(Y) in Millions'])

plt.plot(df['Marketing Budget (X) in Thousands'], y_predict, 'r')

plt.show()

Name: Abhishek Pandey UID: 20BCS9188


#Generate a file with list of sales predictions

# Intercept value

print("Intercept :",lr.intercept_)

# Slope value

print('Slope :',lr.coef_)

# Manually plotting the line with above Intercept and Slope

plt.figure(figsize=(12,6))

plt.scatter(df['Marketing Budget (X) in Thousands'], df['Actual Sales(Y) in Millions'])

plt.plot(df['Marketing Budget (X) in Thousands'], 0.05276727*df['Marketing Budget (X) in Thousands'] +


3.3524968264935975, 'r')

plt.show()

Name: Abhishek Pandey UID: 20BCS9188


plt.figure(figsize=(12,6))

x_ax = range(len(df['Actual Sales(Y) in Millions']))

plt.plot(x_ax, df['Actual Sales(Y) in Millions'], label="original")

plt.plot(x_ax,y_predict, label="predicted")

plt.title("Actual Sales and predicted data")

plt.legend()

plt.show()

Name: Abhishek Pandey UID: 20BCS9188


#proposed budget

from pandas import DataFrame

Stock_Market = {'Marketing Budget': [201,207,225,225,270,275,292,299,310,323,] }

b_df = DataFrame(Stock_Market,columns=['Marketing Budget'])

b_df

#Predict Sales for the forecasted budget amount


ps = lr.predict(b_df) ps

b_df['Forecasted Sales'] = ps b_df

Name: Abhishek Pandey UID: 20BCS9188

You might also like