| |
| """.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()) |