Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Last active April 6, 2025 13:58
Show Gist options
  • Save thinkphp/f0faa1f49de0382e92d2f90d7006d704 to your computer and use it in GitHub Desktop.
Save thinkphp/f0faa1f49de0382e92d2f90d7006d704 to your computer and use it in GitHub Desktop.
marketing_campaign.py
"""
Facem un program care analizeaza Campaniile de Marketing
- unealta complexa pentru analiza performantei campaniilor de marketing.
Scop:
- evalueze performanta diferitelor campanii de Marketing
- vizualizeze datele intr-un mod intuitiv si informativ
- prezicere ROI (return on investment) bazat pe performanta istorica
- simulare diferite scenarii de buget pentru a gasi nivelul optimal de investment
- ofere recomandari actionabile pentru decizii strategice de marketing.
Aplicatia noastra - calculeaza
- profit
- returnarea investitiei ROI
- costul de achizitie al clientilor
- venit per client
- profit per client
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error
import io
# functie care incarca dataset dintr-un fisier source or String
def load_data(source, is_file=False):
if is_file:
df = pd.read_csv( source )
else:
df = pd.read_csv(io.StringIO(source))
return df
def calculate_metrics( df ):
df['Profit'] = df['Revenue_Generated'] - df['Total_Spend']
df['ROI'] = (df['Profit'] / df['Total_Spend']) * 100;
df['Customer_Acquisition_Cost'] = df['Total_Spend'] / df['Customers_Acquired']
df['Revenue_per_Customer'] = df['Revenue_Generated'] / df['Customers_Acquired']
df['Profit_per_Customer'] = df['Profit'] / df['Customers_Acquired']
return df
def analyze_performance( df ):
metrics = ['Profit', 'ROI', 'Customer_Acquisition_Cost', 'Revenue_per_Customer']
results = pd.DataFrame({
'Metric': [],
'Best Campaign': [],
'Best Value': [],
'Worst Campaign': [],
'Worst Value': []
})
for metric in metrics:
if metric == 'Customer_Acquisition_Cost':
best_idx = df[metric].idxmin()
worst_idx = df[metric].idxmax()
else:
best_idx = df[metric].idxmax()
worst_idx = df[metric].idxmin()
results = pd.concat([results, pd.DataFrame({
'Metric': [metric],
'Best Campaign': [df.loc[best_idx, 'Campaign']],
'Best Value': [df.loc[best_idx, metric]],
'Worst Campaign': [df.loc[worst_idx, 'Campaign']],
'Worst Value': [df.loc[worst_idx, metric]],
})], ignore_index = True)
print("Performance Analysis:")
print(results)
print("\nSummary Statistics:")
print(df.describe())
return results
def main():
data = """Campaign,Total_Spend,Revenue_Generated,Customers_Acquired
Email,5000,15000,150
Social Media,7500,22500,300
Google Ads,10000,35000,500
Referral,3000,9000,100
Content MArketing,6000,18000,250"""
df = load_data(data, is_file=False)
df = calculate_metrics(df)
print("Marketing Campaign Data:")
print(df)
performance_results = analyze_performance(df)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment