SlideShare a Scribd company logo
Design Pattern 1: Hashed Feature
Seki @tmhssk1
Machine Learning Design Patterns 読書会 #1
2021/4/13
Problem
カテゴリカル変数をOne-hotエンコーディングするには、事前に変数が取り得る値の種類を把握し
ておく必要があるが、それが不可能な場合に対応が必要となる。
・サンプリングのためにトレーニングデータ側で抽出した変数で全ての範囲を網羅できない。
・変数が取りうる値が膨⼤で、学習が困難となる、または学習できたとしても容量的な制限
が問題となる。
・モデルデプロイ後に本番環境で新規のカテゴリカル変数の値が発⽣すると対応できない。
Incomplete vocabulary
High cardinality
Cold start
例︓⾶⾏機の到着遅延を予測する問題
モデルの⼊⼒の⼀つに空港名があり、このデータセットが収集された時点では、アメリカ国内347の
空港名がカテゴリカル変数を構成する。
カテゴリカル変数の取りうる値が増える可能性がある。
このデータには下記の問題が存在する。
全期間で1〜3便しか⾶んでいない空港が存在する。
347種類の値から構成されるスパースなカテゴリカル変数である。
Incomplete vocabulary
High cardinality
Cold start
Solution
2. 変換した⽂字列に対して、学習時とテスト時の両者に適
⽤可能でランダムシードやソルトを⽤いない決定論的なハッ
シュアルゴリズムを実⾏する。
Hashed Feature
1. カテゴリカル変数の値をそれぞれ⼀意の⽂字列に変換する。
*前述の例ではIATA空港コード(3レターコード)を⽤いる
3. 典型的にはハッシュの結果を整数とし、⽬的のバケット数で
剰余演算を⾏ったあとに絶対値をとる。
https://cloud.google.com/bigquery/docs/reference/standard-sql/hash_functions?hl=ja
Why It Works
Incomplete vocabulary
フライト数が少ない空港がトレーニングデータに含まれていない場合でも、そのハッシュ化された特
徴量は10のバケットにハッシュ化した場合には0-9にコーディングされているため、Incomplete
vocabularyの問題を回避できる。
347の空港数に対して10のバケットにハッシュ化したケースでは平均して35の空港が同じハッ
シュバケットコードをており、トレーニングデータセットに含まれない空港は、バケット内の他の類
似した〜35の空港から、その特徴を「借りて」処理される。
経験則としては、各バケットに約5つのエントリーが⼊るようにハッシュバケットの数を選ぶのが良
いとされる。(例では70個のハッシュバケットが良い妥協点となる)
High cardinality
Why It Works
特定のバケットにハッシュ化することで、システムのメモリやモデルサイズの要件を実⽤的なものに
することが可能。
ハッシュバケットの数が⼗分に少なければ、High cardinalityの問題が解決されることは⾃
明。
変換アルゴリズムはデータ値とは独⽴であるため、vocabularyを保存する必要はない。
ハッシュ化により情報のロスが起きるが、変数の範囲が広すぎてモデルからの除外を検討するよ
うなケースでは許容できる妥協⼿段となり得る。
Cold start
Why It Works
新規の空港が追加された場合、最初はハッシュバケット内の他の空港に対応する予測値
が取得される。
新規の空港からのフライトが増えていった場合、定期的なモデルの再学習により新しい空港
からの到着遅延を反映するようになる。
Trade-Off and Alternatives
デザインパターンの多くは何らかのトレードオフを伴う。
Hashed Featureではモデルの予測性能をある程度犠牲にしてIncomplete
vocabulary、High cardinality、Cold startに対処していることになる。
Bucket Collision
モデルの予測性能をある程度犠牲にしてIncomplete vocabulary、High cardinality、
Cold startに対処するのがHashed Featureである。
事前にカテゴリカル変数の範囲が把握可能でサイズも限定(数百万サンプルのデータセットで
数千程度は許容範囲)されており、Cold startが問題とならないのであればHashed
Featureは⽤いるべきでない。
複数のカテゴリー⼊⼒が同じハッシュバケットの値
を共有する前提でのみHashed Featureは使
⽤できる。
Skew
カテゴリカル変数の値の分布が偏っている場合、予測性能の低下は顕著となる。
ORD(シカゴオヘア空港)からのフライトは約360万回であるのに対し、BTV(バーモント州
バーリントン空港)からのフライトは約67,000回しかない。
シカゴオヘア空港における市街地からの⻑いタクシー時間や、天候による遅延影響などの特
徴がバーモント州バーリントン空港に帰属させられることになる。
シカゴから多くのフライトが出ている影響で、BTVとMCI(カン
ザスシティ空港)のモデルの精度は⾮常に低くなることが予想
される。
これにより、空港コードをハッシュ化する際に、個々の空港に関連する情報が失われるのを
防ぐ。
カテゴリカル変数の分布が歪んでいる場合や、バケットの数が少なくてバケットの衝突が頻
繁に起こるような場合には、モデルの⼊⼒として総計的特徴を追加する。
Aggregate Feature
例えば、トレーニングデータ内で各空港ごとに定時に運航される確率を求め、それをモデル
の特徴として追加する。
バケットの衝突頻度とのトレードオフの関係から、適切なバケット数は問題⾃体に依存
することが多い。
バケット数は、ハッシュ化するカテゴリカル変数の取り得る値の数の常識的な範囲内でハ
イパーパラメータとしてチューニングする。
Hyperparameter tuning
Cryptographic hash
FARM FINGERPRINTは固定⻑(INT64は64ビット)なので、それぞれが0または1であ
る64個の特徴量を使ってバイナリエンコーディングも可能。
しかしバイナリエンコーディングでは、High cardinalityを解決するのみで、Incomplete
vocabularyとCold startに対処できない。
また、バイナリエンコーディングによる符号化はそれぞれの特徴量において容易に擬似相関を
⽣み出すため、推奨されない。
MD5を⽤いたハッシュ化によるバイナリエンコーディングでは出⼒が⼀様に分布するため、擬
似相関発⽣の問題を回避できるが、Deterministic、Uniqueの点で不⼗分であり
hashed featureには適さない。
また、MD5やSHA1のような暗号アルゴリズムは、意図的に計算コストが⾼くなるように作ら
れている点でもhashed featureには適さない。
Order of operation
CREATE TEMPORARY FUNCTION hashed(airport STRING, numbuckets INT64) AS
(ABS(MOD(FARM_FINGERPRINT(airport), numbuckets)));
先にMODを⾏い、次にABSを⾏う。
INT64の範囲は-9,223,372,036,854,775,808から
9,223,372,036,854,775,807までであり、正負で対称ではない。
ABS(FARM_FINGERPRINT(airport)) とすると、FARM_FINGERPRINT操作
が-9,223,372,036,854,775,808を返した場合、その絶対値はINT64では表現
できないため、オーバーフローエラーが発⽣する。
そのため、ハッシュ化された特徴量の列を使⽤する際には、L2正則化を使⽤して、空のバ
ケットに関連する重みがゼロに近くなるようにすることが有益である。
Empty hash buckets
347の空港を表現するために10個のハッシュバケットを選んだとしても、そのうちの1つのハッ
シュバケットが空になる可能性がわずかに存在する。
これにより、Incomplete vocabularyに該当する空港が空のバケットに⼊っても、モデル
が数値的に不安定になることを回避できる。

More Related Content

Recently uploaded

03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
QlikPresalesJapan
 
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
QlikPresalesJapan
 
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
QlikPresalesJapan
 
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
QlikPresalesJapan
 
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
QlikPresalesJapan
 
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
hfujii2
 

Recently uploaded (6)

03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
03 Qlik Cloud データ統合 しっかり学ぶ勉強会 #3 データ変換 タスク監視
 
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
02_2_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データマートの作成」
 
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
02_3_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「データの管理(簡単なデータ変換)」
 
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
02_4_20240611_Qlik Cloud データ統合 しっかり学ぶ勉強会 #2 - データパイプラインの作成「系列と影響分析」
 
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
02_1_20240611_Qlik Cloudデータ統合_データパイプラインの作成「基本動作」
 
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
株式会社アーシャルデザイン_事業説明資料_システム開発_受託開発_WEB開発_Salesforce
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Machine learning design pattern hashed feature