Search code examples
pythonpython-3.xtypesweka

Is possible to change the type of an attribute from an arff with pyweka?


I have an arff like that :

@relation 'Base de datos modelos 1 y 2'

@attribute BMI numeric
@attribute ROM-PADF-KE_D numeric
@attribute Asym-ROM-PHIR(≥8)_discr {No_Bilateral_Asymmetry,Bilateral_Asymmetry}
@attribute Asym_SLCMJLanding-pVGRF(10percent)_discr {No_Bilateral_Asymmetry,Bilateral_Asymmetry}
@attribute Asym_TJ_Valgus_FPPA(10percent)_discr {No_Bilateral_Asymmetry,Bilateral_Asymmetry}
@attribute DVJ_Valgus_KneeMedialDisplacement_D_discr numeric
@attribute Soft-Tissue_injury_≥4days {No,Yes}

@data
18.716444,33,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,0,No
22.182267,41,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,2,No
25.352783,35,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,1,No
21.332873,33,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,0,No
23.085619,25,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,3,Yes
21.428649,42,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,No_Bilateral_Asymmetry,1,No

and i want to change the numeric type of "@attribute DVJ_Valgus_KneeMedialDisplacement_D_discr" to {0,1,2,3}

Is it possible?


Solution

  • You can use the NumericToNominal filter for that purpose.

    The attribute that you want to convert is the 6th one, so you need to do something like this:

    import weka.core.jvm as jvm
    from weka.core.converters import load_any_file, save_any_file
    from weka.filters import Filter
    
    jvm.start()
    data = load_any_file("/some/where/data_in.arff", class_index="last")
    
    f = Filter(classname="weka.filters.unsupervised.attribute.NumericToNominal", options=["-R", "6"])
    f.inputformat(data)
    filtered = f.filter(data)
    
    save_any_file(filtered, "/some/where/data_out.arff")
    
    jvm.stop()