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

Sort features when receiving input as hash #9

Merged
merged 1 commit into from
Nov 12, 2024

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
@@ -241,6 +245,11 @@ def read_int64(ptr)
ptr.read_array_of_int64(1).first
end

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
3 checks passed
@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