SlideShare a Scribd company logo
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Table of Contents
1 Problemy klientów Nethone
2 Przykładowe metryki
3 Poszukiwania modelu
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Table of Contents
1 Problemy klientów Nethone
2 Przykładowe metryki
3 Poszukiwania modelu
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Na czym polega fraud płatniczy
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Źródła danych
• Dane dotyczące płatności
• Inne dane transakcyjne
• Charakterystyki behawioralne
• Informacje sprzętowe
• Otoczenie sieciowe
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Jak sobie z nim radzimy w Nethone
• Wzbogacanie i czyszczenie danych
• Selekcja cech
• XGBoost
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Table of Contents
1 Problemy klientów Nethone
2 Przykładowe metryki
3 Poszukiwania modelu
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Dane wejściowe
1 import numpy as np
2
3 pred = np . a r r a y ( [ 0 . 1 , 0.3 , 0.2 , 0.4 , 0.8 , 0 . 9 ] )
4 t r u e = np . a r r a y ( [ 0 , 1 , 0 , 0 , 1 , 1 ] )
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Accuracy
Maksymalizacja Accuracy
1 def accuracy ( pred , true , t r e s h o l d ) :
2 p o s i t i v e = pred > t r e s h o l d
3 leng th = p o s i t i v e . shape [ 0 ]
4 l o s s = 1.0 ∗ np . sum( p o s i t i v e == t r u e ) / len gth
5 r e t u r n l o s s
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Recall
Maksymalizacja recall przy zadanym precentylu odcięcia ruchu
1 def r e c l o s s ( pred , true , p e r c e n t i l e ) :
2 t r e s h o l d = i n t ( t r u e . shape [ 0 ] ∗ p e r c e n t i l e )
3 zipped = z i p ( pred , t r u e )
4 s o r t e d = s o r t e d ( zipped , key=lambda x : x [ 0 ] )
5 p o s i t i v e = s o r t e d [ t r e s h o l d : ]
6 p o s i t i v e f l a g s = z i p (∗ p o s i t i v e ) [ 1 ]
7 l o s s = 1.0 ∗ np . sum( p o s i t i v e f l a g s ) / np . sum( t r u e )
8 r e t u r n l o s s
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
False Positive Rate
Minimalizacja FPR przy ustalonym odsetku fraudu
1 def FPR loss ( pred , true , max TP rate ) :
2 zipped = z i p ( pred , t r u e )
3 s o r t e d = s o r t e d ( zipped , key=lambda x : x [ 0 ] )
4 unzipped = z i p (∗ s o r t e d )
5 cumsum = 1.0 ∗ np . cumsum( unzipped [ 1 ] )
6 cumsum /= ( np . arange (cumsum . shape [ 0 ] ) + 1)
7 t r y :
8 l a s t i n d = np . where (cumsum < max TP rate )
[0][ −1]
9 except I n d e x E r r o r :
10 l a s t i n d = cumsum . shape [ 0 ] − 1
11 t r e s h o l d = unzipped [ 0 ] [ l a s t i n d ]
12 p o s i t i v e = pred >= t r e s h o l d
13 l o s s = np . mean( t r u e [ p o s i t i v e ] == 0)
14 r e t u r n l o s s
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Table of Contents
1 Problemy klientów Nethone
2 Przykładowe metryki
3 Poszukiwania modelu
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Ogólne podejście
• Minimalizacja RMSE lub LL
• Grid Search
• Randomized Search
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Optymalizacja Bayesowska
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Hyperopt
Potrzebne biblioteki
1 import numpy as np
2 from xgboost import X G B C l a s s i f i e r
3 from hyperopt import tpe , hp , fmin
4 from s k l e a r n . d a t a s e t s import m a k e c l a s s i f i c a t i o n
5 from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Hyperopt
Przestrzeń przeszukiwania
1 param dist = [
2 hp . r a n d i n t ( ’ max depth ’ , 25) ,
3 hp . uniform ( ’ l e a r n i n g r a t e ’ , 0.001 , 0 . 1 ) ,
4 hp . r a n d i n t ( ’ n e s t i m a t o r s ’ , 800) , ]
5
6 param names = [
7 ’ max depth ’ ,
8 ’ l e a r n i n g r a t e ’ ,
9 ’ n e s t i m a t o r s ’ ]
10
11 model = X G B C l a s s i f i e r ()
12
13 X, y = m a k e c l a s s i f i c a t i o n ( n samples =1000,
14 n f e a t u r e s =20, n i n f o r m a t i v e =3,
15 n redundant =2, f l i p y =0.3 ,
16 random state =4)
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Hyperopt
Definicja problemu
1 def model score (X, y , params ) :
2 X train , X test , y t r a i n , y t e s t = t r a i n t e s t s p l i t
(X, y , t e s t s i z e =0.33 , random state =42)
3 params = d i c t ( z i p ( param names , params ) )
4 model . set params (∗∗ params )
5 model . f i t ( X train , y t r a i n )
6 y pred = model . p r e d i c t p r o b a ( X test ) [ : , 1]
7 s c o r e = FPR loss ( y pred , y t e s t , 0 . 1)
8 p r i n t params , s c o r e
9 r e t u r n s c o r e
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Hyperopt
Minimalizacja
1 np . random . seed (1)
2 best params = fmin (
3 lambda params : model score (X, y , params ) ,
4 space=param dist ,
5 max evals =5,
6 algo=tpe . suggest , r s t a t e=np . random )
7
8 # { ’ l e a r n i n g r a t e ’ : 0.05214379590289195 ,
9 # ’ max depth ’ : 3 ,
10 # ’ n e s t i m a t o r s ’ : 683}
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
XGboost
Dane i parametry
1 import numpy as np
2 import xgboost as xgb
3
4 d t r a i n = xgb . DMatrix (X [ : 5 0 0 , : ] , l a b e l=y [ : 5 0 0 ] )
5 d t e s t = xgb . DMatrix (X [ 5 0 0 : , : ] , l a b e l=y [ 5 0 0 : ] )
6
7 param = { ’ max depth ’ : 2 , ’ eta ’ : 1 , ’ s i l e n t ’ : 1}
8 w a t c h l i s t = [ ( dtest , ’ e v a l ’ ) , ( dtrain , ’ t r a i n ’ ) ]
9 num round = 20
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
XGBoost
Definicja funkcji straty
1 def l o g r e g o b j ( preds , d t r a i n ) :
2 l a b e l s = d t r a i n . g e t l a b e l ()
3 preds = 1.0 / ( 1 .0 + np . exp(−preds ) )
4 grad = preds − l a b e l s
5 hess = preds ∗ (1.0 − preds )
6 r e t u r n grad , hess
7
8 def e v a l e r r o r ( preds , d t r a i n ) :
9 l a b e l s = d t r a i n . g e t l a b e l ()
10 r e t u r n ’ e r r o r ’ , f l o a t (sum( l a b e l s != ( preds > 0 .0 ) ) )
/ l e n ( l a b e l s )
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
XGBoost
Minimalizacja funkcji straty
1 bst = xgb . t r a i n ( param , dtrain , num round , w a t c h l i s t ,
logregobj , e v a l e r r o r )
2
3 # [ 0 ] eval −e r r o r : 0 . 4 1 4 t r a i n −e r r o r : 0 . 3 6
4 # [ 1 ] eval −e r r o r : 0 . 2 4 2 t r a i n −e r r o r : 0 . 2 1 4
5 # [ 2 ] eval −e r r o r : 0 . 2 4 6 t r a i n −e r r o r : 0 . 1 9 6
6 # [ 3 ] eval −e r r o r : 0 . 2 4 6 t r a i n −e r r o r : 0 . 1 8 6
7 # [ 4 ] eval −e r r o r : 0 . 2 7 6 t r a i n −e r r o r : 0 . 1 6 6
8 # [ 5 ] eval −e r r o r : 0 . 2 7 6 t r a i n −e r r o r : 0 . 1 4 8
9 # [ 6 ] eval −e r r o r : 0 . 2 8 6 t r a i n −e r r o r : 0 . 1 4
10 # [ 7 ] eval −e r r o r : 0 . 2 9 6 t r a i n −e r r o r : 0 . 1 4
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Co dalej?
Uciąglenie funkcji straty
1 def s o f t i n d ( x , t r e s h o l d , s c a l e ) :
2 r e t u r n 1. / ( 1 . + np . exp(− s c a l e ∗ ( x − t r e s h o l d ) ) )
3
4 def TP( prob , l a b e l ) :
5 r e t u r n l a b e l ∗ s o f t i n d ( prob , 0.5 , s c a l e )
6
7 def TN( prob , l a b e l ) :
8 r e t u r n (1 − l a b e l ) ∗ s o f t i n d ((1 − prob ) , 0.5 ,
s c a l e )
9
10 def FN( prob , l a b e l ) :
11 r e t u r n l a b e l ∗ s o f t i n d (1 − prob , 0.5 , s c a l e )
12
13 def FP( prob , l a b e l ) :
14 r e t u r n (1 − l a b e l ) ∗ s o f t i n d ( prob , 0.5 , s c a l e )
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Na koniec
• Dobra definicja funkcji (FPR)
• XGBoost potrzebuje pochodnej per przykład (Precision)
• AUC! (RankOpt)
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu
Koniec
Dziękuję
Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p

