Sales Prediction Using Python
Sales Prediction Using Python
ipynb - Colaboratory
#importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#dataframe
df=pd.read_csv('/content/Advertising.csv')
df
df.shape
(200, 5)
df.info()
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 1/7
6/17/23, 11:49 AM SALES PREDICTION5.ipynb - Colaboratory
<class 'pandas.core.frame.DataFrame'>
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
df.describe()
df.columns
df.duplicated().sum()
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 2/7
6/17/23, 11:49 AM SALES PREDICTION5.ipynb - Colaboratory
plt.figure(figsize=(4,4))
sns.scatterplot(data=df,x=df['TV'],y=df['Sales'])
plt.show()
plt.figure(figsize=(4,4))
sns.scatterplot(data=df,x=df['Radio'],y=df['Sales'])
plt.show()
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 3/7
6/17/23, 11:49 AM SALES PREDICTION5.ipynb - Colaboratory
plt.figure(figsize=(4,4))
sns.scatterplot(data=df,x=df['Newspaper'],y=df['Sales'])
plt.show()
#splitting the datset into X,the attributes and y,the target variable
X=df.drop('Sales',axis=1)
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 4/7
6/17/23, 11:49 AM SALES PREDICTION5.ipynb - Colaboratory
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()
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 5/7
6/17/23, 11:49 AM SALES PREDICTION5.ipynb - Colaboratory
#fitting the model to the dataset
model.fit(X_train,y_train)
▾ LinearRegression
LinearRegression()
#predictions
y_predictions=model.predict(X_test)
y_predictions
# 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:',np.sqrt(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
https://colab.research.google.com/drive/1UsWj3VJuJOjKRFXDV4sxxzQE3bH-LcNG#scrollTo=mVKE9hD-VplA&printMode=true 6/7