Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable string annotations and predictions? #3

Open
tischi opened this issue May 8, 2021 · 1 comment
Open

Enable string annotations and predictions? #3

tischi opened this issue May 8, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@tischi
Copy link

tischi commented May 8, 2021

@haesleinhuepf

You currently require the annotations to be Integer (0,1,2,3,...).
Is this an intrinsic limitation of Weka?
If not, I think it would be very good to also support String annotations and predictions.
The reason being that when we store the classifier file it would be great it that could contain and output String based class prediction (otherwise one would have to keep track of the mapping from Integer to String somewhere else, and I am in fact not sure where).

@haesleinhuepf haesleinhuepf added the enhancement New feature or request label May 8, 2021
@tischi
Copy link
Author

tischi commented May 8, 2021

I was looking a bit into this myself and here are code snippets that could be useful for you as well:

Converting String annotations to numbers:

final List< String > annotations = new ArrayList<>( new HashSet<>( tableModel.getColumn( annotationColumn ) ) );
annotations.remove( "None" );
attributes.add( new Attribute( "annotations", annotations ) );

final HashMap< String, Integer > annotationToIndex = new HashMap<>();
for ( int i = 0; i < annotations.size(); i++ )
{
	annotationToIndex.put( annotations.get( i ), i );
}

Above code can be used during the creation of the Instances to populate the class column, e.g. like this:

// add class index as last "feature"
doubles[ doubles.length - 1 ] = annotationToIndex.get( tableRow.getCell( annotationColumn ) );
new DenseInstance( 1.0, doubles );

Converting a numeric prediction to a String:

randomForest.classifyInstance( predictionInstance );
final String predictedAnnotation = predictionInstance.classAttribute().value( predictionInstance.classIndex() );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants