About Me

My photo
Just wasting time sharing knowledge about, Big Data and Analytics
Showing posts with label Bayesian. Show all posts
Showing posts with label Bayesian. Show all posts

Aug 5, 2013

Classifieur Naïf Bayésien

Introduction

Le classifieur naïf bayésien est l'une des méthodes les plus simples en apprentissage supervisé basée sur le théorème de Bayes.
il est peu utilisé par les praticiens du data mining au détriment des méthodes traditionnelles que sont les arbres de décision ou les régressions logistiques.
Un avantage de cette méthode est la simplicité de programmation, la facilité d'estimation des paramètres et sa rapidité (même sur de très grandes bases de données). Malgré ses avantages, son peu d'utilisation en pratique vient en partie du fait que ne disposant pas d'un modèle explicite simple (l'explication de probabilité conditionnelle à priori), l'intérêt pratique d'une telle technique et remise en question.

Le classifieur naïf bayésien : L'indépendance conditionnelle

On considère deux variables aléatoires X et Y. Et on essaye de prédire Y à partir de X. Imaginons par exemple que :
Y = “Acheter” ou “ Ne pas acheter” X = “Cher” ou “ Moins cher”
Acheter Ne pas acheter
Cher 50 60
Pas cher 120 90
La règle de Bayes indique que :
\(P( Y = Acheter | X =cher ) =P( Y = Acheter et X = cher ) / P( X = cher)\) ou encore \[P( Y = Ne pas acheter | X = cher ) = P( Y = Ne pas acheter et X = cher )/ P( X = cher )\]
La règle de décision sera de dire : Si la première proabilité est plus grande, alors l'achat sera effectué.
Le classifieur naïf bayésien découle directement de cette formule. Ainsi donc, \(P( Y = Acheter | X =cher ) =P(X= cher et Y = Acheter ) * P( Y= Acheter) P( X = cher)\)
Le terme naïf désigne ici le fait que les descripteurs sont conditionnellement indépendants. Ce qui en pratique n'est presque jamais vrai. Mais cela ne remet pas en cause les résultats comme on peut le lire ici : http://www.cs.iastate.edu/~honavar/bayes-lewis.pdf
En résumé :
Pour mettre en place un classifieur naïf de Bayes :
  • On détermine un ensemble d'apprentissage
  • On détermine des probabilités à priori de chaque classe (par exemple en observant les effectifs)
  • On applique la règle de Bayes : \(P(y=c/X=x)= P(X=x/Y=c) * P(Y=c) /(PX=x)\) pour obtenir la probabilité à posteriori des classes au point x.
  • On choisit la classe la plus probable.
Dans la formule de Bayes :
\(P(Y=c)\) : La proabilité à priori
\(P(X=x/Y=c)\) : La probailité conditionnelle d'appartenance à la classe c, qui peut être interprété comme la vraisemblance de cette classe.

Mise en oeuvre sous R

Le logiciel R fournit à travers plusieurs packages des fonctions pour mettre en oeuvre une classification à l'aide d'un bayésien naïf.
Nous allons utiliser le package e1071.
Pour une application, nous allons utiliser un jeu de données de G. Saporta qui contient les données relatives aux pronostics de survie suite à un infacrtus des patients selon des cractéristiques observées telles que : la fréquence cardiaque, le nombre de pulsations, etc…
rm(list = ls())
setwd("F:/NaivesBayes")
library(e1071)
## Loading required package: class

# Les données
survie <- read.table(file = "Data/survie.txt", header = T)
head(survie)
##   FRCAR INCAR INSYS PRDIA PAPUL PVENT REPUL  PRONO
## 1    90  1.71  19.0    16  19.5  16.0   912 SURVIE
## 2    90  1.68  18.7    24  31.0  14.0  1476  DECES
## 3   120  1.40  11.7    23  29.0   8.0  1657  DECES
## 4    82  1.79  21.8    14  17.5  10.0   782 SURVIE
## 5    80  1.58  19.7    21  28.0  18.5  1418  DECES
## 6    80  1.13  14.1    18  23.5   9.0  1664  DECES
# Echantillon d'apprentissage & de test
set.seed(1)
n = dim(survie)[1]
index = sample(n, 0.7 * n)
Appren = survie[index, ]
Test = survie[-index, ]
# Modélisation
nb.model <- naiveBayes(PRONO ~ ., data = Appren)
l'implémentation du modèle est simple en utilisant la fonction. Les résultats en sortie sont les suivants
## 
## Naive Bayes Classifier for Discrete Predictors
## 
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
## 
## A-priori probabilities:
## Y
##  DECES SURVIE 
## 0.3878 0.6122 
## 
## Conditional probabilities:
##         FRCAR
## Y         [,1]  [,2]
##   DECES  92.16 14.87
##   SURVIE 87.13 14.69
## 
##         INCAR
## Y         [,1]   [,2]
##   DECES  1.366 0.3519
##   SURVIE 2.353 0.6219
## 
##         INSYS
## Y         [,1]  [,2]
##   DECES  15.01 3.834
##   SURVIE 27.93 9.500
## 
##         PRDIA
## Y         [,1]  [,2]
##   DECES  21.42 5.409
##   SURVIE 15.03 4.560
## 
##         PAPUL
## Y         [,1]  [,2]
##   DECES  28.55 7.695
##   SURVIE 20.93 5.519
## 
##         PVENT
## Y         [,1]  [,2]
##   DECES  11.87 4.216
##   SURVIE  8.75 3.997
## 
##         REPUL
## Y          [,1]  [,2]
##   DECES  1777.0 630.9
##   SURVIE  766.3 292.9
La qualité du modèle dépend de sa capacité à bien classer dans le jeu de données test
PRONOTH <- predict(object = nb.model, newdata = Test)
Test.mod <- cbind(Test, PRONOTH)
head(Test.mod, 5)
##   FRCAR INCAR INSYS PRDIA PAPUL PVENT REPUL  PRONO PRONOTH
## 2    90  1.68  18.7    24  31.0  14.0  1476  DECES   DECES
## 3   120  1.40  11.7    23  29.0   8.0  1657  DECES   DECES
## 5    80  1.58  19.7    21  28.0  18.5  1418  DECES   DECES
## 6    80  1.13  14.1    18  23.5   9.0  1664  DECES   DECES
## 9    78  2.16  27.7    15  20.5  11.5   759 SURVIE  SURVIE
tail(Test.mod, 5)
##    FRCAR INCAR INSYS PRDIA PAPUL PVENT REPUL  PRONO PRONOTH
## 50    75  1.21  16.1    19    24     4  1587  DECES   DECES
## 51    80  2.41  30.9    19    24     7   797 SURVIE  SURVIE
## 64   100  1.76  17.6    23    33     2  1500 SURVIE   DECES
## 70    87  2.51  28.8    16    24    20   765  DECES  SURVIE
## 71   100  2.31  23.1     8    12     1   416 SURVIE  SURVIE
# Taux de bien classé
(Confusion = table(Test.mod$PRONO, Test.mod$PRONOTH))
##         
##          DECES SURVIE
##   DECES      9      1
##   SURVIE     2     10
# En pourcetages:
round(prop.table(Confusion), 2)
##         
##          DECES SURVIE
##   DECES   0.41   0.05
##   SURVIE  0.09   0.45
Soit un taux de bien classé de 86.3636 %
Comparons la classification naïve bayésienne à des méthodes traditionnelles comme une régession logistique
log.model <- glm(PRONO ~ ., data = Appren, family = "binomial")
log.model
## 
## Call:  glm(formula = PRONO ~ ., family = "binomial", data = Appren)
## 
## Coefficients:
## (Intercept)        FRCAR        INCAR        INSYS        PRDIA  
##   -36.27863      0.22251     -0.22697      1.25918     -0.41212  
##       PAPUL        PVENT        REPUL  
##    -0.49393      0.26358      0.00785  
## 
## Degrees of Freedom: 48 Total (i.e. Null);  41 Residual
## Null Deviance:       65.4 
## Residual Deviance: 20.2  AIC: 36.2
LOG.PRONOTH <- predict(object = log.model, newdata = Test, type = "response")
Log.test <- cbind(Test, LOG.PRONOTH = ifelse(LOG.PRONOTH >= 0.5, "SURVIE", "DECES"))
# Taux de bien classé
(LOG.Confusion = table(Log.test$PRONO, Log.test$LOG.PRONOTH))
##         
##          DECES SURVIE
##   DECES      9      1
##   SURVIE     2     10
# En pourcetages:
round(prop.table(LOG.Confusion), 2)
##         
##          DECES SURVIE
##   DECES   0.41   0.05
##   SURVIE  0.09   0.45
Soit un taux de bien classé de 86.3636 %
On peut par ailleurs vérifier si les erreurs sont commises au même endroit, ce qui indiquerait le même comportement des algorithmes : Ce qui est bien le cas
(ConfMethode = table(Log.test$LOG.PRONOTH, Test.mod$PRONOTH))
##         
##          DECES SURVIE
##   DECES     11      0
##   SURVIE     0     11
A faire :
  • Tester si ce comportement est identique sur une volumétrie plus importante.
  • D'autres classifieurs naïfs
  • Implémenter manuellement un classifieur naïf
  • Détailler un exemple numérique sur un exemple

