📄 nnge.java
字号:
if (!inst.isMissing(i))
numNotMissingAttr++;
}
if(numNotMissingAttr == 0){
return 0;
} else {
return sum / (double) (numNotMissingAttr * numNotMissingAttr);
}
}
/**
* Return the weight of the Examplar
*
* @return the weight of the Examplar.
*/
private double weight(){
return ((double) (m_PositiveCount + m_NegativeCount)) / ((double) m_PositiveCount);
}
/**
* Return the class of the Exemplar
*
* @return the class of this exemplar as a double (weka format)
*/
private double classValue(){
return m_ClassValue;
}
/**
* Returns the value of the inf border of the Exemplar.
*
* @param attrIndex the index of the attribute
* @return the value of the inf border for this attribute
* @exception Exception is thrown either if the attribute is nominal or if the Exemplar is empty
*/
private double getMinBorder(int attrIndex) throws Exception {
if(!attribute(attrIndex).isNumeric())
throw new Exception("Exception.getMinBorder : not numeric attribute !");
if(numInstances() == 0)
throw new Exception("Exception.getMinBorder : empty Exemplar !");
return m_MinBorder[attrIndex];
}
/**
* Returns the value of the sup border of the hyperrectangle
* Returns NaN if the HyperRectangle doesn't have any border for this attribute
*
* @param attrIndex the index of the attribute
* @return the value of the sup border for this attribute
* @exception Exception is thrown either if the attribute is nominal or if the Exemplar is empty
*/
private double getMaxBorder(int attrIndex) throws Exception {
if(!attribute(attrIndex).isNumeric())
throw new Exception("Exception.getMaxBorder : not numeric attribute !");
if(numInstances() == 0)
throw new Exception("Exception.getMaxBorder : empty Exemplar !");
return m_MaxBorder[attrIndex];
}
/**
* Returns the number of positive classifications
*
* @return the number of positive classifications
*/
private int getPositiveCount(){
return m_PositiveCount;
}
/**
* Returns the number of negative classifications
*
* @return the number of negative classifications
*/
private int getNegativeCount(){
return m_NegativeCount;
}
/**
* Set the number of positive classifications
*
* @param value an integer value (greater than 0 is wise...)
*/
private void setPositiveCount(int value) {
m_PositiveCount = value;
}
/**
* Set the number of negative classifications
*
* @param value an integer value
*/
private void setNegativeCount(int value) {
m_NegativeCount = value;
}
/**
* Increment the number of positive Classifications
*/
private void incrPositiveCount(){
m_PositiveCount++;
}
/**
* Increment the number of negative Classifications
*/
private void incrNegativeCount(){
m_NegativeCount++;
}
/**
* Returns true if the Exemplar is empty (i.e. doesn't yield any Instance)
*
* @return true if the Exemplar is empty, false otherwise
*/
private boolean isEmpty(){
return (numInstances() == 0);
}
/**
* Returns a description of this Exemplar
*
* @return A string that describes this Exemplar
*/
private String toString2(){
String s;
Enumeration enu = null;
s = "Exemplar[";
if (numInstances() == 0) {
return s + "Empty]";
}
s += "{";
enu = enumerateInstances();
while(enu.hasMoreElements()){
s = s + "<" + enu.nextElement().toString() + "> ";
}
s = s.substring(0, s.length()-1);
s = s + "} {" + toRules() + "} p=" + m_PositiveCount + " n=" + m_NegativeCount + "]";
return s;
}
/**
* Returns a string of the rules induced by this examplar
*
* @return a string of the rules induced by this examplar
*/
private String toRules(){
if (numInstances() == 0)
return "No Rules (Empty Exemplar)";
String s = "", sep = "";
for(int i = 0; i < numAttributes(); i++){
if(i == classIndex())
continue;
if(attribute(i).isNumeric()){
if(m_MaxBorder[i] != m_MinBorder[i]){
s += sep + m_MinBorder[i] + "<=" + attribute(i).name() + "<=" + m_MaxBorder[i];
} else {
s += sep + attribute(i).name() + "=" + m_MaxBorder[i];
}
sep = " ^ ";
} else {
s += sep + attribute(i).name() + " in {";
String virg = "";
for(int j = 0; j < attribute(i).numValues() + 1; j++){
if(m_Range[i][j]){
s+= virg;
if(j == attribute(i).numValues())
s += "?";
else
s += attribute(i).value(j);
virg = ",";
}
}
s+="}";
sep = " ^ ";
}
}
s += " ("+numInstances() +")";
return s;
}
}
/** An empty instances to keep the headers, the classIndex, etc... */
private Instances m_Train;
/** The list of Exemplars */
private Exemplar m_Exemplars;
/** The lists of Exemplars by class */
private Exemplar m_ExemplarsByClass[];
/** The minimum values for numeric attributes. */
double [] m_MinArray;
/** The maximum values for numeric attributes. */
double [] m_MaxArray;
/** The number of try for generalisation */
private int m_NumAttemptsOfGene = 5;
/** The number of folder for the Mutual Information */
private int m_NumFoldersMI = 5;
/** Values to use for missing value */
private double [] m_MissingVector;
/** MUTUAL INFORMATION'S DATAS */
/* numeric attributes */
private int [][][] m_MI_NumAttrClassInter;
private int [][] m_MI_NumAttrInter;
private double [] m_MI_MaxArray;
private double [] m_MI_MinArray;
/* nominal attributes */
private int [][][] m_MI_NumAttrClassValue;
private int [][] m_MI_NumAttrValue;
/* both */
private int [] m_MI_NumClass;
private int m_MI_NumInst;
private double [] m_MI;
/** MAIN FUNCTIONS OF THE CLASSIFIER */
/**
* Generates a classifier. Must initialize all fields of the classifier
* that are not being set via options (ie. multiple calls of buildClassifier
* must always lead to the same result). Must not change the dataset
* in any way.
*
* @param data set of instances serving as training data
* @exception Exception if the classifier has not been
* generated successfully
*/
public void buildClassifier(Instances data) throws Exception {
/* check the data */
if (data.checkForStringAttributes()) {
throw new UnsupportedAttributeTypeException("Cannot handle string attributes!");
}
if (!data.attribute(data.classIndex()).isNominal()) {
throw new UnsupportedAttributeTypeException("Class type must be nominal!");
}
// Make a copy of the instances
data = new Instances(data);
/* initialize the classifier */
m_Train = new Instances(data, 0);
m_Exemplars = null;
m_ExemplarsByClass = new Exemplar[m_Train.numClasses()];
for(int i = 0; i < m_Train.numClasses(); i++){
m_ExemplarsByClass[i] = null;
}
m_MaxArray = new double[m_Train.numAttributes()];
m_MinArray = new double[m_Train.numAttributes()];
for(int i = 0; i < m_Train.numAttributes(); i++){
m_MinArray[i] = Double.POSITIVE_INFINITY;
m_MaxArray[i] = Double.NEGATIVE_INFINITY;
}
m_MI_MinArray = new double [data.numAttributes()];
m_MI_MaxArray = new double [data.numAttributes()];
m_MI_NumAttrClassInter = new int[data.numAttributes()][][];
m_MI_NumAttrInter = new int[data.numAttributes()][];
m_MI_NumAttrClassValue = new int[data.numAttributes()][][];
m_MI_NumAttrValue = new int[data.numAttributes()][];
m_MI_NumClass = new int[data.numClasses()];
m_MI = new double[data.numAttributes()];
m_MI_NumInst = 0;
for(int cclass = 0; cclass < data.numClasses(); cclass++)
m_MI_NumClass[cclass] = 0;
for (int attrIndex = 0; attrIndex < data.numAttributes(); attrIndex++) {
if(attrIndex == data.classIndex())
continue;
m_MI_MaxArray[attrIndex] = m_MI_MinArray[attrIndex] = Double.NaN;
m_MI[attrIndex] = Double.NaN;
if(data.attribute(attrIndex).isNumeric()){
m_MI_NumAttrInter[attrIndex] = new int[m_NumFoldersMI];
for(int inter = 0; inter < m_NumFoldersMI; inter++){
m_MI_NumAttrInter[attrIndex][inter] = 0;
}
} else {
m_MI_NumAttrValue[attrIndex] = new int[data.attribute(attrIndex).numValues() + 1];
for(int attrValue = 0; attrValue < data.attribute(attrIndex).numValues() + 1; attrValue++){
m_MI_NumAttrValue[attrIndex][attrValue] = 0;
}
}
m_MI_NumAttrClassInter[attrIndex] = new int[data.numClasses()][];
m_MI_NumAttrClassValue[attrIndex] = new int[data.numClasses()][];
for(int cclass = 0; cclass < data.numClasses(); cclass++){
if(data.attribute(attrIndex).isNumeric()){
m_MI_NumAttrClassInter[attrIndex][cclass] = new int[m_NumFoldersMI];
for(int inter = 0; inter < m_NumFoldersMI; inter++){
m_MI_NumAttrClassInter[attrIndex][cclass][inter] = 0;
}
} else if(data.attribute(attrIndex).isNominal()){
m_MI_NumAttrClassValue[attrIndex][cclass] = new int[data.attribute(attrIndex).numValues() + 1];
for(int attrValue = 0; attrValue < data.attribute(attrIndex).numValues() + 1; attrValue++){
m_MI_NumAttrClassValue[attrIndex][cclass][attrValue] = 0;
}
}
}
}
m_MissingVector = new double[data.numAttributes()];
for(int i = 0; i < data.numAttributes(); i++){
if(i == data.classIndex()){
m_MissingVector[i] = Double.NaN;
} else {
m_MissingVector[i] = data.attribute(i).numValues();
}
}
/* update the classifier with data */
Enumeration enu = data.enumerateInstances();
while(enu.hasMoreElements()){
update((Instance) enu.nextElement());
}
}
/**
* Classifies a given instance.
*
* @param instance the instance to be classified
* @return index of the predicted class as a double
* @exception Exception if instance could not be classified
* successfully
*/
public double classifyInstance(Instance instance) throws Exception {
/* check the instance */
if (m_Train.equalHeaders(instance.dataset()) == false){
throw new Exception("NNge.classifyInstance : Incompatible instance types !");
}
Exemplar matched = nearestExemplar(instance);
if(matched == null){
throw new Exception("NNge.classifyInstance : NNge hasn't been trained !");
}
return matched.classValue();
}
/**
* Updates the classifier using the given instance.
*
* @param instance the instance to include
* @exception Exception if instance could not be incorporated
* successfully
*/
public void updateClassifier(Instance instance) throws Exception {
if (m_Train.equalHeaders(instance.dataset()) == false) {
throw new Exception("Incompatible instance types");
}
update(instance);
}
/** HIGH LEVEL SUB-FUNCTIONS */
/**
* Performs the update of the classifier
*
* @param instance the new instance
* @exception Exception if the update fails
*/
private void update(Instance instance) throws Exception {
if (instance.classIsMissing()) {
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -