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

Implement equivalent to numpy.nan_to_num #479

Closed
ricardoV94 opened this issue Oct 17, 2023 · 2 comments · Fixed by #796
Closed

Implement equivalent to numpy.nan_to_num #479

ricardoV94 opened this issue Oct 17, 2023 · 2 comments · Fixed by #796

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Oct 17, 2023

Description

A couple of switches should do the job, no need to implement a new Op

https://numpy.org/doc/stable/reference/generated/numpy.nan_to_num.html

@ricardoV94 ricardoV94 changed the title Implement equivalent to nump.nan_to_num Implement equivalent to numpy.nan_to_num Oct 17, 2023
@aspeers
Copy link

aspeers commented Dec 7, 2023

I'm interested in working on this.

@priyankavgalagali
Copy link

priyankavgalagali commented Dec 8, 2023

import numpy as np
example_array = np.array([np.nan, 1, np.inf, -np.inf, 3], dtype=float)

nan_replace = -1
posinf_replace = 1000
neginf_replace = -1000
import numpy as np

def custom_nan_to_num(arr, nan_val=0.0, posinf_val=None, neginf_val=None):
if posinf_val is None:
posinf_val = np.finfo(arr.dtype).max
if neginf_val is None:
neginf_val = np.finfo(arr.dtype).min

# Using np.where to replace values based on condition
arr = np.where(np.isnan(arr), nan_val, arr)
arr = np.where(np.isposinf(arr), posinf_val, arr)
arr = np.where(np.isneginf(arr), neginf_val, arr)

return arr

Apply the custom function

result = custom_nan_to_num(example_array, nan_val=nan_replace,
posinf_val=posinf_replace,
neginf_val=neginf_replace)

print(result)

Im new to this.....Can you let me know if its something like this or the function of numpy.nan_to_num is to be replaced?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants