0% found this document useful (0 votes)
20 views6 pages

Sales Prediction with Linear Regression

This document summarizes code for predicting sales using linear regression. It loads advertising data, splits it into training and test sets, fits a linear regression model to predict sales based on TV, radio and newspaper features in the training set, makes predictions on the test set, and evaluates the model's accuracy using various metrics like MAE, RMSE and R-squared.

Uploaded by

Sourabh Yadav
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)
20 views6 pages

Sales Prediction with Linear Regression

This document summarizes code for predicting sales using linear regression. It loads advertising data, splits it into training and test sets, fits a linear regression model to predict sales based on TV, radio and newspaper features in the training set, makes predictions on the test set, and evaluates the model's accuracy using various metrics like MAE, RMSE and R-squared.

Uploaded by

Sourabh Yadav
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

6/17/23, 11:49 AM SALES PREDICTION5.

ipynb - Colaboratory

#importing libraries
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

#dataframe
df=pd.read_csv('/content/[Link]')

df

Unnamed: 0 TV Radio Newspaper Sales

0 1 230.1 37.8 69.2 22.1

1 2 44.5 39.3 45.1 10.4

2 3 17.2 45.9 69.3 9.3

3 4 151.5 41.3 58.5 18.5

4 5 180.8 10.8 58.4 12.9

... ... ... ... ... ...

195 196 38.2 3.7 13.8 7.6

196 197 94.2 4.9 8.1 9.7

197 198 177.0 9.3 6.4 12.8

198 199 283.6 42.0 66.2 25.5

199 200 232.1 8.6 8.7 13.4

200 rows × 5 columns

[Link]

(200, 5)

[Link]()

[Link] 1/7
6/17/23, 11:49 AM SALES [Link] - Colaboratory

<class '[Link]'>
RangeIndex: 200 entries, 0 to 199
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 200 non-null int64
1 TV 200 non-null float64
2 Radio 200 non-null float64
3 Newspaper 200 non-null float64
4 Sales 200 non-null float64
dtypes: float64(4), int64(1)
memory usage: 7.9 KB

[Link]()

Unnamed: 0 TV Radio Newspaper Sales

count 200.000000 200.000000 200.000000 200.000000 200.000000

mean 100.500000 147.042500 23.264000 30.554000 14.022500

std 57.879185 85.854236 14.846809 21.778621 5.217457

min 1.000000 0.700000 0.000000 0.300000 1.600000

25% 50.750000 74.375000 9.975000 12.750000 10.375000

50% 100.500000 149.750000 22.900000 25.750000 12.900000

75% 150.250000 218.825000 36.525000 45.100000 17.400000

max 200.000000 296.400000 49.600000 114.000000 27.000000

[Link]

Index(['Unnamed: 0', 'TV', 'Radio', 'Newspaper', 'Sales'], dtype='object')

[Link]().sum()

[Link] 2/7
6/17/23, 11:49 AM SALES [Link] - Colaboratory

[Link](figsize=(4,4))
[Link](data=df,x=df['TV'],y=df['Sales'])
[Link]()

[Link](figsize=(4,4))
[Link](data=df,x=df['Radio'],y=df['Sales'])
[Link]()

[Link] 3/7
6/17/23, 11:49 AM SALES [Link] - Colaboratory

[Link](figsize=(4,4))
[Link](data=df,x=df['Newspaper'],y=df['Sales'])
[Link]()

#splitting the datset into X,the attributes and y,the target variable
X=[Link]('Sales',axis=1)

[Link] 4/7
6/17/23, 11:49 AM SALES [Link] - Colaboratory

Unnamed: 0 TV Radio Newspaper

0 1 230.1 37.8 69.2

1 2 44.5 39.3 45.1

2 3 17.2 45.9 69.3

3 4 151.5 41.3 58.5

4 5 180.8 10.8 58.4

... ... ... ... ...

195 196 38.2 3.7 13.8

196 197 94.2 4.9 8.1

197 198 177.0 9.3 6.4


y=df['Sales']
198 199 283.6 42.0 66.2

199 200 232.1 8.6 8.7


y
200 rows × 4 columns
0 22.1
1 10.4
2 9.3
3 18.5
4 12.9
...
195 7.6
196 9.7
197 12.8
198 25.5
199 13.4
Name: Sales, Length: 200, dtype: float64

from sklearn.model_selection import train_test_split
X_train, X_test, y_train,y_test = train_test_split(X,y,test_size = 0.20, random_state = 0)

from sklearn.linear_model import LinearRegression
model= LinearRegression()

[Link] 5/7
6/17/23, 11:49 AM SALES [Link] - Colaboratory

#fitting the model to the dataset
[Link](X_train,y_train)

▾ LinearRegression
LinearRegression()

#predictions
y_predictions=[Link](X_test)

y_predictions

array([10.24664397, 7.3067958 , 7.01313011, 24.0629912 , 11.84538583,


6.3623126 , 12.97367539, 15.01392577, 10.95137181, 16.2302401 ,
22.94764396, 9.07892877, 10.29830431, 15.372556 , 11.63358138,
12.38729183, 18.91996453, 10.72601207, 16.03372676, 17.36589369,
23.85225051, 9.51048184, 15.25366204, 12.2528663 , 5.81346738,
15.19998588, 12.12398331, 20.74762723, 13.36651757, 9.16548788,
13.0430809 , 21.47977498, 18.01828076, 21.14170908, 6.74096317,
5.9527019 , 7.82515133, 13.21661502, 14.72150697, 6.18172688])

# Lets evaluate the model for its accuracy using various metrics such as RMSE and R-Squared
from sklearn import metrics

print('MAE:',metrics.mean_absolute_error(y_predictions,y_test))
print('RMSE:',[Link](metrics.mean_squared_error(y_predictions,y_test)))
print('R-Squared',metrics.r2_score(y_predictions,y_test))

MAE: 1.4158480868317487
RMSE: 2.125203007295874
R-Squared 0.8283777385812956

[Link] 6/7

Common questions

Powered by AI

Using multiple independent variables, such as TV, Radio, and Newspaper advertising, allows the linear regression model to capture the influence of various advertising mediums on sales. This multivariable approach can account for multiple factors affecting the outcome, providing a more comprehensive model that better predicts sales than single-variable models by capturing interactions and the combined effect of different ad spends.

The primary attributes used in the sales prediction model are TV, Radio, and Newspaper. These are represented as numerical features in the dataset after the 'Unnamed: 0' column is dropped. The 'Sales' column is considered the target variable. The dataset is split into attributes (X) and target variable (y) for model training, where X includes 'TV', 'Radio', and 'Newspaper'.

The model evaluates its performance using metrics such as Mean Absolute Error (MAE), Root Mean Square Error (RMSE), and R-Squared. MAE provides the average magnitude of the errors, RMSE measures the square root of the average of squared deviations, and R-Squared indicates how well the data fit's the regression model (with 82.84% accuracy)

Preparing the dataset for training involves reading the data into a pandas DataFrame, checking for null values or duplicates, and splitting the data into predictors (X) and the target variable (y). The train_test_split function from sklearn is used to divide the data into training and test sets. Further, libraries like pandas for data manipulation and sklearn for model operations facilitate this process.

The train_test_split function divides the dataset into training and testing sets, allowing the model to learn from a subset of data (80% of the total data) and validate its performance on unseen data (20% of the total data), ensuring the model’s reliability and generalizing ability.

Checking for duplicated entries helps ensure data quality and integrity by confirming that each data point is unique, which is crucial for reliable model training. In this analysis, a result of zero duplicated entries indicates a high-quality dataset without redundancy or duplication issues, ensuring that each observation contributes independently to the model.

Pandas facilitates data manipulation, including reading and inspecting the dataset to identify key statistics and features, handling data structures, and preprocessing. Matplotlib is used for visualizing relationships and trends within the data, facilitating scatter plots of sales against TV, Radio, and Newspaper advertising, allowing for a clearer understanding of potential correlations.

Scatterplot visualizations suggest a positive relationship between the amounts spent on TV and Radio advertising and Sales, indicating that investments in these mediums may significantly impact sales numbers. The scatterplot between Newspaper advertising and sales suggests a weaker correlation. Such visual insights help prioritize TV and Radio as more influential predictors in the sales model.

Mean Absolute Error (MAE) contributes by providing an average error magnitude in predictions without considering their direction. It is a straightforward metric indicating prediction accuracy, offering an average deviation of 1.42 units in sales prediction with this model—an understanding of typical prediction error magnitude.

The R-Squared value measures how well the independent variables explain the variance in the dependent variable, with values closer to 1 indicating a better fit. In this case, an R-Squared value of approximately 0.828 suggests that the advertising mediums collectively explain about 82.84% of the variance in sales, indicating a strong model performance.

You might also like