Marketing Analytics Fundamentals with Python
Contents
- Introduction Marketing Analytics Fundamentals with Python Welcome to this TikTok series, where we'll explore the fundamentals of marketing analytics using the powerful Python programming language.
- Data Collection Collecting Data from APIs Gather data from various sources, such as web APIs, for marketing analytics.
- Data Preprocessing Cleaning and Transforming Data Preprocess data by handling missing values, converting data types, and transforming features.
- Exploratory Data Analysis (EDA) Visualizing Data Create various visualizations to explore and understand your data.
- Customer Segmentation K-Means Clustering Segment customers using K-means clustering based on relevant features.
- Customer Segmentation Hierarchical Clustering Alternatively, use hierarchical clustering to segment customers.
- Marketing Campaign Analysis Calculating Conversion Rates Analyze the effectiveness of marketing campaigns by calculating conversion rates.
- Marketing Campaign Analysis Customer Acquisition Cost Determine the cost of acquiring new customers for each marketing campaign.
- Predictive Modeling Logistic Regression Build a logistic regression model to predict customer behavior, such as likelihood to purchase.
- Predictive Modeling Random Forest Classifier Alternatively, use a random forest classifier for predicting customer behavior.
- A/B Testing Chi-Squared Test Conduct a chi-squared test to compare the performance of different marketing strategies.
- Visualizations and Reporting Interactive Dashboards Create interactive dashboards to effectively communicate insights and recommendations to stakeholders.
Introduction Marketing Analytics Fundamentals with Python Welcome to this TikTok series, where we'll explore the fundamentals of marketing analytics using the powerful Python programming language.
Data Collection Collecting Data from APIs Gather data from various sources, such as web APIs, for marketing analytics.
Code Example:
import requests
# Set API endpoint and parameters
url = 'https://api.example.com/data'
params = {'start_date': '2023-01-01', 'end_date': '2023-03-31'}
# Send the request and retrieve data
response = requests.get(url, params=params)
data = response.json()
Data Preprocessing Cleaning and Transforming Data Preprocess data by handling missing values, converting data types, and transforming features.
Code Example:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# Load data from a CSV file
data = pd.read_csv('marketing_data.csv')
# Drop missing values
data = data.dropna()
# Convert categorical features to numerical
encoder = LabelEncoder()
data['product_category'] = encoder.fit_transform(data['product_category'])
Exploratory Data Analysis (EDA) Visualizing Data Create various visualizations to explore and understand your data.
Code Example:
import matplotlib.pyplot as plt
import seaborn as sns
# Create a scatter plot
plt.figure(figsize=(10, 6))
sns.scatterplot(x='age', y='purchase_amount', data=data, hue='gender')
plt.title('Purchase Amount by Age and Gender')
plt.show()
Customer Segmentation K-Means Clustering Segment customers using K-means clustering based on relevant features.
Code Example:
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
# Standardize features
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data[['age', 'income', 'num_purchases']])
# Perform K-means clustering
kmeans = KMeans(n_clusters=4)
clusters = kmeans.fit_predict(data_scaled)
data['segment'] = clusters
Customer Segmentation Hierarchical Clustering Alternatively, use hierarchical clustering to segment customers.
Code Example:
from sklearn.cluster import AgglomerativeClustering
# Perform hierarchical clustering
cluster = AgglomerativeClustering(n_clusters=4)
clusters = cluster.fit_predict(data[['age', 'income', 'num_purchases']])
data['segment'] = clusters
Marketing Campaign Analysis Calculating Conversion Rates Analyze the effectiveness of marketing campaigns by calculating conversion rates.
Code Example:
# Calculate overall conversion rate
total_conversions = data[data['converted'] == True].shape[0]
total_visitors = data.shape[0]
overall_conversion_rate = total_conversions / total_visitors
# Calculate conversion rate by campaign
campaign_conversion_rates = data.groupby('campaign')['converted'].mean()
Marketing Campaign Analysis Customer Acquisition Cost Determine the cost of acquiring new customers for each marketing campaign.
Code Example:
# Calculate customer acquisition cost
campaign_costs = data.groupby('campaign')['campaign_cost'].sum()
new_customers = data[data['converted'] == True].groupby('campaign')['customer_id'].nunique()
customer_acquisition_cost = campaign_costs / new_customers
Predictive Modeling Logistic Regression Build a logistic regression model to predict customer behavior, such as likelihood to purchase.
Code Example:
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# Split data into features and target
X = data[['age', 'income', 'num_purchases']]
y = data['converted']
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)
Predictive Modeling Random Forest Classifier Alternatively, use a random forest classifier for predicting customer behavior.
Code Example:
from sklearn.ensemble import RandomForestClassifier
# Train the random forest classifier
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = rf_model.predict(X_test)
A/B Testing Chi-Squared Test Conduct a chi-squared test to compare the performance of different marketing strategies.
Code Example:
import statsmodels.stats.api as sms
# Perform a chi-squared test
control_conversions = data[data['group'] == 'control']['converted'].sum()
test_conversions = data[data['group'] == 'test']['converted'].sum()
chi2, p_val, dof, ex = sms.chi2_contingency([[control_conversions, test_conversions]])
print(f'p-value: {p_val}')
Visualizations and Reporting Interactive Dashboards Create interactive dashboards to effectively communicate insights and recommendations to stakeholders.
Code Example:
import plotly.express as px
# Create an interactive scatter plot
fig = px.scatter(data, x='age', y='purchase_amount', color='gender', size='num_purchases',
hover_data=['product_category'])
fig.update_layout(title='Customer Purchase Behavior')
fig.show()