(作者:瑪瑙河,轉載請註明作者或出處,)
以RBFNetwork為例,簡要說明Jython + Weka 協同工作的方法。
1. install weka (into /opt/weka/ or elsewhere) & Jython
2. export CLASSPATH="$CLASSPATH:/opt/weka/weka.jar"
3. jython rbfnetwork.py traindata.arff testdata1.arff testdata2.arff ...
下面是rbfnetwork.py的代碼
rbfnetwork.py
1 #!/usr/bin/env jython
2 import sys,os
3
4 import java.io.FileReader as FileReader
5 import java.lang.StringBuffer as StringBuffer
6 import java.lang.Boolean as Boolean
7 import java.io.ObjectOutputStream as ObjectOutputStream
8 import java.io.ObjectInputStream as ObjectInputStream
9 import java.io.FileOutputStream as FileOutputStream
10 import java.io.FileInputStream as FileInputStream
11 import weka.core.Instances as Instances
12 import weka.classifiers.functions.RBFNetwork as RBFNetwork
13 import weka.classifiers.Evaluation as Evaluation
14 import weka.core.Range as Range
15 import weka.core.Utils.splitOptions as splitOptions
16 import weka.classifiers.evaluation.output.prediction.PlainText as PlainText
17
18
19 # check commandline parameters
20 if ( (len(sys.argv) < 3)):
21 print "Usage: rbfnetwork.py <TRAIN-ARFF-file> <TEST-ARFF-file> ..."
22 sys.exit()
23
24 # load data file
25 print >>sys.stdout,"Loading data..."
26 train_file = FileReader(sys.argv[1])
27
28 train_data = Instances(train_file)
29
30 # set the class Index - the index of the dependent variable
31 train_data.setClassIndex(train_data.numAttributes() - 1)
32
33 model=sys.argv[1]+".model"
34 if os.path.exists(model):
35 #load existed model
36 f_in = FileInputStream (model);
37 obj_in = ObjectInputStream (f_in);
38 rbfnetwork = obj_in.readObject ();
39
40 print "--> Use exsisted model: %s" % model
41 else:
42 # create the model
43 options=splitOptions("-B 2 -S 1 -R 1.0E-8 -M -1 -W 0.1")
44 rbfnetwork = RBFNetwork()
45 rbfnetwork.setOptions(options)
46 rbfnetwork.buildClassifier(train_data) # only a trained classifier can be evaluated
47
48 #save model
49 f_out = FileOutputStream (model);
50 obj_out = ObjectOutputStream (f_out);
51 obj_out.writeObject (rbfnetwork);
52
53 # print out the built model
54 print "--> Generated model:\n"
55
56 print rbfnetwork
57 print "="*80
58
59 for i in range(len(sys.argv)-2):
60 test_file = FileReader(sys.argv[i+2])
61 test_data = Instances(test_file)
62 test_data.setClassIndex(train_data.numAttributes() - 1)
63 buffer = StringBuffer() # buffer for the predictions
64 output=PlainText()
65 output.setHeader(test_data)
66 output.setBuffer(buffer)
67
68 attRange = Range() # attributes to output
69 outputDistribution = Boolean(True) # we want distribution
70 evaluation = Evaluation(train_data)
71 evaluation.evaluateModel(rbfnetwork, test_data, [output, attRange, outputDistribution])
72
73 print "--> Evaluation for %s:\n" % sys.argv[i+2]
74 print evaluation.toSummaryString()
75 print evaluation.toMatrixString()
76 print "-"*80
77 print "--> Predictions for %s:\n" % sys.argv[i+2]
78 print buffer
79 print "="*80
80
81
如果你有大量的類似任務需要運行,你還可以將這些任務提交到任務管理系統如Torque等,然後你就不用管了,等著任務運行完畢後系統給你發送通知訊息吧。