Hi,
I have managed to read the data from a file, learn the naive bayes network and perform inference. After I have performed the inference I want to see what the probability distribution is for the parent node. The code I have for this is pasted below, but it gives a segmentation fault... Does anybody know what's wrong with it and/or the best way to know the values of the new distribution? Also, what is the difference between the checked and unchecked value? Which should I use?
DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Value()->GetMatrix();
DSL_sysCoordinates coords(*probmatrix);
theNames = theNet.GetNode(gaithandle)->Definition()->GetOutcomesNames();
coords.GoFirst();
int gaitindex;
for( int i=1; i<=NUM_GAITS; i++ ){
sprintf(s,"State_%d",i);
gaitindex = theNames->FindPosition(s);
coords.GoTo(gaitindex);
printf("Gait \t %d \t coord index \t %d \t checked \t %f \t unchecked \t %f \n",i,gaitindex,coords.CheckedValue(),coords.UncheckedValue());
}
Thank you very much for the help!
How do I access the values of the prior distribution?
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Re: How do I access the values of the prior distribution?
Probably neither It's quite old option, but still survives in the docs. You can simply iterate over any DSL_Dmatrix with zero-based index. If you need to use multi-dimensional coords, specify them with DSL_intArray.Also, what is the difference between the checked and unchecked value? Which should I use?
Anyway, priors should be acessed with a snippet like the one below - note a call to Definition(), not Value():
DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Definition()->GetMatrix();
int count = probmatrix->GetSize();
for (int i = 0; i < count; i ++) printf("%d %g\n", i, (*probmatrix));
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Yes. The numbers in this matrix will be only if you call DSL_network::UpdateBeliefs first, and inference will succeed. To ensure that posteriors you're accessing are valid, use this:ermutarra wrote:By the way, if I use
DSL_Dmatrix* probmatrix = theNet.GetNode(gaithandle)->Value()->GetMatrix();
instead of "Definition", then I get the posterior distribution, right?
Code: Select all
DSL_nodeValue *v = net.GetNode(handle);
if (v->IsValueValid())
{
DSL_Dmatrix *mtx = v->GetMatrix();
// ... read contents of mtx here
}
Hi Shooltz,
You mention in a previous post of this thread that I should use DSL_intArray in I need to use multi-dimensional coords.
Say I have learned my model with Smilearn. It is a naive bayes classifier. So I can see the prior for the parent node as you pointed out in the previous post. Great!
Now I want to see the CPT for one of the children nodes, how do I do this? Could you provide a code snippet? And how can I see the states of the child node? I get the list in the form of an DSL_idArray but I dont know how to access the individual states.
The code I have at the moment is:
probmatrix = theNet.GetNode(disthandle)->Definition()->GetMatrix();
int count = (probmatrix->GetSize())/NUM_GAITS;
int n=0; double probs[NUM_GAITS][count];
for( int i=0; i<NUM_GAITS; i++ ){
for( int j=0; j<count; j++ ){
probs[j] = (*probmatrix)[n];
n++;
}
}
DSL_idArray *theNames;
theNames = theNet.GetNode(disthandle)->Definition()->GetOutcomesNames();
file << endl << "D\\G ";
for( int i=0; i<NUM_GAITS; i++ ){ file << i << " "; }; file << endl;
for( int j=0; j<count; j++ ){
file << theNames->GetId(j) << " "; // HOW DO I GET WHAT STATE THIS PROB corresponds to???
for( int i=0; i<NUM_GAITS; i++ ){
file << probs[j] << " ";
}
file << endl;
}
Thank you very much!!
You mention in a previous post of this thread that I should use DSL_intArray in I need to use multi-dimensional coords.
Say I have learned my model with Smilearn. It is a naive bayes classifier. So I can see the prior for the parent node as you pointed out in the previous post. Great!
Now I want to see the CPT for one of the children nodes, how do I do this? Could you provide a code snippet? And how can I see the states of the child node? I get the list in the form of an DSL_idArray but I dont know how to access the individual states.
The code I have at the moment is:
probmatrix = theNet.GetNode(disthandle)->Definition()->GetMatrix();
int count = (probmatrix->GetSize())/NUM_GAITS;
int n=0; double probs[NUM_GAITS][count];
for( int i=0; i<NUM_GAITS; i++ ){
for( int j=0; j<count; j++ ){
probs[j] = (*probmatrix)[n];
n++;
}
}
DSL_idArray *theNames;
theNames = theNet.GetNode(disthandle)->Definition()->GetOutcomesNames();
file << endl << "D\\G ";
for( int i=0; i<NUM_GAITS; i++ ){ file << i << " "; }; file << endl;
for( int j=0; j<count; j++ ){
file << theNames->GetId(j) << " "; // HOW DO I GET WHAT STATE THIS PROB corresponds to???
for( int i=0; i<NUM_GAITS; i++ ){
file << probs[j] << " ";
}
file << endl;
}
Thank you very much!!
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
I don't think this will compile. You'll need constant expression as array dimension; the 'count' variable is not constant.ermutarra wrote:The code I have at the moment is:
probmatrix = theNet.GetNode(disthandle)->Definition()->GetMatrix();
int count = (probmatrix->GetSize())/NUM_GAITS;
int n=0; double probs[NUM_GAITS][count];
And how can I see the states of the child node? I get the list in the form of an DSL_idArray but I dont know how to access the individual states.
Please clarify what do you mean by 'accessing the individual states'. Are you interested in the probabilities for i-th state for different parent configurations?
I actually managed to get that code to work.
The method I was looking for to know which state the probability corresponds to is
theNames->Subscript(j)
So all is well, I was just wondering whether there is an easier/shorter way to accomplish the same thing as that code. But I'm not very bothered about it now that it works.
Thanks!
The method I was looking for to know which state the probability corresponds to is
theNames->Subscript(j)
So all is well, I was just wondering whether there is an easier/shorter way to accomplish the same thing as that code. But I'm not very bothered about it now that it works.
Thanks!
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
You can use (*theNames)[j] as well. Or use the reference (not pointer) to DSL_idArray:ermutarra wrote:The method I was looking for to know which state the probability corresponds to is
theNames->Subscript(j)
const DSL_idArray &theNames = *(theNet.GetNode(disthandle)->Definition()->GetOutcomesNames());
cout << theNames[j];