K means clustering using weka python

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))

I used the above code for doing k means custering i am not able to execute the program

The following are the errors I get

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'

I don't know why I am getting these errors. Can someone help me with this?

Upvotes: 0

Views: 935

Answers (1)

cstdvd
cstdvd

Reputation: 41

As described in the python-weka-wrapper API you have to import and start the Java Virtual Machine:

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

It should solve your problem.

Upvotes: 1

Related Questions