2015-03-25 92 views
1

我寫了下面的代碼來做python和weka之間的連接,然後試圖對加載的數據進行聚類。它在第一次執行時工作正常,但是當我嘗試運行此代碼時在同一個會話中發生錯誤。python-weka Wrapper中的運行時錯誤

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

jvm.stop()方法只是繞回到javabridge.kill_vm()方法的調用。根據javabridge documentation,一旦它被殺死,你不能再啓動JVM。通常,在調用主要方法之前和之後,您將啓動和停止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()