📄 distribution.java
字号:
totaL = totaL + weight;
}
}
/**
* Checks if at least two bags contain a minimum number of instances.
*/
public final boolean check(double minNoObj) {
int counter = 0;
int i;
for (i=0;i<m_perBag.length;i++)
if (Utils.grOrEq(m_perBag[i],minNoObj))
counter++;
if (counter > 1)
return true;
else
return false;
}
/**
* Clones distribution (Deep copy of distribution).
*/
public final Object clone() {
int i,j;
Distribution newDistribution = new Distribution (m_perBag.length,
m_perClass.length);
for (i=0;i<m_perBag.length;i++) {
newDistribution.m_perBag[i] = m_perBag[i];
for (j=0;j<m_perClass.length;j++)
newDistribution.m_perClassPerBag[i][j] = m_perClassPerBag[i][j];
}
for (j=0;j<m_perClass.length;j++)
newDistribution.m_perClass[j] = m_perClass[j];
newDistribution.totaL = totaL;
return newDistribution;
}
/**
* Deletes given instance from given bag.
*
* @exception Exception if something goes wrong
*/
public final void del(int bagIndex,Instance instance)
throws Exception {
int classIndex;
double weight;
classIndex = (int)instance.classValue();
weight = instance.weight();
m_perClassPerBag[bagIndex][classIndex] =
m_perClassPerBag[bagIndex][classIndex]-weight;
m_perBag[bagIndex] = m_perBag[bagIndex]-weight;
m_perClass[classIndex] = m_perClass[classIndex]-weight;
totaL = totaL-weight;
}
/**
* Deletes all instances in given range from given bag.
*
* @exception Exception if something goes wrong
*/
public final void delRange(int bagIndex,Instances source,
int startIndex, int lastPlusOne)
throws Exception {
double sumOfWeights = 0;
int classIndex;
Instance instance;
int i;
for (i = startIndex; i < lastPlusOne; i++) {
instance = (Instance) source.instance(i);
classIndex = (int)instance.classValue();
sumOfWeights = sumOfWeights+instance.weight();
m_perClassPerBag[bagIndex][classIndex] -= instance.weight();
m_perClass[classIndex] -= instance.weight();
}
m_perBag[bagIndex] -= sumOfWeights;
totaL -= sumOfWeights;
}
/**
* Prints distribution.
*/
public final String dumpDistribution() {
StringBuffer text;
int i,j;
text = new StringBuffer();
for (i=0;i<m_perBag.length;i++) {
text.append("Bag num "+i+"\n");
for (j=0;j<m_perClass.length;j++)
text.append("Class num "+j+" "+m_perClassPerBag[i][j]+"\n");
}
return text.toString();
}
/**
* Sets all counts to zero.
*/
public final void initialize() {
for (int i = 0; i < m_perClass.length; i++)
m_perClass[i] = 0;
for (int i = 0; i < m_perBag.length; i++)
m_perBag[i] = 0;
for (int i = 0; i < m_perBag.length; i++)
for (int j = 0; j < m_perClass.length; j++)
m_perClassPerBag[i][j] = 0;
totaL = 0;
}
/**
* Returns matrix with distribution of class values.
*/
public final double[][] matrix() {
return m_perClassPerBag;
}
/**
* Returns index of bag containing maximum number of instances.
*/
public final int maxBag() {
double max;
int maxIndex;
int i;
max = 0;
maxIndex = -1;
for (i=0;i<m_perBag.length;i++)
if (Utils.grOrEq(m_perBag[i],max)) {
max = m_perBag[i];
maxIndex = i;
}
return maxIndex;
}
/**
* Returns class with highest frequency over all bags.
*/
public final int maxClass() {
double maxCount = 0;
int maxIndex = 0;
int i;
for (i=0;i<m_perClass.length;i++)
if (Utils.gr(m_perClass[i],maxCount)) {
maxCount = m_perClass[i];
maxIndex = i;
}
return maxIndex;
}
/**
* Returns class with highest frequency for given bag.
*/
public final int maxClass(int index) {
double maxCount = 0;
int maxIndex = 0;
int i;
if (Utils.gr(m_perBag[index],0)) {
for (i=0;i<m_perClass.length;i++)
if (Utils.gr(m_perClassPerBag[index][i],maxCount)) {
maxCount = m_perClassPerBag[index][i];
maxIndex = i;
}
return maxIndex;
}else
return maxClass();
}
/**
* Returns number of bags.
*/
public final int numBags() {
return m_perBag.length;
}
/**
* Returns number of classes.
*/
public final int numClasses() {
return m_perClass.length;
}
/**
* Returns perClass(maxClass()).
*/
public final double numCorrect() {
return m_perClass[maxClass()];
}
/**
* Returns perClassPerBag(index,maxClass(index)).
*/
public final double numCorrect(int index) {
return m_perClassPerBag[index][maxClass(index)];
}
/**
* Returns total-numCorrect().
*/
public final double numIncorrect() {
return totaL-numCorrect();
}
/**
* Returns perBag(index)-numCorrect(index).
*/
public final double numIncorrect(int index) {
return m_perBag[index]-numCorrect(index);
}
/**
* Returns number of (possibly fractional) instances of given class in
* given bag.
*/
public final double perClassPerBag(int bagIndex, int classIndex) {
return m_perClassPerBag[bagIndex][classIndex];
}
/**
* Returns number of (possibly fractional) instances in given bag.
*/
public final double perBag(int bagIndex) {
return m_perBag[bagIndex];
}
/**
* Returns number of (possibly fractional) instances of given class.
*/
public final double perClass(int classIndex) {
return m_perClass[classIndex];
}
/**
* Returns relative frequency of class over all bags with
* Laplace correction.
*/
public final double laplaceProb(int classIndex) {
return (m_perClass[classIndex] + 1) /
(totaL + (double) actualNumClasses());
}
/**
* Returns relative frequency of class for given bag.
*/
public final double laplaceProb(int classIndex, int intIndex) {
return (m_perClassPerBag[intIndex][classIndex] + 1.0) /
(m_perBag[intIndex] + (double) actualNumClasses());
}
/**
* Returns relative frequency of class over all bags.
*/
public final double prob(int classIndex) {
if (!Utils.eq(totaL, 0)) {
return m_perClass[classIndex]/totaL;
}
else {
return 0;
}
}
/**
* Returns relative frequency of class for given bag.
*/
public final double prob(int classIndex,int intIndex) {
if (Utils.gr(m_perBag[intIndex],0))
return m_perClassPerBag[intIndex][classIndex]/m_perBag[intIndex];
else
return prob(classIndex);
}
/**
* Subtracts the given distribution from this one. The results
* has only one bag.
*/
public final Distribution subtract(Distribution toSubstract) {
Distribution newDist = new Distribution(1,m_perClass.length);
newDist.m_perBag[0] = totaL-toSubstract.totaL;
newDist.totaL = newDist.m_perBag[0];
for (int i = 0; i < m_perClass.length; i++) {
newDist.m_perClassPerBag[0][i] = m_perClass[i] - toSubstract.m_perClass[i];
newDist.m_perClass[i] = newDist.m_perClassPerBag[0][i];
}
return newDist;
}
/**
* Returns total number of (possibly fractional) instances.
*/
public final double total() {
return totaL;
}
/**
* Shifts given instance from one bag to another one.
*
* @exception Exception if something goes wrong
*/
public final void shift(int from,int to,Instance instance)
throws Exception {
int classIndex;
double weight;
classIndex = (int)instance.classValue();
weight = instance.weight();
m_perClassPerBag[from][classIndex] -= weight;
m_perClassPerBag[to][classIndex] += weight;
m_perBag[from] -= weight;
m_perBag[to] += weight;
}
/**
* Shifts all instances in given range from one bag to another one.
*
* @exception Exception if something goes wrong
*/
public final void shiftRange(int from,int to,Instances source,
int startIndex,int lastPlusOne)
throws Exception {
int classIndex;
double weight;
Instance instance;
int i;
for (i = startIndex; i < lastPlusOne; i++) {
instance = (Instance) source.instance(i);
classIndex = (int)instance.classValue();
weight = instance.weight();
m_perClassPerBag[from][classIndex] -= weight;
m_perClassPerBag[to][classIndex] += weight;
m_perBag[from] -= weight;
m_perBag[to] += weight;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -