M
Mize-ze
Hello,
I am inserting a key and a object into a Map. later in the code I try
to retrieve the object by calling
Map.get(key) and I get a null.
I can't see where is my problem...
The application is a weka classifier.
the problem can be seen in isAcceptable(Instance) function.
First I populate a hashtable (calculateAprior method) where the key is
an instance and the value
is a double.
later I will try to fetch the double value by the key and get null (in
isAcceptable method).
I will eventually recieve a NullPointerException.
Thanks
CODE:
/*=============================buildIB3TrainingSet===========================*/
private Instances buildIB3TrainingSet(Instances instances) {
/** see implementation below <==**/
calculateAprior(instances);
double distance
=0,minDistance=Double.MAX_VALUE,classValue=Double.NaN;
/*** Sucess rate*/
successRate = new HashMap<Instance,int[]>();
Instances res = new Instances(instances,0,1);
this.addSuccessSuccessRate(res.firstInstance());
//Iterate on original
for(int i=0;i<instances.numInstances();i++)
{
Instance candidate = instances.instance(i);
Instance yInstance = null;
//Find nearest neighbor
for(int j=0;j<res.numInstances();j++)
{
Instance conceptDescription = res.instance(j);
/** see implementation below <== here is the
problem!!!!!!!!!!!**/
if(!isAcceptable(conceptDescription))
continue;
if (!conceptDescription.classIsMissing())
{
distance = distance(candidate, conceptDescription);
if (distance < minDistance)
{
minDistance = distance;
classValue = conceptDescription.classValue();
yInstance = conceptDescription;
}
}
}
if (yInstance!=null)
{
if(candidate.classValue()!=classValue&&classValue!=Double.NaN)
{
res.add(candidate);
addFailureSuccessRate(yInstance);
}
else
{
addSuccessSuccessRate(yInstance);
}
}
for(int j=0;j<res.numInstances();j++)
{
Instance conceptDescription = res.instance(j);
if(isRejected(conceptDescription))
{
res.delete(j);
successRate.remove(conceptDescription);
}
}
}
return res;
}
/*=======================calculateAprior===================================*/
/**
* calculateAprior
*
* @param instances
* @return double array of aPriory
*/
private void calculateAprior(Instances instances) {
this.aPriori = new
Hashtable<Instance,Double>(instances.numInstances());
/** count classes*/
Map<Double,Double> classCounter = new
HashMap<Double,Double>(m_Train.numClasses());
for(Enumeration enumeration =
instances.enumerateInstances();enumeration.hasMoreElements()
{
Instance ins = (Instance)enumeration.nextElement();
if (classCounter.containsKey(ins.classValue()))
{
classCounter.put(ins.classValue(), new
Double(classCounter.get(ins.classValue())+ 1));
}
else
{
classCounter.put(ins.classValue(), new Double(1));
}
}
/** Update instances a priori*/
for(Enumeration enumeration =
instances.enumerateInstances();enumeration.hasMoreElements()
{
Instance ins = (Instance)enumeration.nextElement();
aPriori.put(ins,
(double)classCounter.get(ins.classValue())
/instances.numInstances());
}
}
/*=========================isAcceptable==================================*/
/**
* is the Instance acceptable
* @param j
* @return
*/
private boolean isAcceptable(Instance instance) {
int [] inf = successRate.get(instance);
double rate = (double)inf[1]/inf[0];
/* here is the problem!!!!!!!!!!! aPriori.get(instance)
is null!!!!*/
return (rate-aPriori.get(instance)>ACCEPTENCE);
}
I am inserting a key and a object into a Map. later in the code I try
to retrieve the object by calling
Map.get(key) and I get a null.
I can't see where is my problem...
The application is a weka classifier.
the problem can be seen in isAcceptable(Instance) function.
First I populate a hashtable (calculateAprior method) where the key is
an instance and the value
is a double.
later I will try to fetch the double value by the key and get null (in
isAcceptable method).
I will eventually recieve a NullPointerException.
Thanks
CODE:
/*=============================buildIB3TrainingSet===========================*/
private Instances buildIB3TrainingSet(Instances instances) {
/** see implementation below <==**/
calculateAprior(instances);
double distance
=0,minDistance=Double.MAX_VALUE,classValue=Double.NaN;
/*** Sucess rate*/
successRate = new HashMap<Instance,int[]>();
Instances res = new Instances(instances,0,1);
this.addSuccessSuccessRate(res.firstInstance());
//Iterate on original
for(int i=0;i<instances.numInstances();i++)
{
Instance candidate = instances.instance(i);
Instance yInstance = null;
//Find nearest neighbor
for(int j=0;j<res.numInstances();j++)
{
Instance conceptDescription = res.instance(j);
/** see implementation below <== here is the
problem!!!!!!!!!!!**/
if(!isAcceptable(conceptDescription))
continue;
if (!conceptDescription.classIsMissing())
{
distance = distance(candidate, conceptDescription);
if (distance < minDistance)
{
minDistance = distance;
classValue = conceptDescription.classValue();
yInstance = conceptDescription;
}
}
}
if (yInstance!=null)
{
if(candidate.classValue()!=classValue&&classValue!=Double.NaN)
{
res.add(candidate);
addFailureSuccessRate(yInstance);
}
else
{
addSuccessSuccessRate(yInstance);
}
}
for(int j=0;j<res.numInstances();j++)
{
Instance conceptDescription = res.instance(j);
if(isRejected(conceptDescription))
{
res.delete(j);
successRate.remove(conceptDescription);
}
}
}
return res;
}
/*=======================calculateAprior===================================*/
/**
* calculateAprior
*
* @param instances
* @return double array of aPriory
*/
private void calculateAprior(Instances instances) {
this.aPriori = new
Hashtable<Instance,Double>(instances.numInstances());
/** count classes*/
Map<Double,Double> classCounter = new
HashMap<Double,Double>(m_Train.numClasses());
for(Enumeration enumeration =
instances.enumerateInstances();enumeration.hasMoreElements()
{
Instance ins = (Instance)enumeration.nextElement();
if (classCounter.containsKey(ins.classValue()))
{
classCounter.put(ins.classValue(), new
Double(classCounter.get(ins.classValue())+ 1));
}
else
{
classCounter.put(ins.classValue(), new Double(1));
}
}
/** Update instances a priori*/
for(Enumeration enumeration =
instances.enumerateInstances();enumeration.hasMoreElements()
{
Instance ins = (Instance)enumeration.nextElement();
aPriori.put(ins,
(double)classCounter.get(ins.classValue())
/instances.numInstances());
}
}
/*=========================isAcceptable==================================*/
/**
* is the Instance acceptable
* @param j
* @return
*/
private boolean isAcceptable(Instance instance) {
int [] inf = successRate.get(instance);
double rate = (double)inf[1]/inf[0];
/* here is the problem!!!!!!!!!!! aPriori.get(instance)
is null!!!!*/
return (rate-aPriori.get(instance)>ACCEPTENCE);
}