开始研究WEKA,一个开源java的数据挖掘工具

开始研究WEKA,一个开源java的数据挖掘工具。

HS沉寂这么多天,谁知道偏偏在我申请离职的时候给我安排了个任务,哎,无语。

于是,今天看了一天的Weka。

主要是看了HS提供的三个文章(E文,在google的帮助下看完的):

 

http://www.ibm.com/developerworks/opensource/library/os-weka1/index.html
http://www.ibm.com/developerworks/opensource/library/os-weka2/index.html
http://www.ibm.com/developerworks/opensource/library/os-weka3/index.html
 
还有一个中文文档: http://www.doc88.com/p-881687347690.html
 
还有一个E文的PPT(http://www.cs.ccsu.edu/~markov/weka-tutorial.pdf),还没仔细看。里面有个数据挖掘经常用到的软件的列表。
 
简单学会了Weka的用法。
weka就是一个工具,他根据用户输入的N组数据,通过用户指定的算法,如:回归分析、分类分析、聚类分析、邻近分析等,然后输出一个统计分析的结果。还可以使用测试数据对统计结果进行验证,以确认分析结果的可用性。
weka本身有一个GUI,通过java -jar weka.jar启动。然后点击Explorer启动子程序(我只学了Explorer)。
在weka explorer中点击OpenFile打开*.arff格式的数据。arff其实就是文本文件。
 
weka explorer顶部标签的第二个标签,可以进行分类相关的分析,如回归分析、分类分析、临近分析等。
利用第三个标签为集群相关的分析可以进行聚类分析等。
算法是在标签的Classifier中的Choose按钮中选择的,其中,
以树的形式展示分类分析是用:Classify->Choose->Classifiers->trees->J48; 对应java类(weka.classifiers.trees.J48;)
回归分析是:Classify->Choose->Classifiers->Functions->LinearRegression 刚刚注意到界面上有这个类(weka.classifiers.functions.LinearRegression),也许就是用java编程时要用到的。
聚类分析是:Cluster->Choose->clusterers->SimpleKMeans
邻近分析是:Classify->Choose->Classifiers->lazy->IBK
 
WEKA Explorer的使用,在文档(http://www.doc88.com/p-881687347690.html)中的WEKA Explorer一节中有很详细的介绍。请参考之。
 
通过Java可以很简单的进行API的调用:如下代码从一个arff文件中读取数据,然后进行了回归分析,然后打印了根据回归分析结果预测的某个房屋的价格:
import java.io.FileNotFoundException;
import java.io.IOException;

import weka.classifiers.functions.LinearRegression;
import weka.core.Instances;

public class Weka {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String inputFile = "C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\weka-3-7-5\\weka-3-7-5\\my\\house.arff";
        java.io.Reader r;
        try {
            r = new java.io.BufferedReader(new java.io.FileReader(inputFile));
            Instances instances = new Instances(r);
            instances.setClassIndex(instances.numAttributes() - 1);
            LinearRegression linearRegression = new LinearRegression();
            linearRegression.buildClassifier(instances);
            double[] coef = linearRegression.coefficients();
            double myHouseValue = (coef[0] * 3198) + (coef[1] * 9669)
                    + (coef[2] * 5) + (coef[3] * 3) + (coef[4] * 1) + coef[6];
            System.out.println(myHouseValue);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 

posted on 2012-04-19 18:07  yoyo002  阅读(5452)  评论(0编辑  收藏  举报