⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 knearestneighbor_extend.java

📁 Description: FASBIR(Filtered Attribute Subspace based Bagging with Injected Randomness) is a variant
💻 JAVA
字号:
package fasbir.classifiers;

import java.util.Calendar;
import java.util.Random;

import weka.core.Instances;
import weka.core.Randomizable;

 /**
 * <p>Description: extend KNearestNeighbor to enable random distance metric</p>
 * @author Y. Yu (yuy@lamda.nju.edu.cn), LAMDA Group (http://lamda.nju.edu.cn/)
 * @version 1.0
 */
public class KNearestNeighbor_Extend
    extends KNearestNeighbor implements Randomizable {
    protected boolean m_useRandomDistanceOrder = false;
    protected double m_dist_p = 2, m_dist_q = 1;
    protected int m_Seed;

    /**
     * set randomization seed
     * @param seed int
     */
    public void setSeed(int seed) {
        this.m_Seed = seed;
    }

    /**
     * get randomization seed
     * @return int
     */
    public int getSeed() {
        return m_Seed;
    }

    /**
     * set Minkowsky distance order and VDM distance order
     * @param p int Minkowsky distance order
     * @param q int VDM distance order
     */
    public void setDistanceOrder(int p, int q) {
        m_dist_p = p;
        m_dist_q = q;
    }

    /**
     * decide whether use random distance order
     * @param use boolean [code]true[/code] to use random order
     */
    public void setUseRandomDistanceOrder(boolean use) {
        m_useRandomDistanceOrder = use;
    }

    /**
     * calculate distance according to given order
     * @param point1 double[] first instance
     * @param point2 double[] second instance
     * @return double distance
     */
    protected double distanceCalc(double[] point1, double[] point2) {
        double dist = 0;
        for (int d = 0; d < m_sampleDimension; d++) {
            // deal with missing value
            if (Double.isNaN(point1[d]) || Double.isNaN(point2[d]))
                continue;
            if (m_nominalAttribute[d]) {
                double tmp = 0;
                for (int i = 0; i < m_numClasses; i++) {
                    tmp += Math.pow( Math.abs( (m_probability[d][ (int) (point1[d] - m_minValue[d])][i] - m_probability[d][ (int) (point2[d] - m_minValue[d])][i])), m_dist_q);
                }
                dist += Math.abs(tmp);
            }
            else {
                dist += Math.pow(Math.abs(point1[d] - point2[d]), m_dist_p);
            }
        }
        dist = Math.pow(Math.abs(dist), 1 / m_dist_p);

        return dist;
    }

    /**
     * train a kNN classifier
     * @param tsample Instances
     * @throws Exception
     */
    public void buildClassifier(Instances tsample) throws java.lang.Exception {
        Random realrnd = new Random(Calendar.getInstance().getTime().getTime());

        int dimension = tsample.numAttributes() - 1;

        // random order
        if (this.m_useRandomDistanceOrder) {
            this.m_dist_p = realrnd.nextInt(3) + 1;
            this.m_dist_q = realrnd.nextInt(3) + 1;
        }

        // call KNearestNeighbor's buildClassifier
        super.buildClassifier(tsample);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -