# 探索資料分析

## 取得基本統計資料 basic-statistics

``````table_name = 'ecommerce_events'

basic_statistics_query = f"""
SELECT
COUNT(_id) as "totalRows",
COUNT(DISTINCT _id) as "distinctUsers"
FROM {table_name}"""

df = qs_cursor.query(basic_statistics_query, output="dataframe")
df
``````

totalRows
distinctUsers
0
1276563
1276563

## 建立大型資料集的取樣版本 create-dataset-sample

• 首先， 分析 要以指定取樣比率建立取樣版本的資料集
• 接下來，查詢資料集的抽樣版本。 根據您套用至樣本資料集的函式，您可能想要將輸出縮放為數字以取得完整資料集

### 建立5%樣本 create-sample

``````# A sampling rate of 10 is 100% in Query Service, so for 5% use a sampling rate 0.5
sampling_rate = 0.5

analyze_table_query=f"""
SET aqp=true;
ANALYZE TABLE {table_name} TABLESAMPLE SAMPLERATE {sampling_rate}"""

qs_cursor.query(analyze_table_query, output="raw")
``````

### 檢視您的範例 view-sample

``````sampled_version_of_table_query = f'''SELECT sample_meta('{table_name}')'''

df_samples = qs_cursor.query(sampled_version_of_table_query, output="dataframe")
df_samples
``````

sample_table_name
sample_dataset_id
parent_dataset_id
sample_type
sampling_rate
filter_condition_on_source_dataset
sample_num_rows

0
cmle_synthetic_data_experience_event_dataset_c…
650f7a09ed6c3e28d34d7fc2
64fb4d7a7d748828d304a2f4

0.5
6427
23/09/2023
11:51:37

### 查詢您的範例 query-sample-data

``````sample_table_name = df_samples[df_samples["sampling_rate"] == sampling_rate]["sample_table_name"].iloc[0]

count_query=f'''SELECT count(*) as cnt from {sample_table_name}'''

df = qs_cursor.query(count_query, output="dataframe")
# Divide by the sampling rate to extrapolate to the full dataset
approx_count = df["cnt"].iloc[0] / (sampling_rate / 100)

print(f"Approximate count: {approx_count} using {sampling_rate *10}% sample")
``````

``````Approximate count: 1284600.0 using 5.0% sample
``````

## 電子郵件漏斗分析 email-funnel-analysis

``````simple_funnel_analysis_query = f'''SELECT eventType, COUNT(DISTINCT _id) as "distinctUsers",COUNT(_id) as "distinctEvents" FROM {table_name} GROUP BY eventType ORDER BY distinctUsers DESC'''

funnel_df = qs_cursor.query(simple_funnel_analysis_query, output="dataframe")
funnel_df
``````

eventType
distinctUsers
distinctEvents
0
directMarketing.emailSent
598840
598840
1
directMarketing.emailOpened
239028
239028
2
web.webpagedetails.pageViews
120118
120118
3
119669
119669
4
directMarketing.emailClicked
51581
51581
5
commerce.productViews
37915
37915
6
decisioning.propositionDisplay
37650
37650
7
37581
37581
8
web.formFilledOut
17860
17860
9
7610
7610
10
decisioning.propositionInteract
2964
2964
11
decisioning.propositionDismiss
2889
2889
12
commerce.purchases
2858
2858

### 繪製查詢結果 plot-results

``````import plotly.express as px

email_funnel_events = ["directMarketing.emailSent", "directMarketing.emailOpened", "directMarketing.emailClicked", "web.formFilledOut"]
email_funnel_df = funnel_df[funnel_df["eventType"].isin(email_funnel_events)]

fig = px.funnel(email_funnel_df, y='eventType', x='distinctUsers')
fig.show()
``````

## 事件關聯 event-correlations

1. 依設定檔計算每種事件型別的事件數。
2. 彙總各設定檔中每種事件型別的計數，並計算每種事件型別的關聯，其中 `web,formFilledOut`.
3. 將計數和相關性的資料流轉換為每個功能的Pearson相關係數（事件型別計數）表格與目標事件。
4. 將結果以視覺化方式顯示在繪圖中。

``````large_correlation_query=f'''
SELECT SUM(webFormsFilled) as webFormsFilled_totalUsers,
SUM(productViews) as productViews_totalUsers,
SUM(productPurchases) as productPurchases_totalUsers,
SUM(propositionDismisses) as propositionDismisses_totaUsers,
SUM(propositionDisplays) as propositionDisplays_totaUsers,
SUM(propositionInteracts) as propositionInteracts_totalUsers,
SUM(emailClicks) as emailClicks_totalUsers,
SUM(emailOpens) as emailOpens_totalUsers,
SUM(webPageViews) as webPageViews_totalusers,
corr(webFormsFilled, emailOpens) as webForms_EmailOpens,
corr(webFormsFilled, productViews) as webForms_productViews,
corr(webFormsFilled, productPurchases) as webForms_productPurchases,
corr(webFormsFilled, propositionDismisses) as webForms_propositionDismisses,
corr(webFormsFilled, propositionInteracts) as webForms_propositionInteracts,
corr(webFormsFilled, emailClicks) as webForms_emailClicks,
corr(webFormsFilled, emailOpens) as webForms_emailOpens,
corr(webFormsFilled, emailSends) as webForms_emailSends,
corr(webFormsFilled, webPageViews) as webForms_webPageViews
FROM(
SELECT _{tenant_id}.cmle_id as userID,
SUM(CASE WHEN eventType='web.formFilledOut' THEN 1 ELSE 0 END) as webFormsFilled,
SUM(CASE WHEN eventType='commerce.productViews' THEN 1 ELSE 0 END) as productViews,
SUM(CASE WHEN eventType='commerce.productPurchases' THEN 1 ELSE 0 END) as productPurchases,
SUM(CASE WHEN eventType='decisioning.propositionDismiss' THEN 1 ELSE 0 END) as propositionDismisses,
SUM(CASE WHEN eventType='decisioning.propositionDisplay' THEN 1 ELSE 0 END) as propositionDisplays,
SUM(CASE WHEN eventType='decisioning.propositionInteract' THEN 1 ELSE 0 END) as propositionInteracts,
SUM(CASE WHEN eventType='directMarketing.emailClicked' THEN 1 ELSE 0 END) as emailClicks,
SUM(CASE WHEN eventType='directMarketing.emailOpened' THEN 1 ELSE 0 END) as emailOpens,
SUM(CASE WHEN eventType='directMarketing.emailSent' THEN 1 ELSE 0 END) as emailSends,
SUM(CASE WHEN eventType='web.webinteraction.pageViews' THEN 1 ELSE 0 END) as webPageViews
FROM {table_name}
GROUP BY userId
)
'''
large_correlation_df = qs_cursor.query(large_correlation_query, output="dataframe")
large_correlation_df
``````

webFormsFilled_totalUsers
productViews_totalUsers
productPurchases_totalUsers
propositionDismisses_totaUsers
propositionDisplays_totaUsers
propositionInteracts_totalUsers
emailClicks_totalUsers
emailOpens_totalUsers
webForms_productViews
webForms_productPurchases
webForms_propositionDismisses
webForms_propositionInteracts
webForms_emailClicks
webForms_emailOpens
webForms_emailSends
webForms_webPageViews
0
17860
7610
37915
0
2889
37650
2964
51581
239028
37581
0.026805
0.2779
None
0.06014
0.143656
0.305657
0.218874
0.192836
0.259353
None

### 將列轉換為事件型別關聯 event-type-correlation

``````cols = large_correlation_df.columns
corrdf = large_correlation_df[[col for col in cols if ("webForms_"  in col)]].melt()
corrdf["feature"] = corrdf["variable"].apply(lambda x: x.replace("webForms_", ""))
corrdf["pearsonCorrelation"] = corrdf["value"]

corrdf.fillna(0)
``````

pearsonCorrelation
0
`webForms_EmailOpens`
0.218874
EmailOpens
0.218874
1
`webForms_advertisingClicks`
0.026805

0.026805
2
`webForms_productViews`
0.277900

0.277900
3
`webForms_productPurchases`
0.000000
productPurchases
0.000000
4
`webForms_propositionDismisses`
0.060140
propositionDismiss
0.060140
5
`webForms_propositionInteracts`
0.143656
propositionInteracts
0.143656
6
`webForms_emailClicks`
0.305657

0.305657
7
`webForms_emailOpens`
0.218874
emailOpens
0.218874
8
`webForms_emailSends`
0.192836

0.192836
9
`webForms_webLinkClicks`
0.259353
0.259353
10
`webForms_webPageViews`
0.000000
webPageViews
0.000000

``````import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5,10))
sns.barplot(data=corrdf.fillna(0), y="feature", x="pearsonCorrelation")
ax.set_title("Pearson Correlation of Events with the outcome event")
``````

## 後續步驟

recommendation-more-help
ccf2b369-4031-483f-af63-a93b5ae5e3fb