注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Weka KDTree 应用  

2014-05-24 20:16:43|  分类: weka |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
0.准备Instance
public Instance createInstance(final Point3d p, final Instances d){
    // Create numeric attributes "x" and "y" and "z"
    Attribute x = d.attribute(0); Attribute y = d.attribute(1); Attribute z = d.attribute(2);
 
    // Create vector of the above attributes
    FastVector a= new FastVector(3);
    a.addElement(x);  a.addElement(y);  a.addElement(z);
 
    // Create empty instance with three attribute values
    Instance inst = new Instance(3);               
     
    // Set instance's values for the attributes "x", "y", and "z"
    inst.setValue(x, p.x);  inst.setValue(y, p.y); inst.setValue(z, p.z);             
 
    // Set instance's dataset to be the dataset "points1"
    inst.setDataset(d);
    return inst;   
}
1.Setting up the KDTree
// Create the weka datastructure for 3D Points
Instances wekaPoints1 = insertIntoWeka(points1, "wekaPoints1");
// Set up the KDTree
KDTree tree = new KDTree();    
try{
    tree.setInstances(wekaPoints1);
    EuclideanDistance df = new EuclideanDistance(wekaPoints1);
    df.setDontNormalize(true);
    tree.setDistanceFunction(df);          
}
catch (Exception e) { e.printStackTrace();}

2. Searching the KDTree
// let's search for the nearest and second nearest neighbor
Instance nn1, nn2;
final Instance p = createInstance(new Point3d(0,0,0), wekaPoints);                    
try{
    Instances neighbors = tree.kNearestNeighbours(p, 2);
    nn1 = neighbors.instance(0);
    nn2 = neighbors.instance(1);                           
}
catch (Exception e) { nn1 = nn2 = null; }
 
System.out.println(nn1 + " is the nearest neigbor for " + p);
System.out.println(nn2 + " is the second nearest neigbor for " + p);
 
// Now we can also easily compute the distances as the KDTree does it
 
DistanceFunction df = tree.getDistanceFunction();
System.out.println("The distance between" + nn1 + " and " + p + " is " + df.distance(nn1, p));
System.out.println("The distance between" + nn2 + " and " + p + " is " + df.distance(nn2, p));

参见:Use Weka in your Java code
  评论这张
 
阅读(359)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017