swati sinha
swati sinha

Reputation: 53

Runtime error in python-weka Wrapper

I have written the following code to do connection between python and weka then tried to do clustering on the loaded data.It works fine when it first time execute but when I tried to run this code second time in the same session it gives an error.

jvm.start()
loader = Loader(classname="weka.core.converters.CSVLoader")
data = loader.load_file("/C:/Users/swati/Desktop/MTP_FINAL/Music_recommendation/recommendation/matrix2.csv")

convert = Filter(classname="weka.filters.unsupervised.attribute.NumericToNominal")
convert.inputformat(data)
data = convert.filter(data)
#print data

clusterer = Clusterer(classname="weka.clusterers.SimpleKMeans", options=["-N", "6"])
clusterer.build_clusterer(data)
#print(clusterer)
ans1=[]
for i in range(0,data.num_instances):
    ans1.append(clusterer.cluster_instance(data.get_instance(i)))

n = data.num_instances
jvm.stop()

Following is the error. It unable to start the jvm second time. Please help me regarding this. How this can be corrected.

RuntimeError at /

Failed to start Java VM

Request Method:     GET
Request URL:    http://127.0.0.1:8000/
Django Version:     1.7.4
Exception Type:     RuntimeError
Exception Value:    

Failed to start Java VM

Exception Location:     C:\Python27\lib\site-packages\javabridge\jutil.py in start_vm, line 259
Python Executable:  C:\Python27\python.exe
Python Version:     2.7.9
Python Path:    

['C:\\Users\\swati\\Desktop\\MTP_FINAL\\Music_recommendation',
 'C:\\Python27\\lib\\site-packages\\setuptools-12.0.5-py2.7.egg',
 'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']

Upvotes: 1

Views: 913

Answers (1)

fracpete
fracpete

Reputation: 2608

The jvm.stop() method just wraps around a call to the javabridge.kill_vm() method. According to the javabridge documentation, you cannot start the JVM again once it has been killed. Usually, you'd start and stop the JVM before and after calling your main method, similar to this:

import traceback
import weka.core.jvm as jvm

def main():
    # cool stuff happening here
    pass

if __name__ == "__main__":
    try:
        jvm.start()
        main()
    except Exception, e:
        print(traceback.format_exc())
    finally:
        jvm.stop()

Upvotes: 1

Related Questions