【问题标题】:clustering using weka api使用 weka api 进行聚类
【发布时间】:2017-03-05 01:00:43
【问题描述】:

我通过使用 java + weka lib 使用开源代码开始对我的数据进行聚类 它在数据集 .arff 的格式时正确运行,但我想使用 movielens 的数据集(使用他们的人口统计信息对用户进行聚类) 文件名为“u.user” 您可以在此处找到文件说明 http://files.grouplens.org/datasets/movielens/ml-100k-README.txt

这是我的代码

import weka.clusterers.SimpleKMeans;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import java.io.IOException;
public class Clustering {
    public static void main(String args[]) throws Exception{
        //load dataset
        String dataset = "C:/Users/DELL/Desktop/work/u.user";
        DataSource source = new DataSource(dataset);
        //get instances object
        Instances data = source.getDataSet();
        // new instance of clusterer
        SimpleKMeans model = new SimpleKMeans();//Simple EM (expectation maximisation)
        //number of clusters
        model.setNumClusters(4);
        //set distance function
        //model.setDistanceFunction(new weka.core.ManhattanDistance());
        // build the clusterer
        model.buildClusterer(data);
        System.out.println(model);

}
}

运行后显示此错误

Exception in thread "main" java.io.IOException: File not found : C:\Users\DELL\Desktop\work\u.names
    weka.core.converters.C45Loader.setSource(C45Loader.java:190)
    weka.core.converters.AbstractFileLoader.setFile(AbstractFileLoader.java:90)
    weka.core.converters.ConverterUtils$DataSource.reset(ConverterUtils.java:306)
    weka.core.converters.ConverterUtils$DataSource.<init>(ConverterUtils.java:141)
    Clustering.main(Clustering.java:24)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

    at weka.core.converters.C45Loader.setSource(C45Loader.java:190)
    at weka.core.converters.AbstractFileLoader.setFile(AbstractFileLoader.java:90)
    at weka.core.converters.ConverterUtils$DataSource.reset(ConverterUtils.java:306)
    at weka.core.converters.ConverterUtils$DataSource.<init>(ConverterUtils.java:141)
    at Clustering.main(Clustering.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

我确定它是因为文件的扩展,因为当我使用带有extension.arff 的其他文件时它可以工作 你能帮我如何对我的数据进行聚类吗

【问题讨论】:

    标签: java weka


    【解决方案1】:

    您还需要注意文件格式(不仅仅是扩展名)。转换数据集格式以匹配Weka ARFF format。如果您的数据为u.user,您需要将扩展​​名更改为*.arff(例如user.arff)并将格式更改为:

    @RELATION user
    
    @ATTRIBUTE id   INTEGER  % this is actually useless
    @ATTRIBUTE age  INTEGER
    @ATTRIBUTE gender   {M,F}
    @ATTRIBUTE occupation   {administrator,artist,doctor,educator,engineer,entertainment,executive,healthcare,homemaker,lawyer,librarian,marketing,none,other,programmer,retired,salesman,scientist,student,technician,writer}  % from u.occupation
    @ATTRIBUTE zipcode  STRING
    
    @DATA
    1,24,M,technician,85711
    2,53,F,other,94043
    3,23,M,writer,32067
    4,24,M,technician,43537
    5,33,F,other,15213
    6,42,M,executive,98101
    7,57,M,administrator,91344
    8,36,M,administrator,05201
    ...
    

    您应该能够将数据集解析为weka.core.Instances。但是,不幸的是,SimpleKMeans 会拒绝您的数据:

    weka.core.UnsupportedAttributeTypeException: weka.clusterers.SimpleKMeans:无法处理字符串属性!

    所以你有(至少)3个选项:

    1. 将数据的特征向量化或转换为数值(同时删除无用的数据,例如id
    2. 使用另一种可以处理分类值的聚类算法,例如weka.clusterers.HierarchicalClusterer
    3. 结合两种解决方案

    祝你好运!

    【讨论】:

    • (1) 您不需要程序来执行此操作。我仅使用简单的复制粘贴和全部替换就在不到 5 分钟的时间内转换了整个数据。 (2) 你能在定义中看到除zipcode 之外的任何其他STRING 属性吗? (3) 如果您只是希望代码运行,那么您很好。如果您希望数据正确聚类,那么您不是。这是帖子解释了为什么quora.com/Why-in-categorical-data-k-means-clustering-is-weak
    • 谢谢,你帮了我很多 1-关于选项一,如何隐藏功能是否有任何代码或工具? 2-对于选项三,我可以更改此命令 SimpleKMeans model = new SimpleKMeans();与 HierarchicalClusterer 类似,其他步骤是否相同
    • (1) 没有任何工具/代码可以直接使用,因为每个数据集的过程都是唯一的。您可以尝试的一种方法是将zipcode 转换为标称数据类型(如occupation)(2)您可以。我建议您在下次询问之前尝试一下
    • 我想说的是,k-means 不适合分类特征(在您的数据中占主导地位),正如 here 所解释的那样。但是如果你坚持使用它,你可以通过将它们映射到合理的数字来转换数据(例如,通过从邮政编码 05201 -> 5201 中删除前导零)和normalize它们
    • 关于id,您可以通过将用户的特征传递给训练好的模型来判断用户属于哪个集群。
    猜你喜欢
    • 2014-07-05
    • 2016-10-25
    • 2013-04-22
    • 2013-06-08
    • 2017-01-14
    • 2013-12-04
    • 2016-03-10
    • 2017-07-21
    • 2012-08-31
    相关资源
    最近更新 更多