使用 WEKA API 定义用于聚类的输入数据 [英] Define input data for clustering using WEKA API

查看:27
本文介绍了使用 WEKA API 定义用于聚类的输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对经纬度指定的点进行聚类.我正在使用 WEKA API问题在于 Instances instances = new Instances(40.01,1.02);那么,如何在不使用 ARFF 文件的情况下指定输入数据?我只想将数组读入 Instances.

import java.io.Reader;导入 weka.clusterers.ClusterEvaluation;导入 weka.clusterers.SimpleKMeans;导入 weka.core.Instances;公开课测试{/*** @param 参数*/公共静态无效主(字符串 [] args){实例实例 = 新实例(40.01,1.02);SimpleKMeans simpleKMeans = new SimpleKMeans();simpleKMeans.buildClusterer(instances);ClusterEvaluation eval = new ClusterEvaluation();eval.setClusterer(simpleKMeans);eval.evaluateClusterer(new Instances(instances));eval.clusterResultsToString();}}

解决方案

我相信您必须创建自己的实例.下面我将展示如何从具有两个属性(纬度和经度)的数组创建一个新实例.

<预><代码>导入 weka.core.Attribute;导入 weka.core.DenseInstance;导入 weka.core.FastVector;导入 weka.core.Instances;公共类 AttTest {public static void main(String[] args) 抛出异常{双[]一个={0,1,2,3};double[] 二={3,2,1,0};double[][] both=new double[2][4];两者[0]=一;两者[1]=二;实例 to_use=AttTest.buildArff(both);System.out.println(to_use.toString());}公共静态实例 buildArff(double[][] array) 抛出异常{FastVector atts = new FastVector();atts.addElement(new Attribute("lat"));//纬度atts.addElement(new Attribute("lon"));//经度//2. 创建实例对象Instances test = new Instances("location", atts, 0);//3. 填充数据for(int s1=0; s1

I want to cluster points specified by latitude and longitude. I am using WEKA API The problem is with Instances instances = new Instances(40.01,1.02); So, how to specify input data without using ARFF file? I would like just to read an array into Instances.

import java.io.Reader;

import weka.clusterers.ClusterEvaluation;
import weka.clusterers.SimpleKMeans;
import weka.core.Instances;


public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Instances instances = new Instances(40.01,1.02);

        SimpleKMeans simpleKMeans = new SimpleKMeans();
        simpleKMeans.buildClusterer(instances);

        ClusterEvaluation eval = new ClusterEvaluation();
        eval.setClusterer(simpleKMeans);
        eval.evaluateClusterer(new Instances(instances));

        eval.clusterResultsToString();
    }

}

解决方案

I believe you have to create your own instances. Below I show creating a new instances from an array with two attributes, latitude and longitude.


import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.FastVector;
import weka.core.Instances;

public class AttTest {

    public static void main(String[] args) throws Exception
    {
        double[] one={0,1,2,3};
        double[] two={3,2,1,0};
        double[][] both=new double[2][4];
        both[0]=one;
        both[1]=two;

        Instances to_use=AttTest.buildArff(both);
        System.out.println(to_use.toString());
    }

  public static Instances buildArff(double[][] array) throws Exception
  {
         FastVector      atts = new FastVector();
         atts.addElement(new Attribute("lat")); //latitude
         atts.addElement(new Attribute("lon")); //longitude

         // 2. create Instances object
         Instances test = new Instances("location", atts, 0);

         // 3. fill with data
         for(int s1=0; s1 < array[0].length; s1=s1+1)
         {
             double vals[] = new double[test.numAttributes()];
             vals[0] = array[0][s1];
             vals[1] = array[1][s1];
             test.add(new DenseInstance(1.0, vals));
         }

         return(test);
  }
}

这篇关于使用 WEKA API 定义用于聚类的输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