JAVA 使用weka进行KNN预测

本文详细介绍如何使用Weka库中的KNN算法进行实例搜索。通过解析Maven依赖配置,展示如何读取ARFF文件并利用LinearNNSearch类获取指定数量的最近邻样本。示例代码展示了完整的操作流程,包括数据集加载、距离计算及邻居查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

maven

<dependency>
      <groupId>nz.ac.waikato.cms.weka</groupId>
      <artifactId>weka-stable</artifactId>
      <version>3.8.3</version>
    </dependency>

以线性查询为例,别的实现方法替换对应的实现类即可

/**
     *
     * @param sourceIns 表示你在哪个数据集中找邻居
     * @param target 找邻居的样本
     * @param kNN 多少邻居
     * @return k个邻居
     */
    public static Instances getKNeighbour(Instances sourceIns, Instance target, int kNN)
    {
        Instances neighbours = null;
        try{
            EuclideanDistance dfunc = new EuclideanDistance();
            LinearNNSearch lnn = new LinearNNSearch();
            lnn.setDistanceFunction(dfunc);
            lnn.setInstances( sourceIns );
            lnn.addInstanceInfo(target);
            neighbours = lnn.kNearestNeighbours(target, kNN);
        }catch(Exception e){
            System.out.println("Util.buildKdTree(LinearNNSearch m_NNSearch,Instance mean, int m_kNN) is wrong!");
        }
        return neighbours;
    }

把arff文件读取成Instances

private static Instances getFileInstances(String fileName) throws Exception {
        ConverterUtils.DataSource frData = new ConverterUtils.DataSource( fileName );
        return frData.getDataSet();
    }

跑一个例子:

public static void main(String[] args) throws Exception {
    String path = "D:\\poi_test.arff";
    Instances instances = getFileInstances(path);
    System.out.println("instances:" + instances);
    ArrayList<Attribute> atts = new ArrayList<>();
    atts.add(new Attribute("x", Attribute.NUMERIC));
    atts.add(new Attribute("y", Attribute.NUMERIC));
    atts.add(new Attribute("class", Attribute.NUMERIC));
    Instances df = new Instances("predictData", atts, 0);
    df.setClassIndex(df.numAttributes() - 1);
    Instance target = new DenseInstance(2);
    target.setValue(0, 116.41);
    target.setValue(1, 39.95);
    System.out.println("target:" + target);
    df.add(target);
    System.out.println("=============knn=================:");
    Instances result = getKNeighbour(instances, target, 3);
    System.out.println("result:" + result);
}

数据:

@relation distance 

@attribute x NUMERIC
@attribute y NUMERIC
@attribute class NUMERIC

@data
116.415668,39.953726,0
116.438614,39.930745,0
116.426367,39.928047,0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值