train_data = converters.load_any_file(data_dir + "1kStemsClasses.arff")
test_data = converters.load_any_file(data_dir + "1kStemsClassesTest.arff")
train_data.class_is_last()
test_data.class_is_last()
# http://git.mrman.de/dataMining/raw/8b3cae92dc714fdd46ad428a37ca76a0196268ab/assignment-question6.ipynb
#cls = Classifier(classname="weka.classifiers.trees.J48", options=["-C", "0.25", "-M", "2"])
cls = Classifier(
classname="weka.classifiers.bayes.BayesNet",
options=["-D", "-Q", "weka.classifiers.bayes.net.search.local.K2", "--", "-P", "2", "-S", "BAYES"])
print ("Classifier is: " + cls.classname)
pout = PredictionOutput(classname="weka.classifiers.evaluation.output.prediction.PlainText")
print ("Training...")
evl = Evaluation(train_data)
print("Crossvalidating...")
evl.crossvalidate_model(cls, train_data, 10, Random(1), pout)
print("Evaluation")
print(evl.summary())
print("pctCorrect: " + str(evl.percent_correct))
print("incorrect: " + str(evl.incorrect))
cls.build_classifier(train_data)
Failed to instantiate weka.classifiers.bayes.EditableBayesNet: weka.classifiers.bayes.EditableBayesNet
cls = Classifier(
classname="weka.classifiers.bayes.net.EditableBayesNet",
options=["-D", "-Q", "weka.classifiers.bayes.net.search.local.K2", "--", "-P", "3", "-S", "BAYES"])
--
You received this message because you are subscribed to the Google Groups "python-weka-wrapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-weka-wra...@googlegroups.com.
To post to this group, send email to python-we...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-weka-wrapper/993b6440-9e02-45b1-ba50-f710feb32467%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
cls = Classifier(
classname="weka.classifiers.bayes.net.EditableBayesNet",
options=["-D", "-Q", "weka.classifiers.bayes.net.search.local.K2", "--", "-P", "3", "-S", "BAYES"])
print ("Classifier is: " + cls.classname)
cls.build_classifier(train_data)
gd = cls.jwrapper.getDistribution()
print(gd)
To unsubscribe from this group and stop receiving emails from it, send an email to python-weka-wrapper+unsub...@googlegroups.com.
gn = cls.jwrapper.getNode2('ab')
print(gn)
gd0 = cls.jwrapper.getDistribution(0)
print(gd0)
cls = Classifier(
classname="weka.classifiers.bayes.net.EditableBayesNet",
options=["-D", "-Q", "weka.classifiers.bayes.net.search.local.K2", "--", "-P", "2", "-S", "BAYES"])
print ("Classifier is: " + cls.classname)
cls.build_classifier(train_data)
#Get CPC node ID
cpcID = cls.jwrapper.getNode2('CPC')
print('cpcID: ', cpcID) # 324 for CPC
#Print distribution for CPC node
gdCPC = cls.jwrapper.getDistribution(cpcID)
arr = typeconv.double_matrix_to_ndarray(gdCPC.o)
print('matrix length:', len(arr[0])) #len = 294
print('cpc distribution:', arr)
#Get values of CPC node
print('cpc values')
gvCPC = cls.jwrapper.getValues(cpcID)
lstVals = typeconv.string_array_to_list(gvCPC.o)
print('length, list:', len(lstVals), lstVals) # len = 294 also
#Get value name of CPC node
print('get cpc value name')
gvnCPC = cls.jwrapper.getValueName(cpcID, 1)
print('value in 1 position:', gvnCPC)
#Print margin for CPC node
#gmCPC = cls.jwrapper.getMargin(cpcID) #<---fails
#arr = typeconv.double_matrix_to_ndarray(gmCPC.o)
#print('cpc margin:', arr)
#Set evidence to first element
print('get evidence')
print(cls.jwrapper.getEvidence(cpcID)) #<---fails
#Set evidence to first element
print('set evidence')
cls.jwrapper.setEvidence(cpcID, 0) #<---fails
#Print new distribution for CPC node
print('new distribution')
gdCPC = cls.jwrapper.getDistribution(cpcID)
arr = typeconv.double_matrix_to_ndarray(gdCPC.o)
print(arr)
> To unsubscribe from this group and stop receiving emails from it, send an email to python-weka-wrapper+unsub...@googlegroups.com.
marginCalc = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.MarginCalculator"))
marginCalc.jwrapper.calcMargins(editable)
marginCalc = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.MarginCalculator"))
marginCalc.jwrapper.calcMargins(editable.jobject)
so = javabridge.make_instance("weka/core/SerializedObject", "(Ljava/lang/Object;)V", marginCalc.jobject)
m_marginCalculatorWithEvidence = javabridge.call(so, "getObject", "()Ljava/lang/Object;")
print('margincalc: ', marginCalc)
import weka.core.jvm as jvm
from weka.classifiers import Classifier
from weka.core.classes import JavaObject
import numpy as np
import weka.core.typeconv as typeconv
import javabridge
# from: http://pythonhosted.org/python-weka-wrapper/examples.html
# https://github.com/LeeKamentsky/python-javabridge/blob/master/javabridge/noseplugin.py
# https://gist.github.com/carl0967/1138b5a55d31ca1dda5c
# https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/python-weka-wrapper/qF4vw_6sqAA/EmqTph1NAAAJ
jvm.start(max_heap_size="4000M")
data_dir = "D:/weka/"
xmlfile = "D:/weka/iris.xml" # created with BayesNet, MaxNrParents=2
bifreader = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.BIFReader"))
editable = Classifier(jobject=javabridge.make_instance(
"weka/classifiers/bayes/net/EditableBayesNet",
"(Lweka/classifiers/bayes/net/BIFReader;)V",
bifreader.jwrapper.processFile(xmlfile)))
classID = editable.jwrapper.getNode2('class')
print('class id:', classID)
marginCalc = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.MarginCalculator"))
marginCalc.jwrapper.calcMargins(editable.jobject)
print('Pre-evidence:\n', marginCalc)
marginCalc.jwrapper.setEvidence(classID, 1)
print('Post-evidence:\n', marginCalc)
gm4 = marginCalc.jwrapper.getMargin(0) # I believe this could be 0 to 4
#arr = typeconv.double_matrix_to_ndarray(gm4.o) # <--- This line fails... I couldn't find a conversion that works
print('getMargin4: ', gm4)
jvm.stop()
import weka.core.jvm as jvm
from weka.classifiers import Classifier
from weka.core.classes import JavaObject
import numpy as np
import weka.core.typeconv as typeconv
import javabridge
import weka.core.serialization as Serial
# from: http://pythonhosted.org/python-weka-wrapper/examples.html
# https://github.com/LeeKamentsky/python-javabridge/blob/master/javabridge/noseplugin.py
# https://gist.github.com/carl0967/1138b5a55d31ca1dda5c
# https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/python-weka-wrapper/qF4vw_6sqAA/EmqTph1NAAAJ
jvm.start(max_heap_size="4000M")
data_dir = "D:/weka/"
'''
5 Nodes for Iris dataset:
4 attributes:
node id: 0 (sepallength) [-inf-5.55, 5.55-6.15, 6.15-inf]
node id: 1 (sepalwidth) [-inf-2.95, 2.95-3.35, 3.35-inf]
node id: 2 (petallength) [-inf-2.45, 2.45-4.75, 4.75-inf]
node id: 3 (petalwidth) [-inf-0.8, 0.8-1.75, 1.75-inf]
1 class:
node id: 4 (class) [0=iris-setosa, 1=iris-versicolor, 2=iris-virginica]
'''
# Choose trained Weka BIFXML file
xmlfile = "D:/weka/iris.xml" # created with BayesNet, MaxNrParents=2, BIFXML file
bifreader = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.BIFReader"))
editable = Classifier(jobject=javabridge.make_instance(
"weka/classifiers/bayes/net/EditableBayesNet",
"(Lweka/classifiers/bayes/net/BIFReader;)V",
bifreader.jwrapper.processFile(xmlfile)))
# We need to calculate the margins of all the attributes
marginCalc = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.MarginCalculator"))
marginCalc.jwrapper.calcMargins(editable.jobject)
marginCalcNoEvidence = Serial.deepcopy(marginCalc) # could maybe get by without this, just use marginCalc()
# Have a look before we set evidence
print('Pre-evidence:\n', marginCalcNoEvidence)
# determine your class ID by name
classID = editable.jwrapper.getNode2('class')
print('class id:', classID)
for j in range(0,3):
marginCalc = JavaObject(JavaObject.new_instance("weka.classifiers.bayes.net.MarginCalculator"))
marginCalc.jwrapper.calcMargins(editable.jobject)
# Set evidence to an index value of the class node to see which attributes are most influential
marginCalc.jwrapper.setEvidence(classID, j) #0=setosa, 1=versicolor, 2=virginica
print('\n\n', editable.jwrapper.getNodeValue(classID, j))
# View margins post-evidence
print('Post-evidence:\n', marginCalc)
# Get the margin on the attribute nodes(0-4)
for i in range(0,4):
gm = marginCalc.jwrapper.getMargin(i) # I believe this could be 0 to 4
arr = javabridge.get_env().get_double_array_elements(gm.o)
print('Margin node', i, '(' + editable.jwrapper.getNodeName(i) + '):', arr)
jvm.stop()