Skip to content

Conversation

@nunosilva800
Copy link
Contributor

@nunosilva800 nunosilva800 commented Oct 18, 2024

Given the popular usage of hashes in Ruby, I think this would be a nice quality of life addition to the Booster#predict API.

@nunosilva800 nunosilva800 marked this pull request as ready for review October 18, 2024 15:59

def sorted_feature_values(input_hash)
@cached_feature_names ||= feature_name
input_hash.transform_keys(&:to_s).fetch_values(*@cached_feature_names)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering if accepting the input hash with keys as symbol or string is worth it, or if it should expect only strings.

Quick benchmark (10000 iterations):

                user     system      total        real
   keys are strings:  0.031401   0.001063   0.032464 (  0.032646)
with transform_keys:  0.108264   0.005040   0.113304 (  0.113892)
Suggested change
input_hash.transform_keys(&:to_s).fetch_values(*@cached_feature_names)
input_hash.fetch_values(*@cached_feature_names)

@ankane ankane merged commit ea9ae02 into ankane:master Nov 12, 2024
@ankane
Copy link
Owner

ankane commented Nov 12, 2024

Thanks @nunosilva800! I think it should support both symbols and strings (it could check the first row to see which is used and take different paths, but think it's probably not worth the complexity).

@nunosilva800 nunosilva800 deleted the sort-feature-in-hash branch November 13, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants