python-weka Wrapper中的运行时错误

时间:2015-03-25 07:01:28

标签: python django weka

我编写了以下代码来进行python和weka之间的连接然后尝试对加载的数据进行聚类。它在第一次执行时工作正常但是当我尝试在同一会话中第二次运行此代码时它给出了错误。

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

以下是错误。它无法第二次启动jvm。请帮我解决这个问题。如何纠正这一点。

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']

1 个答案:

答案 0 :(得分:1)

jvm.stop()方法只包含对javabridge.kill_vm()方法的调用。根据{{​​3}},一旦被杀死,你就无法再次启动JVM。通常,在调用main方法之前和之后启动和停止JVM,类似于:

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()
相关问题