K表示使用weka python进行聚类

时间:2016-06-23 01:47:22

标签: python weka k-means

from weka.clusterers import Clusterer
import weka.core.converters as converters

data = converters.load_any_file("/home/ubuntu/test.csv")
data.class_is_last()

clusterer = Clusterer(classname="weka.clusterers.SimpleKMeans", options=["-N", "3"])
clusterer.build_clusterer(data)

print(clusterer)

# cluster the data
for inst in data:
    cl = clusterer.cluster_instance(inst)  # 0-based cluster index
    dist = clusterer.distribution_for_instance(inst)   # cluster membership distribution

    print("cluster=" + str(cl) + ", distribution=" + str(dist))

我使用上面的代码做k意味着custering我无法执行程序

以下是我得到的错误

Traceback (most recent call last):
  File "clus.py", line 6, in <module>
    data = converters.load_any_file("/home/ubuntu/hello.csv")
  File "/usr/local/lib/python2.7/dist-packages/weka/core/converters.py", line 255, in load_any_file
    loader = loader_for_file(filename)
  File "/usr/local/lib/python2.7/dist-packages/weka/core/converters.py", line 239, in loader_for_file
    "(Ljava/lang/String;)Lweka/core/converters/AbstractFileLoader;", filename)
  File "/usr/local/lib/python2.7/dist-packages/javabridge/jutil.py", line 932, in static_call
    fn = make_static_call(class_name, method_name, sig)
  File "/usr/local/lib/python2.7/dist-packages/javabridge/jutil.py", line 903, in make_static_call
    klass = env.find_class(class_name)
AttributeError: 'NoneType' object has no attribute 'find_class'

我不知道为什么我会收到这些错误。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

python-weka-wrapper API中所述,您必须导入并启动Java虚拟机:

import weka.core.jvm as jvm
jvm.start()

它应该可以解决你的问题。