-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcmc_example.py
More file actions
31 lines (22 loc) · 902 Bytes
/
mcmc_example.py
File metadata and controls
31 lines (22 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy
from sklearn import datasets
from sklearn.model_selection import train_test_split
from discretesampling.base.algorithms import DiscreteVariableMCMC
from discretesampling.domain import decision_tree as dt
data = datasets.load_wine()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=5)
a = 0.01
b = 5
target = dt.TreeTarget(a, b)
initialProposal = dt.TreeInitialProposal(X_train, y_train)
# Create an MCMC sampler on type dt.Tree with target distribution target
dtMCMC = DiscreteVariableMCMC(dt.Tree, target, initialProposal)
try:
treeSamples = dtMCMC.sample(N=1000)
mcmcLabels = dt.stats(treeSamples.samples[500:999], X_test).predict(X_test)
mcmc_acc = dt.accuracy(y_test, mcmcLabels)
print(numpy.mean(mcmc_acc))
except ZeroDivisionError:
print("MCMC sampling failed due to division by zero")