Jul 16, 2013

What are my chances to talk to this girl? Fisher or Bayes

Robert Mathews said that : "Ronald Fisher gave scientists a mathematical machine for turning baloney into breakthroughs, and ukes into funding. It is time to pull the plug.". He's right.
In one previous life, I wrote a thesis in Philosophy. But, a specific area, Epistemology also called
theory of knowledge, because, It questions what knowledge is and how it can be acquired,
and the extent to which any given subject or entity can be known.

My thesis deal about : The tradition, since Cournot in applying mathematical modelling to social sphere and more specific, how the climate modelling interact with interdiscplinary source of knowledge (mathematics, physics,geography, philosophy).

After reading this : http://www.academia.edu/1075253/Climate_Change_Epistemic_Trust_and_Expert_Trustworthiness 
It seems that the use of bayesian statistics is misunderstood.

Assume two thesis :
  • According to the Bayesian's statistic is the science who deal about the degree of of proofs in the observations. That means that, Bayesian statistics self-contained paradigm providing tools and techniques for all statistical problems.
  • In the classical frequentist view point of statistical theory, a statistical  procedure is judged by averaging its performance over all possible data
However, the bayesian approach gives prime importance to how a given procedure performs for
the actual data observed in a given situation.

The core of this theory have been formalised by Popper (Karl) with two main principles :
  1. Knowledge cannot start from nothing — from a tabula rasa – nor yet from observation. The advance of knowledge consists, mainly, in the modification of earlier knowledge. Although we may sometimes, for example in archaeology, advance through a chance observation,the significance of the discovery will usually depend upon its power to modify our earlier theories.
  2. Any probability is a degree of belief about something; It's not a property.That means that any scientific model produce data absolutely or conditionnaly on probability. It's possible to measure how the data can modify the degrees of belief (baye's principle).
One of the result of frequentist theory, may be the most criticized, is the fisher s p-value.


A problem can be to evaluate how the diploma is important to get in the first job

Hypothese :

- The diploma has no effect on the first job. In frequentist, we compute the p-value that can be
interpreted as the probability to  observe a difference at least as  important observed in the data if our hypothesis is true.

Read more: http://www.answers.com/topic/bayesian-statistics#ixzz2UKZDkq3v

So, let us talk about the p-value  and this problem :
If you cross seven times a beautiful girl each day for  10 days at the same place, can you conclude that she is always there? And then, tomorrow, youalways have a chance to speak with her.
We're going to examine both approach : Fisher (based on p_value) or Jeffreys (Bayesian)

  • With Fisher, we have :
H0 : p=0.5 and H1 :p=0.5
where p is "the probability to cross the girl". If we reject H0 (fisher) we can conclude that the girl is always there.
the p_value is :


> 2*pbinom(3,10,0.5)
0.34375
which is greater than 5% (the arbitrary threshold that everybody likes). indeed, we cannot conclude... Thank's Fisher; I can't trust you. 

With Bayes, we take our two hypothesis again and we suppose p(H0)=p(H1)=0.5. We have the same chance to cross her every day. With bayesian approach, wa need to compute the likelihood,  wich require, a priori distribution, that can be sum up (in our example) by "your a priori belief of cross again and to talk to her"


Let s compute this :
> (p <- c(0.5,0.6,0.7))
[1] 0.5 0.6 0.7
> (apriori <- c(0.5,0.3,0.2))
[1] 0.5 0.3 0.2
> (vraisemblance <- dbinom(7,10,p))
[1] 0.1171875 0.2149908 0.2668279
> (loi.jointe <- apriori*vraisemblance)
[1] 0.05859375 0.06449725 0.05336559
> (p.y <- sum(loi.jointe))
[1] 0.1764566
> (aposteriori <- loi.jointe/p.y)
[1] 0.3320576 0.3655134 0.3024290
 
So, the conditionnal probability p(H0/y)=is 33% and P(H1/y) = 66% .
There are two out of three chance that the girl is always there. In other words;
I have 66% of chances to cross her tomorrow.
Bayesian approach is hopeful. I like it; I can take my time with the girl next door.