More Related Content

Featured

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
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Project for Public Spaces & National Center for Biking and Walking
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 

Featured (20)

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...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Jak optymalizować modele predykcyjne pod konkretne problemy biznesowe

  • 1.
  • 2. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Table of Contents 1 Problemy klientów Nethone 2 Przykładowe metryki 3 Poszukiwania modelu Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 3. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Table of Contents 1 Problemy klientów Nethone 2 Przykładowe metryki 3 Poszukiwania modelu Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 4. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Na czym polega fraud płatniczy Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 5. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Źródła danych • Dane dotyczące płatności • Inne dane transakcyjne • Charakterystyki behawioralne • Informacje sprzętowe • Otoczenie sieciowe Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 6. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Jak sobie z nim radzimy w Nethone • Wzbogacanie i czyszczenie danych • Selekcja cech • XGBoost Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 7. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Table of Contents 1 Problemy klientów Nethone 2 Przykładowe metryki 3 Poszukiwania modelu Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 8. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Dane wejściowe 1 import numpy as np 2 3 pred = np . a r r a y ( [ 0 . 1 , 0.3 , 0.2 , 0.4 , 0.8 , 0 . 9 ] ) 4 t r u e = np . a r r a y ( [ 0 , 1 , 0 , 0 , 1 , 1 ] ) Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 9. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Accuracy Maksymalizacja Accuracy 1 def accuracy ( pred , true , t r e s h o l d ) : 2 p o s i t i v e = pred > t r e s h o l d 3 leng th = p o s i t i v e . shape [ 0 ] 4 l o s s = 1.0 ∗ np . sum( p o s i t i v e == t r u e ) / len gth 5 r e t u r n l o s s Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 10. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Recall Maksymalizacja recall przy zadanym precentylu odcięcia ruchu 1 def r e c l o s s ( pred , true , p e r c e n t i l e ) : 2 t r e s h o l d = i n t ( t r u e . shape [ 0 ] ∗ p e r c e n t i l e ) 3 zipped = z i p ( pred , t r u e ) 4 s o r t e d = s o r t e d ( zipped , key=lambda x : x [ 0 ] ) 5 p o s i t i v e = s o r t e d [ t r e s h o l d : ] 6 p o s i t i v e f l a g s = z i p (∗ p o s i t i v e ) [ 1 ] 7 l o s s = 1.0 ∗ np . sum( p o s i t i v e f l a g s ) / np . sum( t r u e ) 8 r e t u r n l o s s Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 11. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu False Positive Rate Minimalizacja FPR przy ustalonym odsetku fraudu 1 def FPR loss ( pred , true , max TP rate ) : 2 zipped = z i p ( pred , t r u e ) 3 s o r t e d = s o r t e d ( zipped , key=lambda x : x [ 0 ] ) 4 unzipped = z i p (∗ s o r t e d ) 5 cumsum = 1.0 ∗ np . cumsum( unzipped [ 1 ] ) 6 cumsum /= ( np . arange (cumsum . shape [ 0 ] ) + 1) 7 t r y : 8 l a s t i n d = np . where (cumsum < max TP rate ) [0][ −1] 9 except I n d e x E r r o r : 10 l a s t i n d = cumsum . shape [ 0 ] − 1 11 t r e s h o l d = unzipped [ 0 ] [ l a s t i n d ] 12 p o s i t i v e = pred >= t r e s h o l d 13 l o s s = np . mean( t r u e [ p o s i t i v e ] == 0) 14 r e t u r n l o s s Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 12. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Table of Contents 1 Problemy klientów Nethone 2 Przykładowe metryki 3 Poszukiwania modelu Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 13. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Ogólne podejście • Minimalizacja RMSE lub LL • Grid Search • Randomized Search Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 14. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Optymalizacja Bayesowska Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 15. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Hyperopt Potrzebne biblioteki 1 import numpy as np 2 from xgboost import X G B C l a s s i f i e r 3 from hyperopt import tpe , hp , fmin 4 from s k l e a r n . d a t a s e t s import m a k e c l a s s i f i c a t i o n 5 from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 16. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Hyperopt Przestrzeń przeszukiwania 1 param dist = [ 2 hp . r a n d i n t ( ’ max depth ’ , 25) , 3 hp . uniform ( ’ l e a r n i n g r a t e ’ , 0.001 , 0 . 1 ) , 4 hp . r a n d i n t ( ’ n e s t i m a t o r s ’ , 800) , ] 5 6 param names = [ 7 ’ max depth ’ , 8 ’ l e a r n i n g r a t e ’ , 9 ’ n e s t i m a t o r s ’ ] 10 11 model = X G B C l a s s i f i e r () 12 13 X, y = m a k e c l a s s i f i c a t i o n ( n samples =1000, 14 n f e a t u r e s =20, n i n f o r m a t i v e =3, 15 n redundant =2, f l i p y =0.3 , 16 random state =4) Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 17. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Hyperopt Definicja problemu 1 def model score (X, y , params ) : 2 X train , X test , y t r a i n , y t e s t = t r a i n t e s t s p l i t (X, y , t e s t s i z e =0.33 , random state =42) 3 params = d i c t ( z i p ( param names , params ) ) 4 model . set params (∗∗ params ) 5 model . f i t ( X train , y t r a i n ) 6 y pred = model . p r e d i c t p r o b a ( X test ) [ : , 1] 7 s c o r e = FPR loss ( y pred , y t e s t , 0 . 1) 8 p r i n t params , s c o r e 9 r e t u r n s c o r e Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 18. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Hyperopt Minimalizacja 1 np . random . seed (1) 2 best params = fmin ( 3 lambda params : model score (X, y , params ) , 4 space=param dist , 5 max evals =5, 6 algo=tpe . suggest , r s t a t e=np . random ) 7 8 # { ’ l e a r n i n g r a t e ’ : 0.05214379590289195 , 9 # ’ max depth ’ : 3 , 10 # ’ n e s t i m a t o r s ’ : 683} Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 19. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu XGboost Dane i parametry 1 import numpy as np 2 import xgboost as xgb 3 4 d t r a i n = xgb . DMatrix (X [ : 5 0 0 , : ] , l a b e l=y [ : 5 0 0 ] ) 5 d t e s t = xgb . DMatrix (X [ 5 0 0 : , : ] , l a b e l=y [ 5 0 0 : ] ) 6 7 param = { ’ max depth ’ : 2 , ’ eta ’ : 1 , ’ s i l e n t ’ : 1} 8 w a t c h l i s t = [ ( dtest , ’ e v a l ’ ) , ( dtrain , ’ t r a i n ’ ) ] 9 num round = 20 Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 20. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu XGBoost Definicja funkcji straty 1 def l o g r e g o b j ( preds , d t r a i n ) : 2 l a b e l s = d t r a i n . g e t l a b e l () 3 preds = 1.0 / ( 1 .0 + np . exp(−preds ) ) 4 grad = preds − l a b e l s 5 hess = preds ∗ (1.0 − preds ) 6 r e t u r n grad , hess 7 8 def e v a l e r r o r ( preds , d t r a i n ) : 9 l a b e l s = d t r a i n . g e t l a b e l () 10 r e t u r n ’ e r r o r ’ , f l o a t (sum( l a b e l s != ( preds > 0 .0 ) ) ) / l e n ( l a b e l s ) Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 21. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu XGBoost Minimalizacja funkcji straty 1 bst = xgb . t r a i n ( param , dtrain , num round , w a t c h l i s t , logregobj , e v a l e r r o r ) 2 3 # [ 0 ] eval −e r r o r : 0 . 4 1 4 t r a i n −e r r o r : 0 . 3 6 4 # [ 1 ] eval −e r r o r : 0 . 2 4 2 t r a i n −e r r o r : 0 . 2 1 4 5 # [ 2 ] eval −e r r o r : 0 . 2 4 6 t r a i n −e r r o r : 0 . 1 9 6 6 # [ 3 ] eval −e r r o r : 0 . 2 4 6 t r a i n −e r r o r : 0 . 1 8 6 7 # [ 4 ] eval −e r r o r : 0 . 2 7 6 t r a i n −e r r o r : 0 . 1 6 6 8 # [ 5 ] eval −e r r o r : 0 . 2 7 6 t r a i n −e r r o r : 0 . 1 4 8 9 # [ 6 ] eval −e r r o r : 0 . 2 8 6 t r a i n −e r r o r : 0 . 1 4 10 # [ 7 ] eval −e r r o r : 0 . 2 9 6 t r a i n −e r r o r : 0 . 1 4 Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 22. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Co dalej? Uciąglenie funkcji straty 1 def s o f t i n d ( x , t r e s h o l d , s c a l e ) : 2 r e t u r n 1. / ( 1 . + np . exp(− s c a l e ∗ ( x − t r e s h o l d ) ) ) 3 4 def TP( prob , l a b e l ) : 5 r e t u r n l a b e l ∗ s o f t i n d ( prob , 0.5 , s c a l e ) 6 7 def TN( prob , l a b e l ) : 8 r e t u r n (1 − l a b e l ) ∗ s o f t i n d ((1 − prob ) , 0.5 , s c a l e ) 9 10 def FN( prob , l a b e l ) : 11 r e t u r n l a b e l ∗ s o f t i n d (1 − prob , 0.5 , s c a l e ) 12 13 def FP( prob , l a b e l ) : 14 r e t u r n (1 − l a b e l ) ∗ s o f t i n d ( prob , 0.5 , s c a l e ) Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 23. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Na koniec • Dobra definicja funkcji (FPR) • XGBoost potrzebuje pochodnej per przykład (Precision) • AUC! (RankOpt) Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p
  • 24. Problemy klientów Nethone Przykładowe metryki Poszukiwania modelu Koniec Dziękuję Piotr Tempczyk Jak optymalizować modele predykcyjne pod konkretne p