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

[BUG] Force softmax returning Series #1139

Merged
merged 2 commits into from
Aug 7, 2022

Conversation

Zeroto521
Copy link
Member

@Zeroto521 Zeroto521 commented Aug 7, 2022

PR Description

Please describe the changes proposed in the pull request:

115         >>> import pandas as pd
116         >>> import janitor
117         >>> s = pd.Series([0, 1, 3], name="numbers")
118         >>> s.softmax()
Expected:
    0    0.042010
    1    0.114195
    2    0.843795
    Name: numbers, dtype: float64
Got:
    array([0.04201007, 0.1141952 , 0.84379473])

This PR resolves https://github.com/pyjanitor-devs/pyjanitor/runs/7703616556?check_suite_focus=true .

PR Checklist

Please ensure that you have done the following:

  1. PR in from a fork off your branch. Do not PR from <your_username>:dev, but rather from <your_username>:<feature-branch_name>.
  1. If you're not on the contributors list, add yourself to AUTHORS.md.
  1. Add a line to CHANGELOG.md under the latest version header (i.e. the one that is "on deck") describing the contribution.
    • Do use some discretion here; if there are multiple PRs that are related, keep them in a single line.

Automatic checks

There will be automatic checks run on the PR. These include:

  • Building a preview of the docs on Netlify
  • Automatically linting the code
  • Making sure the code is documented
  • Making sure that all tests are passed
  • Making sure that code coverage doesn't go down.

Relevant Reviewers

Please tag maintainers to review.

@@ -124,7 +124,8 @@ def softmax(s: pd.Series) -> pd.Series:
:param s: Input Series.
:return: Transformed Series.
"""
return scipy_softmax(s)

return pd.Series(scipy_softmax(s), index=s.index, name=s.name)
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I'd like use np.exp(s) / sum(np.exp(s)) instead of using scipy

Copy link
Member

Choose a reason for hiding this comment

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

I think the SciPy implementation comes with numerical stability checks for free. Is there a reason why you would prefer to use a NumPy implementation?

Copy link
Member Author

Choose a reason for hiding this comment

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

To totally replace scipy. Since we only use it in math.py and functions/impute.py

  • mode function in functions/impute.py could replace by Series.value_counts
  • math.py
    • sigmoid (expit) could use np.exp
    • softmax (scipy.special.softmax) could use np.exp
    • logit (scipy.special.softmax) could use np.log
    • normal_cdf (scipy.stats.norm.cdf) and probit (scipy.stats.norm.ppf) can't replace easily.

@codecov
Copy link

codecov bot commented Aug 7, 2022

Codecov Report

Merging #1139 (9fcbc4e) into dev (f748406) will decrease coverage by 0.01%.
The diff coverage is 97.14%.

@@            Coverage Diff             @@
##              dev    #1139      +/-   ##
==========================================
- Coverage   97.36%   97.34%   -0.02%     
==========================================
  Files          77       77              
  Lines        3186     3240      +54     
==========================================
+ Hits         3102     3154      +52     
- Misses         84       86       +2     

Copy link
Member

@ericmjl ericmjl left a comment

Choose a reason for hiding this comment

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

I am good to go. I think we should keep the SciPy soft Mac implementation rather than switch to numpy, for numerical stability reasons (IIRC).

@ericmjl ericmjl merged commit cadd12f into pyjanitor-devs:dev Aug 7, 2022
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.

3 participants