Last active
September 27, 2018 09:55
-
-
Save xoelop/a3811c229772d24329a8a6a784a95ef2 to your computer and use it in GitHub Desktop.
Code to replace the inf and -inf values in a np.array for the max and min of the non inf values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# To substitute inf value by the max non inf value | |
a = np.array([1, np.inf, 10, -5, -np.inf]) | |
print(a) | |
# [ 1. inf 10. -5. -inf] | |
a[np.isposinf(a)] = a[~np.isposinf(a)].max() | |
a[np.isneginf(a)] = a[~np.isneginf(a)].min() | |
print(a) | |
# [ 1. 10. 10. -5. -5.] | |
def replace_infs(a): | |
"""Replaces the inf and -inf values in a np.array or pd.Series for the max | |
and min of the non inf values""" | |
a[np.isposinf(a)] = a[~np.isposinf(a)].max() | |
a[np.isneginf(a)] = a[~np.isneginf(a)].min() | |
return a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment