탐색적 데이터 분석
이 문서에서는 Data Distiller을 사용하여 Python 전자 필기장의 데이터를 탐색하고 분석하는 기본 예제와 모범 사례를 제공합니다.
시작하기
이 안내서를 계속하기 전에 Python 전자 필기장에서 Data Distiller에 대한 연결을 만들었는지 확인하십시오. Data Distiller에 Python 전자 필기장을 연결하는 방법에 대한 지침은 설명서를 참조하세요.
기본 통계 얻기 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
샘플 출력
대용량 데이터 세트의 샘플 버전 만들기 create-dataset-sample
쿼리하려는 데이터 세트가 너무 크거나 탐색적 쿼리의 정확한 결과가 필요하지 않은 경우 Data Distiller 쿼리에 사용할 수 있는 샘플링 기능을 사용하십시오. 2단계 프로세스입니다.
- 먼저 데이터 집합을 분석 하여 지정된 샘플링 비율로 샘플링된 버전을 만듭니다.
- 그런 다음, 샘플링된 데이터 세트 버전을 쿼리합니다. 샘플링된 데이터 세트에 적용하는 함수에 따라 출력을 전체 데이터 세트에 대한 숫자로 조정할 수 있습니다
5% 샘플 만들기 create-sample
아래 예제는 데이터 세트를 분석하고 5% 샘플을 만듭니다.
# 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
sample_meta
함수를 사용하여 지정된 데이터 집합에서 만든 샘플을 볼 수 있습니다. 아래의 코드 조각은 sample_meta
함수를 사용하는 방법을 보여 줍니다.
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
샘플 출력:
샘플 쿼리 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
단계 분석은 목표 결과에 도달하는 데 필요한 단계와 이러한 각 단계를 통과하는 사용자 수를 이해하는 방법입니다. 아래 예제는 뉴스레터를 구독하는 사용자에게 연결되는 단계를 간단하게 분석하는 방법을 보여 줍니다. 구독 결과가 이벤트 유형 web.formFilledOut
(으)로 표시됩니다.
먼저 쿼리를 실행하여 각 단계의 사용자 수를 가져옵니다.
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
샘플 출력
그래프 쿼리 결과 plot-results
그런 다음 Python plotly
라이브러리를 사용하여 쿼리 결과를 플롯합니다.
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
또 다른 일반적인 분석은 이벤트 유형과 대상 전환 이벤트 유형 간의 상관 관계를 계산하는 것입니다. 이 예제에서 구독 이벤트는 web.formFilledOut
(으)로 표시됩니다. 이 예제에서는 Data Distiller 쿼리에서 사용할 수 있는 Spark 함수를 사용하여 다음 단계를 수행합니다.
- 프로필별로 각 이벤트 유형에 대한 이벤트 수를 카운트합니다.
- 프로필에서 각 이벤트 유형의 개수를 집계하고
web,formFilledOut
과(와) 각 이벤트 유형의 상관 관계를 계산합니다. - 카운트 및 상관 관계의 데이터 프레임을 대상 이벤트가 있는 각 피쳐(이벤트 유형 카운트)의 피어슨 상관 계수 표로 변환합니다.
- 플롯에서 결과를 시각화합니다.
Spark 함수는 데이터를 집계하여 작은 결과 테이블을 반환하므로 전체 데이터 집합에서 이 형식의 쿼리를 실행할 수 있습니다.
large_correlation_query=f'''
SELECT SUM(webFormsFilled) as webFormsFilled_totalUsers,
SUM(advertisingClicks) as advertisingClicks_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(webLinkClicks) as webLinksClicks_totalUsers,
SUM(webPageViews) as webPageViews_totalusers,
corr(webFormsFilled, emailOpens) as webForms_EmailOpens,
corr(webFormsFilled, advertisingClicks) as webForms_advertisingClicks,
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, webLinkClicks) as webForms_webLinkClicks,
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='advertising.clicks' THEN 1 ELSE 0 END) as advertisingClicks,
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.linkClicks' THEN 1 ELSE 0 END) as webLinkClicks,
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
샘플 출력:
행을 이벤트 유형 상관 관계로 변환 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)
샘플 출력:
webForms_EmailOpens
webForms_advertisingClicks
webForms_productViews
webForms_productPurchases
webForms_propositionDismisses
webForms_propositionInteracts
webForms_emailClicks
webForms_emailOpens
webForms_emailSends
webForms_webLinkClicks
webForms_webPageViews
마지막으로 matplotlib
Python 라이브러리와의 상관 관계를 시각화할 수 있습니다.
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")
다음 단계
이 문서를 읽고 Data Distiller을 사용하여 Python 전자 필기장의 데이터를 탐색하고 분석하는 방법을 배웠습니다. Experience Platform에서 머신 러닝 환경의 피드 사용자 지정 모델까지 기능 파이프라인을 만드는 다음 단계는 머신 러닝용 기능 엔지니어입니다.