File size: 1,963 Bytes
90b74ae
 
 
 
 
 
 
 
 
0a86160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
""".2646

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1rm4V5QMLQuWMZikisvPv9jIeKgMEAptr
"""

import pandas as pd

df = pd.read_csv('//content/Advertising And Sales.csv')

print(df.head())

print(df.describe())

print(df.info())

import matplotlib.pyplot as plt
import seaborn as sns

sns.set(style="whitegrid")

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

plt.subplot(1, 3, 1)
sns.scatterplot(x='TV', y='Sales', data=df)
plt.title('TV Advertising vs Sales')

plt.subplot(1, 3, 2)
sns.scatterplot(x='Radio', y='Sales', data=df)
plt.title('Radio Advertising vs Sales')

plt.subplot(1, 3, 3)
sns.scatterplot(x='Newspaper', y='Sales', data=df)
plt.title('Newspaper Advertising vs Sales')

plt.tight_layout()
plt.show()

corr_matrix = df.corr()

plt.figure(figsize=(8, 6))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Correlation Matrix')
plt.show()

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

X = df[['TV', 'Radio', 'Newspaper']]
y = df['Sales']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)

print('Mean Squared Error:' , mean_squared_error(y_test, y_pred))
print('R^2 Score:', r2_score(y_test, y_pred))

coefficients = pd.DataFrame(model.coef_, X.columns, columns=['Coefficient'])
print(coefficients)

def calculate_roi(spend, sales):
  return sales / spend if spend != 0 else 0

df['TV_ROI'] = df.apply(lambda row: calculate_roi(row['TV'], row['Sales']), axis=1)
df['Radio_ROI'] = df.apply(lambda row: calculate_roi(row['Radio'], row['Sales']), axis=1)
df['Newspaper_ROI'] = df.apply(lambda row: calculate_roi(row['Newspaper'], row['Sales']), axis=1)

print(df[['TV_ROI', 'Radio_ROI', 'Newspaper_ROI']].head())