BigQuery ML は、データ アナリストが最小限のコーディングによる ML モデルの作成、トレーニング、評価、予測に使用できる BigQuery の機能です。
このラボでは、Google Merchandise Store の数百万件の Google アナリティクス レコードが BigQuery に読み込まれた特別な e コマース データセットを使用します。このデータを使用して、顧客の購買習慣を予測する分類(ロジスティック回帰)モデルを BigQuery ML で作成します。
#standardSQL
WITH visitors AS(
SELECT
COUNT(DISTINCT fullVisitorId) AS total_visitors
FROM `data-to-insights.ecommerce.web_analytics`
),
purchasers AS(
SELECT
COUNT(DISTINCT fullVisitorId) AS total_purchasers
FROM `data-to-insights.ecommerce.web_analytics`
WHERE totals.transactions IS NOT NULL
)
SELECT
total_visitors,
total_purchasers,
total_purchasers / total_visitors AS conversion_rate
FROM visitors, purchasers
[実行] をクリックします。
結果: 2.69%
質問: 売り上げで上位 5 つの商品は何ですか。
前のクエリを消去し、エディタで以下のクエリを追加します。
SELECT
p.v2ProductName,
p.v2ProductCategory,
SUM(p.productQuantity) AS units_sold,
ROUND(SUM(p.localProductRevenue/1000000),2) AS revenue
FROM `data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h,
UNNEST(h.product) AS p
GROUP BY 1, 2
ORDER BY revenue DESC
LIMIT 5;
# 再訪問時に購入した人(最初の訪問時に購入した人も含む)WITH all_visitor_stats AS (
SELECT
fullvisitorid, # 一意の訪問者数 741,721
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM `data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid
)
SELECT
COUNT(DISTINCT fullvisitorid) AS total_visitors,
will_buy_on_return_visit
FROM all_visitor_stats
GROUP BY will_buy_on_return_visit
回答: 唯一の正解はありませんが、よくある理由のひとつは、最終的に購入を決定する前に、別の e コマースサイトと比較検討してから購入するというものです。これは、事前に入念な事前調査と比較が必要になる高額な商品(自動車など)の購入の際に顕著ですが、比較的程度は低いものの、このサイトの商品(T シャツやアクセサリーなど)の購入にも当てはまります。
ここからは、BigQuery で ML モデルを作成し、新しいユーザーが将来的に購入を行うかどうかを予測します。こうした高い価値を持つユーザーを識別することで、マーケティング チームがそれらのユーザーにターゲットを絞って特別プロモーションや広告キャンペーンを実施することが可能になり、それらのユーザーが自社の e コマースサイトを再度訪問するまでの間に他のサイトと比較していたとしても、コンバージョンにつなげやすくなります。
タスク 3. 特徴量を選択し、トレーニング データセットを作成する
Google アナリティクスでは、さまざまなディメンションを捉えてこの e コマース ウェブサイトのユーザー訪問が計測されます。フィールドの一覧を [UA] BigQuery Export スキーマのドキュメントで確認してからデモ データセットをプレビューし、ユーザーによるウェブサイト初回訪問のデータと、そのユーザーが戻ってきて購入を行うかどうかの関係を、ML モデルが理解するために役立つ特徴を見つけます。
次の 2 つのフィールドが分類モデルに適した入力であるかどうかをテストしましょう。
totals.bounces(訪問者がウェブサイトをすぐに離れたかどうか)
totals.timeOnSite(訪問者がウェブサイトに留まった期間)
質問: 上記の 2 つのフィールドのみを使用することにはどんなリスクがありますか。
解答: ML の良し悪しは、提供されるトレーニング データにかかっています。入力した特徴とラベル(ここでは、訪問者が将来購入するかどうか)の関係をモデルに判断、学習させるための十分な情報が揃わない場合、精度の高いモデルを確立することはできません。これら 2 つのフィールドでモデルをトレーニングすることは出発点にはなりますが、精度の高いモデルを生成するために 2 つのフィールドだけで十分かどうかを見極める必要があります。
BigQuery エディタで、次のクエリを実行します。
SELECT
* EXCEPT(fullVisitorId)
FROM
# 特徴
(SELECT
fullVisitorId,
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site
FROM
`data-to-insights.ecommerce.web_analytics`
WHERE
totals.newVisits = 1)
JOIN
(SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM
`data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid)
USING (fullVisitorId)
ORDER BY time_on_site DESC
LIMIT 10;
CREATE OR REPLACE MODEL `ecommerce.classification_model`
OPTIONS
(
model_type='logistic_reg',
labels = ['will_buy_on_return_visit']
)
AS
#standardSQL
SELECT
* EXCEPT(fullVisitorId)
FROM
# 特徴
(SELECT
fullVisitorId,
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site
FROM
`data-to-insights.ecommerce.web_analytics`
WHERE
totals.newVisits = 1
AND date BETWEEN '20160801' AND '20170430') # 最初の 9 か月分でトレーニング
JOIN
(SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM
`data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid)
USING (fullVisitorId)
;
SELECT
roc_auc,
CASE
WHEN roc_auc > .9 THEN 'good'
WHEN roc_auc > .8 THEN 'fair'
WHEN roc_auc > .7 THEN 'decent'
WHEN roc_auc > .6 THEN 'not great'
ELSE 'poor' END AS model_quality
FROM
ML.EVALUATE(MODEL ecommerce.classification_model, (
SELECT
* EXCEPT(fullVisitorId)
FROM
# 特徴
(SELECT
fullVisitorId,
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site
FROM
`data-to-insights.ecommerce.web_analytics`
WHERE
totals.newVisits = 1
AND date BETWEEN '20170501' AND '20170630') # 2 か月分で評価
JOIN
(SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM
`data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid)
USING (fullVisitorId)
));
新しい特徴をいくつか追加し、classification_model_2 という名前の 2 番目の ML モデルを作成します。
初回訪問時に訪問者は購入手続きをどこまで進めていたか
訪問者はどこからアクセスしたか(トラフィック ソースがオーガニック検索、参照元サイトなど)
デバイスのカテゴリ(モバイル、タブレット、パソコン)
地理情報(国)
「+」(クエリを新規作成)アイコンをクリックして、この 2 番目のモデルを作成します。
CREATE OR REPLACE MODEL `ecommerce.classification_model_2`
OPTIONS
(model_type='logistic_reg', labels = ['will_buy_on_return_visit']) AS
WITH all_visitor_stats AS (
SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM `data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid
)
# 新しい特徴を追加
SELECT * EXCEPT(unique_session_id) FROM (
SELECT
CONCAT(fullvisitorid, CAST(visitId AS STRING)) AS unique_session_id,
# ラベル
will_buy_on_return_visit,
MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress,
# サイトでの行動
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site,
IFNULL(totals.pageviews, 0) AS pageviews,
# 訪問経路
trafficSource.source,
trafficSource.medium,
channelGrouping,
# モバイルかデスクトップか
device.deviceCategory,
# 地域
IFNULL(geoNetwork.country, "") AS country
FROM `data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h
JOIN all_visitor_stats USING(fullvisitorid)
WHERE 1=1
# 初回訪問のみ予測
AND totals.newVisits = 1
AND date BETWEEN '20160801' AND '20170430' # 9 か月分でトレーニング
GROUP BY
unique_session_id,
will_buy_on_return_visit,
bounces,
time_on_site,
totals.pageviews,
trafficSource.source,
trafficSource.medium,
channelGrouping,
device.deviceCategory,
country
);
注: これは新しいモデルですが、ここでも同じように最初の 9 か月分のデータを使ってトレーニングします。優れた出力が優れた入力(特徴)に起因することを確認できるように、新しいトレーニング データや異なるトレーニング データではなく、同じトレーニング データセットを使用することが重要です。
#standardSQL
SELECT
roc_auc,
CASE
WHEN roc_auc > .9 THEN 'good'
WHEN roc_auc > .8 THEN 'fair'
WHEN roc_auc > .7 THEN 'decent'
WHEN roc_auc > .6 THEN 'not great'
ELSE 'poor' END AS model_quality
FROM
ML.EVALUATE(MODEL ecommerce.classification_model_2, (
WITH all_visitor_stats AS (
SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM `data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid
)
# 新しい特徴を追加
SELECT * EXCEPT(unique_session_id) FROM (
SELECT
CONCAT(fullvisitorid, CAST(visitId AS STRING)) AS unique_session_id,
# ラベル
will_buy_on_return_visit,
MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress,
# サイトでの行動
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site,
totals.pageviews,
# 訪問経路
trafficSource.source,
trafficSource.medium,
channelGrouping,
# モバイルかデスクトップか
device.deviceCategory,
# 地域
IFNULL(geoNetwork.country, "") AS country
FROM `data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h
JOIN all_visitor_stats USING(fullvisitorid)
WHERE 1=1
# 初回訪問のみ予測
AND totals.newVisits = 1
AND date BETWEEN '20170501' AND '20170630' # 2 か月分で評価
GROUP BY
unique_session_id,
will_buy_on_return_visit,
bounces,
time_on_site,
totals.pageviews,
trafficSource.source,
trafficSource.medium,
channelGrouping,
device.deviceCategory,
country
)
));
以下の予測クエリでは、改善された分類モデルを使用して、Google Merchandise Store への初めての訪問者が再訪問で購入を行う確率を予測します。
SELECT
*
FROM
ml.PREDICT(MODEL `ecommerce.classification_model_2`,
(
WITH all_visitor_stats AS (
SELECT
fullvisitorid,
IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit
FROM `data-to-insights.ecommerce.web_analytics`
GROUP BY fullvisitorid
)
SELECT
CONCAT(fullvisitorid, '-',CAST(visitId AS STRING)) AS unique_session_id,
# ラベル
will_buy_on_return_visit,
MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress,
# サイトでの行動
IFNULL(totals.bounces, 0) AS bounces,
IFNULL(totals.timeOnSite, 0) AS time_on_site,
totals.pageviews,
# 訪問経路
trafficSource.source,
trafficSource.medium,
channelGrouping,
# モバイルかパソコンか
device.deviceCategory,
# 地域
IFNULL(geoNetwork.country, "") AS country
FROM `data-to-insights.ecommerce.web_analytics`,
UNNEST(hits) AS h
JOIN all_visitor_stats USING(fullvisitorid)
WHERE
# 初回訪問のみ予測
totals.newVisits = 1
AND date BETWEEN '20170701' AND '20170801' # 1 か月分でテスト
GROUP BY
unique_session_id,
will_buy_on_return_visit,
bounces,
time_on_site,
totals.pageviews,
trafficSource.source,
trafficSource.medium,
channelGrouping,
device.deviceCategory,
country
)
)
ORDER BY
predicted_will_buy_on_return_visit DESC;
予測は、最後の 1 か月(12 か月中)のデータセットで行われます。
[進行状況を確認] をクリックして、目標に沿って進んでいることを確認します。
どの新しい訪問者が戻ってきて購入を行うかを予測する
これで、モデルがこれらの 2017 年 7 月の e コマース セッションの予測を出力するようになりました。ここでは、新しく追加された 3 つのフィールドを確認できます。
Labs create a Google Cloud project and resources for a fixed time
Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
On the top left of your screen, click Start lab to begin
Use private browsing
Copy the provided Username and Password for the lab
Click Open console in private mode
Sign in to the Console
Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
Accept the terms, and skip the recovery resource page
Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project
このコンテンツは現在ご利用いただけません
利用可能になりましたら、メールでお知らせいたします
ありがとうございます。
利用可能になりましたら、メールでご連絡いたします
One lab at a time
Confirm to end all existing labs and start this one
Use private browsing to run the lab
Use an Incognito or private browser window to run this lab. This
prevents any conflicts between your personal account and the Student
account, which may cause extra charges incurred to your personal account.
このラボでは、利用可能な e コマース データセットを使用して、顧客の購買習慣を予測する分類(ロジスティック回帰)モデルを BigQuery ML で作成します。