📄 kl.java
字号:
if (value2 > 0) { attrValues[counter] = value1 * Math.log(value1/value2) / LOG2; if (m_useIDivergence) { attrValues[counter] += value2; } } } if (m_useIDivergence) { attrValues[counter] -= value1; } } else { // add the class value if (instance1.classValue() == instance2.classValue()) { attrValues[counter] = 1; } else { attrValues[counter] = -1; } } counter++; } SparseInstance diffInstance = new SparseInstance(1.0, attrValues, indices, instance1.dataset().numAttributes()); diffInstance.setDataset(instance1.dataset()); // if i-divergence is used, need to pick up "orphan" values of instance2 if (m_useIDivergence) { for (int i = 0; i < numValues2; i++) { int attrIdx = instance2.index(i); if (attrIdx != m_classIndex) { double value2 = instance2.valueSparse(i); int idx1 = instance1.locateIndex(attrIdx); if (idx1 < 0 || attrIdx != instance1.index(idx1)) { diffInstance.setValue(attrIdx, value2); } } } } return diffInstance; } /** * Create an instance with features corresponding to dot-product components of the two given instances * @param instance1 first sparse instance * @param instance2 second non-sparse instance */ protected Instance createDiffInstanceSparseNonSparse (SparseInstance instance1, Instance instance2) { int numValues1 = instance1.numValues(); int numValues2 = instance2.numValues(); Instance diffInstance = null; double[] values2 = instance2.toDoubleArray(); int classIndex = instance1.classIndex(); if (m_useIDivergence) { diffInstance = new Instance(instance2); // "orphan values" from instance2 will be copied here for (int i = 0; i < numValues1; i++) { int idx = instance1.index(i); if (idx != classIndex) { // skip class attributes double value1 = instance1.valueSparse(i); if (values2[idx] != 0) { diffInstance.setValue(idx, value1 * Math.log(value1/values2[idx]) / LOG2 - value1); } else { System.err.println("\n\n\nPROBLEM! Zero value in non-sparse instance in createDiffInstanceSparseNonSparse\n\n\n"); diffInstance.setValue(idx, -value1); } } else { // add the class value if (instance1.classValue() == instance2.classValue()) { diffInstance.setClassValue(1); } else { diffInstance.setClassValue(-1); } } } } else { // I-Divergence term is not used int counter = 0; double [] attrValues = new double[numValues1]; int [] indices = new int[numValues1]; Arrays.fill(attrValues, 0); Arrays.fill(indices, Integer.MAX_VALUE); for (int i = 0; i < numValues1; i++) { int attrIdx = instance1.index(i); indices[counter] = attrIdx; if (attrIdx != classIndex) { // skip class attributes double value1 = instance1.valueSparse(i); if (values2[attrIdx] > 0) { attrValues[counter] = value1 * Math.log(value1/values2[attrIdx]); } } else { // add the class value if (instance1.classValue() == instance2.classValue()) { attrValues[counter] = 1; } else { attrValues[counter] = -1; } } counter++; } diffInstance = new SparseInstance(1.0, attrValues, indices, instance1.dataset().numAttributes()); } diffInstance.setDataset(instance1.dataset()); return diffInstance; } /** * Create a nonsparse instance with features corresponding to dot-product components of the two given instances * @param instance1 first nonsparse instance * @param instance2 second nonsparse instance */ protected Instance createDiffInstanceNonSparse (Instance instance1, Instance instance2) { double[] values1 = instance1.toDoubleArray(); double[] values2 = instance2.toDoubleArray(); int numAttributes = values1.length; // create an extra attribute if there was no class index originally int classIndex = instance1.classIndex(); if (classIndex < 0) { classIndex = numAttributes; numAttributes++; } double[] diffInstanceValues = new double[numAttributes]; for (int i = 0; i < values1.length; i++) { if (i != classIndex) { // round up to float significance to be able to weed out duplicates later if (values1[i] != 0 && values2[i] != 0) { diffInstanceValues[i] = values1[i] * Math.log(values1[i]/values2[i]) / LOG2; if (m_useIDivergence) { diffInstanceValues[i] += values2[i] - values1[i]; } } else if (values2[i] != 0) { diffInstanceValues[i] = values2[i]; } else if (values1[i] != 0) { diffInstanceValues[i] = Double.MAX_VALUE; } } else { // class value if (values1[i] == values2[i]) { diffInstanceValues[i] = 1; } else { diffInstanceValues[i] = -1; } } } Instance diffInstance = new Instance(1.0, diffInstanceValues); diffInstance.setDataset(instance1.dataset()); return diffInstance; } /** * Create an instance with features corresponding to JS components * @param instance1 first instance * @param instance2 second instance */ public Instance createDiffInstanceJS (Instance instance1, Instance instance2) { if (instance1 instanceof SparseInstance && instance2 instanceof SparseInstance) { return createDiffInstanceJSSparse((SparseInstance)instance1, (SparseInstance)instance2); } else if (instance1 instanceof SparseInstance) { return createDiffInstanceJSSparseNonSparse((SparseInstance)instance1, instance2); } else if (instance2 instanceof SparseInstance) { return createDiffInstanceJSSparseNonSparse((SparseInstance)instance2, instance1); } else { return createDiffInstanceJSNonSparse(instance1, instance2); } } /** * Create a sparse instance with features corresponding to dot-product components of the two given instances * @param instance1 first sparse instance * @param instance2 second sparse instance */ protected SparseInstance createDiffInstanceJSSparse (SparseInstance instance1, SparseInstance instance2) { SparseInstance diffInstanceJS = null; // try to look up this constraint in the hash if (m_instanceConstraintMap.containsKey(instance1)) { HashMap instanceDiffInstanceMap = (HashMap) m_instanceConstraintMap.get(instance1); if (instanceDiffInstanceMap.containsKey(instance2)) { diffInstanceJS = (SparseInstance) instanceDiffInstanceMap.get(instance2); } } // if the lookup failed, compute it and hash it if (diffInstanceJS == null) { System.err.println("\n\n\nThis should not happen! Could not look up instance1 in createDiffInstanceJSSparse!\n\n\n\n"); int numValues1 = instance1.numValues(); int numValues2 = instance2.numValues(); int maxNumValues = numValues1 + numValues2; // the overall number of attributes double [] attrValues = new double[maxNumValues]; int [] indices = new int[maxNumValues]; Arrays.fill(attrValues, 0); Arrays.fill(indices, Integer.MAX_VALUE); int classIndex = instance1.classIndex(); int counter = 0, counter1 = 0, counter2 = 0, attrIdx1, attrIdx2; while (counter1 < numValues1 || counter2 < numValues2) { if (counter1 < numValues1) { attrIdx1 = instance1.index(counter1); } else { attrIdx1 = Integer.MAX_VALUE; } if (counter2 < numValues2) { attrIdx2 = instance2.index(counter2); } else { attrIdx2 = Integer.MAX_VALUE; } while (attrIdx1 < attrIdx2 && counter1 < numValues1) { if (attrIdx1 != m_classIndex) { attrValues[counter] = 0.5 * instance1.valueSparse(counter1); indices[counter] = attrIdx1; counter++; } counter1++; if (counter1 < numValues1) { attrIdx1 = instance1.index(counter1); } } while (attrIdx2 < attrIdx1 && counter2 < numValues2) { if (attrIdx2 != m_classIndex) { attrValues[counter] = 0.5 * instance2.valueSparse(counter2); indices[counter] = attrIdx2; counter++; } counter2++; if (counter2 < numValues2) { attrIdx2 = instance2.index(counter2); } } if (attrIdx1 == attrIdx2 && attrIdx1 != m_classIndex && attrIdx1 < Integer.MAX_VALUE && attrIdx2 < Integer.MAX_VALUE) { double value1 = instance1.valueSparse(counter1); double value2 = instance2.valueSparse(counter2); attrValues[counter] = 0.5 * (value1 * Math.log(value1) + value2 * Math.log(value2) - (value1 + value2) * Math.log((value1+value2)/2.0)) / LOG2; indices[counter] = attrIdx1; counter++; counter1++; counter2++; } else if (attrIdx1 == m_classIndex) { if (instance1.classValue() == instance2.classValue()) { attrValues[counter] = 1; } else { attrValues[counter] = -1; } indices[counter] = m_classIndex; counter++; counter1++; counter2++; } } diffInstanceJS = new SparseInstance(1.0, attrValues, indices, instance1.dataset().numAttributes()); diffInstanceJS.setDataset(instance1.dataset()); // hash the diff-instance for both instances involved HashMap instanceDiffInstanceMap1; if (m_instanceConstraintMap.containsKey(instance1)) { instanceDiffInstanceMap1 = (HashMap) m_instanceConstraintMap.get(instance1); } else { instanceDiffInstanceMap1 = new HashMap(); m_instanceConstraintMap.put(instance1, instanceDiffInstanceMap1); } instanceDiffInstanceMap1.put(instance2, diffInstanceJS); HashMap instanceDiffInstanceMap2; if (m_instanceConstraintMap.containsKey(instance2)) { instanceDiffInstanceMap2 = (HashMap) m_instanceConstraintMap.get(instance2); } else { instanceDiffInstanceMap2 = new HashMap(); m_instanceConstraintMap.put(instance2, instanceDiffInstanceMap2); } instanceDiffInstanceMap2.put(instance1, diffInstanceJS); } return diffInstanceJS; } /** * Create an instance with features corresponding to dot-product components of the two given instances * @param instance1 first sparse instance * @param instance2 second non-sparse instance */ protected Instance createDiffInstanceJSSparseNonSparse (SparseInstance instance1, Instance instance2) { int numValues = instance2.numValues(); int classIndex = instance1.classIndex(); double [] attrValues = new double[numValues]; double[] values2 = instance2.toDoubleArray(); // add all contributions of the second instance; unnecessary ones will overwritten later for (int i = 0; i < values2.length; i++) { if (i != m_classIndex) { attrValues[i] = 0.5 * values2[i]; } } for (int i = 0; i < instance1.numValues(); i++) { Attribute attribute = instance1.attributeSparse(i); int attrIdx = attribute.index(); if (attrIdx != m_classIndex) { double value1 = instance1.value(attrIdx); double value2 = values2[attrIdx]; if (value1 != 0 && value2 != 0) { attrValues[attrIdx] = 0.5 * (value1 * Math.log(value1) + value2 * Math.log(value2) - (value1 + value2) * Math.log((value1+value2)/2.0)) / LOG2; } else if (value1 != 0) { attrValues[attrIdx] = 0.5 * value1; } else if (value2 != 0) { attrValues[attrIdx] = 0.5 * value2; } } else { // class index if (instance1.classValue() == instance2.classValue()) { attrValues[attrIdx] = 1; } else { attrValues[attrIdx] = -1; } } } Instance diffInstanceJS = new Instance(1.0, attrValues); diffInstanceJS.setDataset(instance1.dataset()); return diffInstanceJS; } /** * Create a nonsparse instance with features corresponding to dot-product components of the two given instances * @param instance1 first nonsparse instance * @param instance2 second nonsparse instance */ protected Instance createDiffInstanceJSNonSparse (Instance instance1, Instance instance2) { double[] values1 = instance1.toDoubleArray(); double[] values2 = instance2.toDoubleArray(); int numAttributes = values1.length; // create an extra attribute if there was no class index originally int classIndex = instance1.classIndex(); if (classIndex < 0) { classIndex = numAttributes; numAttributes++; } double[] diffInstanceJSValues = new double[numAttributes]; for (int i = 0; i < values1.length; i++) { if (i != classIndex) { // round up to float significance to be able to weed out duplicates later if (values2[i] != 0 && values1[i] != 0) { diffInstanceJSValues[i] = 0.5 * (values1[i] * Math.log(values1[i]) + values2[i] * Math.log(values2[i]) - (values1[i] + values2[i]) * Math.log((values1[i]+values2[i])/2.0)) / LOG2; } else if (values1[i] != 0) { diffInstanceJSValues[i] = 0.5 * values1[i]; } else if (values2[i] != 0) { diffInstanceJSValues[i] = 0.5 * values2[i]; } } else { // class value if (values1[i] == values2[i]) { diffInstanceJSValues[i] = 1; } else { diffInstanceJSValues[i] = -1; } } } Instance diffInstanceJS = new Instance(1.0, diffInstanceJSValues); diffInstanceJS.setDataset(instance1.dataset()); return diffInstanceJS; } /** * Set the type of distance to similarity conversion. Values other * than CONVERSION_LAPLACIAN, CONVERSION_UNIT, or CONVERSION_EXPONENTIAL will be ignored * * @param type type of the similarity to distance conversion to use */ public void setConversionType(SelectedTag conversionType) { if (conversionType.getTags() == TAGS_CONVERSION) { m_conversionType = conversionType.getSelectedTag().getID(); } } /** * return the type of distance to similarity conversion * @return one of CONVERSION_LAPLACIAN, CONVERSION_UNIT, or CONVERSION_EXPONENTIAL */ public SelectedTag getConversionType() { return new SelectedTag(m_conversionType, TAGS_CONVERSION); } /** * Set the type of smoothing * * @param type type of smoothing */ public void setSmoothingType(SelectedTag smoothingType) { if (smoothingType.getTags() == TAGS_SMOOTHING) { m_smoothingType = smoothingType.getSelectedTag().getID(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -