Issues accessing class attributes of external package distance function
Martin Trat <[email protected]> Thu, 14 Jul 2022 12:47:01 +0000
| Newsgroups | gmane.comp.ai.weka |
|---|---|
| Message-ID | <[email protected]> |
Dear Weka developers,
I am trying to fetch the result of a distance metric, implemented as Weka
external package, and use it in python via python-weka-wrapper3
<https://github.com/fracpete/python-weka-wrapper3> .
For this, I augment DilcaDistance.java by the method getMatricesDilca (and
build a jar myself), as shown below (most parts of the file are omitted):
<https://svn.cms.waikato.ac.nz/svn/weka/trunk/packages/external/DilcaDistanc
e/src/main/java/weka/core/DilcaDistance.java>
https://svn.cms.waikato.ac.nz/svn/weka/trunk/packages/external/DilcaDistance
/src/main/java/weka/core/DilcaDistance.java
public class DilcaDistance implements DistanceFunction, Serializable{
// ...
protected Vector<double[][]> matricesDilca;
//...
public Object[] getMatricesDilca() {
return matricesDilca.toArray();
}
}
matricesDilca is computed for a given data set as core functionality of
DilcaDistance. I need to work with this set of matrices that represent
distances between categorical feature values, for all features in the data.
In the python context, I proceed as follows:
Employed versions:
* python = "3.9.*"
* python-weka-wrapper3 = "0.2.10"
* python-javabridge = "4.0.3"
Find an excerpt of my Python code, containing a small example, below:
import numpy as np
import javabridge
import weka.core.jvm as jvm
from weka.filters import Filter
from weka.core.distances import DistanceFunction
from weka.core.typeconv import jdouble_matrix_to_ndarray
from weka.core.dataset import create_instances_from_matrices
class DilcaDistance(DistanceFunction):
def __init__(self, supervised):
_jobject = DistanceFunction.new_instance('weka.core.DilcaDistance')
self.enforce_type(_jobject, 'weka.core.DistanceFunction')
super().__init__(jobject=_jobject, options=None)
self.is_optionhandler = True
javabridge.call(self.jobject, 'setSupervisedDiscretization', '(Z)V',
supervised)
def get_matrices_dilca(self):
"""Fetches dilca matrices from Java and loads those as numpy arrays.
Matrix computation is done on a batch of data instances set via
self.instances.
Returns:
list: list of dilca matrices (numpy.ndarray each)
"""
if not self.instances:
raise ValueError('No instances provided.')
jobj_arr = javabridge.call(self.jobject, 'getMatricesDilca',
'()[Ljava/lang/Object;') # calling the new java method
jmatrices = javabridge.get_env().get_object_array_elements(jobj_arr)
matrices_dilca = []
for jmat in jmatrices:
matrices_dilca.append(jdouble_matrix_to_ndarray(jmat))
return matrices_dilca
if not jvm.started:
jvm.start(packages=True)
data = np.random.randint(1, 10, size=(10,5))
# print(data)
num2nom = Filter('weka.filters.unsupervised.attribute.NumericToNominal',
options=['-R', '1-5'])
ds_weka = create_instances_from_matrices(data, name='data')
num2nom.inputformat(ds_weka)
ds_weka = num2nom.filter(ds_weka) # interpreting generated data as nominal
# print(ds_weka)
DD = DilcaDistance(supervised=False)
DD.instances = ds_weka
dm = DD.get_matrices_dilca()
print(dm)
jvm.stop()
I feel, there is a need to access the set of matricesDilca somehow. In my
case, this needs to occur from Python in order to integrate with other
elements. Is there a more elegant solution (maybe even without having to
augment the java code please forgive my limited experience with Java)?
If not, is there any way to include the above augmentation in java (method
getMatricesDilca) in the official/public code? The reason for this is that,
alongside a publication of mine, I would like to reference the Weka
implementation for the sake of reproducibility of my findings.
Best
Martin
Martin Trat, M.Sc.
Wissenschaftlicher Mitarbeiter | Research Associate
Intelligent Systems and Production Engineering (ISPE)
FZI Forschungszentrum Informatik
Haid-und-Neu-Str. 1014
76131 Karlsruhe, Germany
Tel.: +49 721 9654-509
[email protected]
<https://www.fzi.de/team/martin-trat> www.fzi.de/team/martin-trat
<https://www.fzi.de/> www.fzi.de | <http://www.twitter.com/FZI_official>
www.twitter.com/FZI_official |
<http://www.linkedin.com/company/fzi-official>
www.linkedin.com/company/fzi-official |
<https://www.youtube.com/FZIchannel> www.youtube.com/FZIchannel
FZI Forschungszentrum Informatik
Stiftung des bürgerlichen Rechts
Stiftung Az: 14-0563.1 Regierungspräsidium Karlsruhe
Vorstand: Prof. Dr. Andreas Oberweis, Jan Wiesenberger, Prof. Dr.-Ing. J.
Marius Zöllner
Vorsitzender des Kuratoriums: Ministerialdirigent Günther Leßnerkraus
_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
smime.p7s
(application/pkcs7-signature, 6.2 KB) - not displayed