今天滴滴逮得凶哟,为什么有黑车去西翥田庄吗不去抓,搞笑

3742人阅读
Felven在职场(80)
学过数据挖掘和机器学习课的人估计都知道有weka这么一个开源软件,这个软件实现了很多聚类,分类算法。如果没有接触过这个软件,可以参看这篇文章,看完后基本就了解的差不多了:
当然,很多情况下我们不是直接使用这个软件,而是希望把软件中的算法拿过来集成到我们自己的系统中,也就是通常意义上的二次开发。由于weka是开源的,二次开发自然很方面。下面来简单说说如何进行二次开发。
首先在本地安装weka,在安装目录下找到jar包
在eclipse下新建一个工程,导入weka的jar包
接下来就可以尽情使用weka里面提供的算法了,这里我使用的EM算法对用户进行聚类
import java.io.*;
import weka.core.*;
import weka.clusterers.*;
import weka.filters.*;
import weka.filters.unsupervised.attribute.R
* This class shows how to perform a &classes-to-clusters&
* evaluation like in the Explorer using EM. The class needs as
* first parameter an ARFF file to work on. The last attribute is
* interpreted as the class attribute.
* This code is based on the method &startClusterer& of the
* &weka.gui.explorer.ClustererPanel& class and the
* &evaluateClusterer& method of the &weka.clusterers.ClusterEvaluation&
FracPete (fracpete at waikato dot ac dot nz)
public class ClassesToClusters {
public static void main(String[] args) throws Exception {
// load data
BufferedReader reader = new BufferedReader(new FileReader(&C:/Users/felven/Desktop/1.arff&));
Instances data = new Instances(reader);
data.setClassIndex(data.numAttributes() - 1);
// generate data for clusterer (w/o class)
Remove filter = new Remove();
filter.setAttributeIndices(&& + (data.classIndex() + 1));
filter.setInputFormat(data);
Instances dataClusterer = Filter.useFilter(data, filter);
// train clusterer
EM clusterer = new EM();
// set further options for EM, if necessary...
String[] options = new String[4];
// max. iterations
options[0] = &-I&;
options[1] = &100&;
//set cluseter numbers
options[2]=&-N&;
options[3]=&3&;
clusterer.setOptions(options);
clusterer.buildClusterer(dataClusterer);
// evaluate clusterer
ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(clusterer);
eval.evaluateClusterer(data);
// print results
System.out.println(eval.clusterResultsToString());
由于weka使用的文件大多是arff格式的,这里我的程序从桌面上读入一个arff格式的文件,对其中数据进行聚类,在控制台下输出聚类的情况分析。
当然,我们也可以实现稍复杂的功能,例如把结果输出到文件,同时为了美观,把程序按模块进行分解。
import java.io.BufferedR
import java.io.BufferedW
import java.io.FileNotFoundE
import java.io.FileR
import java.io.FileW
import java.io.IOE
import weka.clusterers.ClusterE
import weka.clusterers.EM;
import weka.core.I
import weka.core.converters.ConverterUtils.DataS
import weka.filters.F
import weka.filters.unsupervised.attribute.R
public class UserBehaviourCluster {
private String sourceF
private String targetF
private int maxC
private int[]
public UserBehaviourCluster(String sourceFile, String targetFile, int maxCluster) {
this.sourceFile = sourceF
this.targetFile = targetF
this.maxCluster = maxC
public void loadData() throws Exception {
DataSource dataSource = new DataSource(sourceFile);
instances = dataSource.getDataSet();
public void cluster() throws Exception {
//set attribute 1 weight to 0
instances.attribute(0).setWeight(0);
EM clusterer = new EM();
// set further options for EM, if necessary...
String[] options = new String[4];
// max. iterations
options[0] = &-I&;
options[1] = &100&;
//set cluseter numbers
options[2]=&-N&;
options[3]= Integer.toString(maxCluster);
clusterer.setOptions(options);
clusterer.buildClusterer(instances);
// evaluate clusterer
ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(clusterer);
eval.evaluateClusterer(instances);
// print results
//System.out.println(eval.clusterResultsToString());
assignment
= new int[instances.numInstances()];
for (int j = 0; j&instances.numInstances() ; j++){
assignment[j]=clusterer.clusterInstance(instances.get(j) );
System.out.println(assignment[j]);
public void writeResult() throws Exception {
FileReader fr =
BufferedReader br =
FileWriter fw =
BufferedWriter bw =
String line=
fr = new FileReader(sourceFile);
br = new BufferedReader(fr);
fw = new FileWriter(targetFile);
bw = new BufferedWriter(fw);
line=br.readLine();
bw.write(line+&,cluster\n&);
while((line=br.readLine())!=null){
bw.write(line+&,&+assignment[j++]+&\n&);
} finally {
if (br != null) {
br.close();
if (bw != null) {
bw.close();
public void process() throws Exception{
loadData();
cluster();
writeResult();
public static void main(String[] args) throws Exception{
UserBehaviourCluster cluster=new UserBehaviourCluster(&C:/Users/felven/Desktop/test3.csv&, &C:/Users/felven/Desktop/test4.csv&, 2);
cluster.process();
上面userBehaviourCluster中第三个参数指定的是最终聚类的个数,这里要求数据聚为3类。
其中读入的文件格式如下:
输出文件格式如下:
感觉还不错吧,更多的内容可以看这里:&
最后要感谢老大在java方面提供帮助,我java实在太菜了。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1741268次
积分:31330
积分:31330
排名:第139名
原创:1401篇
转载:121篇
译文:20篇
评论:889条
(10)(9)(18)(13)(13)(10)(13)(9)(12)(2)(12)(4)(10)(10)(9)(21)(10)(8)(8)(10)(12)(13)(15)(1)(1)(9)(10)(6)(19)(19)(16)(11)(17)(22)(18)(20)(10)(13)(15)(10)(22)(29)(38)(43)(34)(29)(38)(47)(47)(72)(18)(11)(9)(9)(7)(3)(3)(17)(3)(4)(4)(28)(31)(18)(14)(10)(3)(2)(113)(270)(26)(10)(11)(6)(11)(10)(7)(1)(3)(5)(2)(2)(8)MEKA: A Multi-label Extension to WEKA
The MEKA project provides an open source implementation of methods for multi-label learning and evaluation.
In , we want to predict multiple output variables for each input instance. This different from the 'standard' case (binary, or multi-class classification) which involves only a single target variable.
MEKA is based on the
Machine Learning T it includes dozens of
from the scientific literature, as well as a wrapper to the related
framework.
Main developers:
(Ecole Polytechnique, France)
(University of Waikato, New Zealand)
(Johannes Gutenberg University Mainz)
NEW RELEASE April 12, 2017:
is released. This release gives several minor improvements and fixes over the previous version, but it also includes new features and new classifiers. See the
regarding changes.
@article{MEKA,
&&&&author = {Read, Jesse and Reutemann, Peter and Pfahringer, Bernhard and Holmes, Geoff},
&&&&title = {{MEKA}: A Multi-label/Multi-target Extension to {Weka}},
&&&&journal = {Journal of Machine Learning Research},
&&&&year = {2016},
&&&&volume = {17},
&&&&number = {21},
&&&&pages = {1--5},
= {http://jmlr.org/papers/v17/12-164.html},
MEKA in your Java code Dec 08, 2016: Added some examples of how to .
Meka in JMLR-mloss April 29, 2016: Meka published in Journal of Machine Learning Research MLOSS track. You can now .
Move to Github Nov 04, 2015: Meka is moving to . The
will still be updated at SourceForge.net, but the code repository is now: .
To include it in your projects,
&dependency&
&groupId&net.sf.meka&/groupId&
&artifactId&meka&/artifactId&
&version&1.9.1&/version&
&/dependency&
Download MEKA .
Checkout the :
/Waikato/meka.git
To get started,
MEKA and run ./run.sh (run.bat on Windows) to launch the GUI.
has numerous examples on how to run and extend MEKA.
available in MEKA, and (command-line) examples on how to use them.
There are some .
is available.
MEKA originated from implementations of work from various
(note also the references therein). There are some
about multi-label and multi-target classification in general.
Have a specific problem or query? Post to MEKA's
(please avoid contacting developers directly for MEKA-related help).
A collection of multi-label and multi-target datasets is . Even more datasets are available at the
(note that MULAN indexes labels as the final attributes, whereas MEKA indexs as the beginning). See the
for more information.
The following text datasets have been created / compiled into WEKA's ARFF format using the
filter. Also available are
Description and Original Source(s)
A subset of the , as labelled by the
Article titles and partial blurbs mined from
Articles posted on the
Movie plot text summaries labelled with genres sourced from the
interface, labeled with genres.
N = The number of examples (training+testing) in the datasets
L = The number of predefined labels relevant to this dataset
LC = Label Cardinality. Average number of labels assigned per document
PU = Percentage of documents with Unique label combinations
Other software that uses MEKA
framework - integrates MEKA into workflows
can use Updateable MEKA classifiers
can interface to MEKA in Python
framework, includes a plugin to integrate Meka Classifiers into workflows
Other multi-label links
from the Machine Learning and Knowledge Discovery Group in the Aristotle University of Thessaloniki.27335人阅读
语言学研究(10)
weka是很好用的机器学习库,这里就不了。
言归正传,要使用程序方式使用weka,步骤如下:
一、在eclipse里新建一个java project:
1.建立工程:单击菜单中file-&new-&java project,在弹出对话框的project name中起任意一个名字,此处假设是wekaTest。单击Finish按钮(在对话框底部)。
2.建立package:在package Explorer中找到刚才新建的工程,在其上右键-&New-&package。在Name文本框里面输入名称,此处假设为Test。单击Finish按钮。
3.建立程序文件:在刚才新建的package上面右键-&New-&class,选中public static void main(String[] args)多选框,单击Finish。
二、在该工程中添加weka的引用:
1.package Explorer中工程名上右键,选择弹出菜单最后一项properties-&在左面选中java Build Path-&在右面的Library页面-&单击Add External JARs&-&浏览weka所在目录,将weka.jar添加进来,然后单击ok。
2.在package Explorer中在双击Test文件,然后在package wekaT一句下面添加四句代码:
import java.io.F
import weka.classifiers.C
import weka.classifiers.trees.J48;
import weka.core.I
import weka.core.converters.ArffL
三、在程序中添加weka调用代码:
将以下代码添加到Main函数中(在// TODO Auto-generated method stub下面):
&&& Classifier m_classifier = new J48();
&&&&&&& File inputFile = new File("D://Program Files//Weka-3-6//data//cpu.with.vendor.arff");//训练语料文件
&&&&&&& ArffLoader atf = new ArffLoader();&
&&&&&&& atf.setFile(inputFile);
& & &&& Instances instancesTrain = atf.getDataSet(); // 读入训练文件&&&&
& & &&& inputFile = new File("D://Program Files//Weka-3-6//data//cpu.with.vendor.arff");//测试语料文件
& & &&& atf.setFile(inputFile);&&&&&&&&&&
&&&&&&& Instances instancesTest = atf.getDataSet(); // 读入测试文件
&&&&&&& instancesTest.setClassIndex(0); //设置分类属性所在行号(第一行为0号),instancesTest.numAttributes()可以取得属性总数
&&&&&&& double sum = instancesTest.numInstances(),//测试语料实例数
&&&&&&& right = 0.0f;
& & &&& instancesTrain.setClassIndex(0);
& & &&& m_classifier.buildClassifier(instancesTrain); //训练&&&&&&& & &&
&&&&&&& for(int& i = 0;i&i++)//测试分类结果
&&&&&&&&&&& if(m_classifier.classifyInstance(instancesTest.instance(i))==instancesTest.instance(i).classValue())//如果预测值和答案值相等(测试语料中的分类列提供的须为正确答案,结果才有意义)
&&&&&&&&&&& {
&&&&&&&&&&& & right++;//正确值加1
&&&&&&&&&&& }
&&&&&&& System.out.println("J48 classification precision:"+(right/sum));
四、运行一下试试。   
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:905736次
积分:10928
积分:10928
排名:第1245名
原创:136篇
转载:50篇
译文:38篇
评论:622条
阅读:38175
(3)(1)(1)(2)(10)(1)(1)(1)(1)(2)(1)(1)(1)(1)(2)(1)(1)(1)(1)(3)(7)(2)(7)(16)(4)(2)(1)(4)(12)(2)(2)(2)(3)(1)(4)(8)(1)(7)(47)(8)(3)(1)(2)(6)(4)(1)(1)(1)(1)(2)(2)(18)(8)Weka 3 - Data Mining with Open Source Machine Learning Software in Java
Machine Learning Group at the University of Waikato
Downloading and installing Weka
There are two versions of Weka: Weka 3.8 is the latest
stable version, and Weka 3.9 is the development version. For
the bleeding edge, it is also possible to download nightly
snapshots.
Stable versions receive only bug fixes, while the
development version receives new features. Weka 3.8 and 3.9
feature a package management system that makes it easy for
the Weka community to add new functionality to Weka. The
package management system requires an internet connection in
order to download and install packages.
Note (1) for users upgrading from Weka 3.7 to Weka 3.8 or later: if the Weka 3.8 package manager does not start up, please delete the file installedPackageCache.ser in the packages folder that resides in the wekafiles folder in your user home.
Note (2) for users upgrading from Weka 3.7 to Weka 3.8 or later: serialized models created in 3.7 are not compatible with 3.8. We have a
tool that can migrate some models to be compatible with 3.8.0. One exception is RandomForest, which can be migrated up to 3.7.13 but no further. Usage is as follows:
java -cp &path to modelMigrator.jar&:&path to weka.jar& weka.core.ModelMigrator -i &path to old serialized weka model& -o &upgraded model file name&
Every night a snapshot of the Subversion repository is taken, compiled and
put together in ZIP files. For those who want to have the
latest bugfixes, they can download these snapshots
Stable version
Weka 3.8 is the latest stable version of Weka. This branch of Weka receives bug
fixes only, although new features may become available in packages.
There are different options for downloading and installing it on your system:
Click here to download a self-extracting
executable for 64-bit Windows that includes Oracle's 64-bit Java VM 1.8 (weka-3-8-1jre-x64.
Click here to download a self-extracting
executable for 64-bit Windows without a Java VM (weka-3-8-1-x64.
Click here to download a self-extracting
executable for 32-bit Windows that includes Oracle's 32-bit Java VM 1.8 (weka-3-8-1jre.
Click here to download a self-extracting
executable for 32-bit Windows without a Java VM (weka-3-8-1.
These executables will install Weka in your Program Menu.
Download the version without the Java VM if you already have Java 1.7 (or
later) on your system.
Click here to download a disk image for OS X that contains
a Mac application including Oracle's Java 1.8 JVM
(weka-3-8-1-oracle-jvm.
Other platforms (Linux, etc.)
Click here to download a zip archive containing
Weka (weka-3-8-1. 50.9 MB)
First unzip the zip file. This will create a new directory
called weka-3-8-1. To run Weka, change into that directory
java -jar weka.jar
Note that Java needs to be installed on your system for this
to work. Also note, that using -jar will
override your current CLASSPATH variable and only use the
Developer version
This is the trunk of Weka and continues from the stable-3-8 code line. It
receives both bug fixes and new features.
Click here to download a self-extracting
executable that includes 64 bit Java VM 1.8 (weka-3-9-1jre-x64.
Click here to download a self-extracting
executable without the Java VM (weka-3-9-1-x64.
Click here to download a self-extracting
executable that includes Java VM 1.8 (weka-3-9-1jre.
Click here to download a self-extracting
executable without the Java VM (weka-3-9-1.
These executables will install Weka in your Program Menu.
Download the second version if you already have Java 1.7 (or
later) on your system.
Click here to download a disk image for OS X that contains
a Mac application including Oracle's Java 1.8 JVM
(weka-3-9-1-oracle-jvm. 123.6 MB)
Other platforms (Linux, etc.)
Click here to download a zip archive containing
Weka (weka-3-9-1. 50.8 MB)
First unzip the zip file. This will create a new directory
called weka-3-9-1. To run Weka, change into that directory
java -jar weka.jar
Note that Java needs to be installed on your system for this
to work. Also note, that using -jar will
override your current CLASSPATH variable and only use the
Old versions
All old versions of Weka are available from the
Sourceforge website.

我要回帖

更多关于 太原去阳泉的黑车 的文章

 

随机推荐