Use python to call weka-Jython & Weka

Source: Internet
Author: User

(Author: Ma Weihe. Please indicate the author or source for reprinting ,)

The following uses RBFNetwork as an example to describe how Jython and Weka work collaboratively.

1. install weka (into/opt/weka/or elsewhere) & Jython

2. export CLASSPATH = "$ CLASSPATH:/opt/weka. jar"

3. jython rbfnetwork. py traindata. arff testdata1.arff testdata2.arff...

 

The code for rbfnetwork. py is as follows:

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

 

If you have a large number of similar tasks to run, you can also submit these tasks to the task management system, such as Torque, and then you don't need to worry about them, wait until the task is completed and the system will send you a notification email.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.