This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
snippets for xai bqml and looker post #72
Open
slopp
wants to merge
2
commits into
GoogleCloudPlatform:master
Choose a base branch
from
slopp:lopp-xai-code-snippets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
11 changes: 11 additions & 0 deletions
11
finance/explainable-fraud-model-bqml-looker/create_models.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| CREATE OR REPLACE MODEL retail_banking.fraud_prediction | ||
| OPTIONS(model_type='logistic_reg', labels=['is_fraud']) AS | ||
| SELECT * EXCEPT(trans_id) | ||
| FROM retail_banking.training_data | ||
| -- Account for class imbalance. Alternatively, use AUTO_CLASS_WEIGHTS=True in the model options | ||
| WHERE (is_fraud IS TRUE) OR | ||
| (is_fraud IS NOT TRUE | ||
| AND rand() <=( | ||
| SELECT COUNTIF(is_fraud)/COUNT(*) FROM retail_banking.training_data | ||
| ) | ||
| ); |
29 changes: 29 additions & 0 deletions
29
finance/explainable-fraud-model-bqml-looker/create_training_data.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| CREATE OR REPLACE TABLE retail_banking.training_data as ( | ||
| SELECT | ||
| card_transactions.trans_id AS trans_id, | ||
| card_transactions.is_fraud AS is_fraud, | ||
| --amount for transaction: higher amounts are more likely to be fraud | ||
| cast(card_transactions.amount as FLOAT64) AS card_transactions_amount, | ||
|
|
||
| --distance from the customers home: further distances are more likely to be fraud | ||
| ST_DISTANCE((ST_GEOGPOINT((cast(card_transactions.merchant_lon as FLOAT64)), | ||
| (cast(card_transactions.merchant_lat as FLOAT64)))), | ||
| (ST_GeogPoint((cast(SPLIT(client.address,'|')[ | ||
| OFFSET | ||
| (4)] as float64)), | ||
| (cast(SPLIT(client.address,'|')[ | ||
| OFFSET | ||
| (3)] as float64))))) AS card_transactions_transaction_distance, | ||
|
|
||
| --hour that transaction occured: fraud occurs in middle of night (usually between midnight and 4 am) | ||
| EXTRACT(HOUR FROM TIMESTAMP(CONCAT(card_transactions.trans_date,' ',card_transactions.trans_time)) ) AS card_transactions_transaction_hour_of_day | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is perhaps too long. Could you try using the || operator to concat, and/or break this line into multiple lines? |
||
|
|
||
| FROM `looker-private-demo.retail_banking.card_transactions` AS card_transactions | ||
| LEFT JOIN `looker-private-demo.retail_banking.card` AS card | ||
| ON card.card_number = card_transactions.cc_number | ||
| LEFT JOIN `looker-private-demo.retail_banking.disp` AS disp | ||
| ON card.disp_id = disp.disp_id | ||
| LEFT JOIN `looker-private-demo.retail_banking.client`AS client | ||
| ON disp.client_id = client.client_id | ||
| ); | ||
21 changes: 21 additions & 0 deletions
21
finance/explainable-fraud-model-bqml-looker/explain_hypothetical_data.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| SELECT * FROM | ||
| ML.EXPLAIN_PREDICT(MODEL retail_banking.fraud_prediction, ( | ||
| SELECT | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be helpful to add a comment as to what kind of transaction each of these are ... e.g. "a sample datapoint with an unusually high transaction amount" |
||
| '001' as trans_id, | ||
| 500.00 as card_transactions_amount, | ||
| 600 as card_transactions_transaction_distance, | ||
| 2 as card_transactions_transaction_hour_of_day | ||
| UNION ALL | ||
| SELECT | ||
| '002' as trans_id, | ||
| 5.25 as card_transactions_amount, | ||
| 2 as card_transactions_transaction_distance, | ||
| 13 as card_transactions_transaction_hour_of_day | ||
| UNION ALL | ||
| SELECT | ||
| '003' as trans_id, | ||
| 175.50 as card_transactions_amount, | ||
| 45 as card_transactions_transaction_distance, | ||
| 10 as card_transactions_transaction_hour_of_day | ||
| ), STRUCT(0.55 AS threshold) | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ST_GeogPoint seems to use camelcase unexpectedly -- perhaps all caps would be better?