Upload _2646.py
Browse files
_2646.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
""".2646
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1rm4V5QMLQuWMZikisvPv9jIeKgMEAptr
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
|
| 12 |
+
df = pd.read_csv('//content/Advertising And Sales.csv')
|
| 13 |
+
|
| 14 |
+
print(df.head())
|
| 15 |
+
|
| 16 |
+
print(df.describe())
|
| 17 |
+
|
| 18 |
+
print(df.info())
|
| 19 |
+
|
| 20 |
+
import matplotlib.pyplot as plt
|
| 21 |
+
import seaborn as sns
|
| 22 |
+
|
| 23 |
+
sns.set(style="whitegrid")
|
| 24 |
+
|
| 25 |
+
plt.figure(figsize=(14, 6))
|
| 26 |
+
|
| 27 |
+
plt.subplot(1, 3, 1)
|
| 28 |
+
sns.scatterplot(x='TV', y='Sales', data=df)
|
| 29 |
+
plt.title('TV Advertising vs Sales')
|
| 30 |
+
|
| 31 |
+
plt.subplot(1, 3, 2)
|
| 32 |
+
sns.scatterplot(x='Radio', y='Sales', data=df)
|
| 33 |
+
plt.title('Radio Advertising vs Sales')
|
| 34 |
+
|
| 35 |
+
plt.subplot(1, 3, 3)
|
| 36 |
+
sns.scatterplot(x='Newspaper', y='Sales', data=df)
|
| 37 |
+
plt.title('Newspaper Advertising vs Sales')
|
| 38 |
+
|
| 39 |
+
plt.tight_layout()
|
| 40 |
+
plt.show()
|
| 41 |
+
|
| 42 |
+
corr_matrix = df.corr()
|
| 43 |
+
|
| 44 |
+
plt.figure(figsize=(8, 6))
|
| 45 |
+
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f')
|
| 46 |
+
plt.title('Correlation Matrix')
|
| 47 |
+
plt.show()
|
| 48 |
+
|
| 49 |
+
from sklearn.model_selection import train_test_split
|
| 50 |
+
from sklearn.linear_model import LinearRegression
|
| 51 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 52 |
+
|
| 53 |
+
X = df[['TV', 'Radio', 'Newspaper']]
|
| 54 |
+
y = df['Sales']
|
| 55 |
+
|
| 56 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 57 |
+
|
| 58 |
+
model = LinearRegression()
|
| 59 |
+
model.fit(X_train, y_train)
|
| 60 |
+
|
| 61 |
+
y_pred = model.predict(X_test)
|
| 62 |
+
|
| 63 |
+
print('Mean Squared Error:' , mean_squared_error(y_test, y_pred))
|
| 64 |
+
print('R^2 Score:', r2_score(y_test, y_pred))
|
| 65 |
+
|
| 66 |
+
coefficients = pd.DataFrame(model.coef_, X.columns, columns=['Coefficient'])
|
| 67 |
+
print(coefficients)
|
| 68 |
+
|
| 69 |
+
def calculate_roi(spend, sales):
|
| 70 |
+
return sales / spend if spend != 0 else 0
|
| 71 |
+
|
| 72 |
+
df['TV_ROI'] = df.apply(lambda row: calculate_roi(row['TV'], row['Sales']), axis=1)
|
| 73 |
+
df['Radio_ROI'] = df.apply(lambda row: calculate_roi(row['Radio'], row['Sales']), axis=1)
|
| 74 |
+
df['Newspaper_ROI'] = df.apply(lambda row: calculate_roi(row['Newspaper'], row['Sales']), axis=1)
|
| 75 |
+
|
| 76 |
+
print(df[['TV_ROI', 'Radio_ROI', 'Newspaper_ROI']].head())
|