Nan to Num Python

2024/7/7 19:40:12

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a 'NaN'. I know that to convert an array where there are numbers that are NaN you can convert them using numpy.nan_to_num. But what if I want to convert a single value that's not in a array, but a result of a linear regression calculation?

EDIT: It's not a duplicate question (convert nan value to zero ) since I'm referring to a item/result that is not in a array

Answer

numpy.nan_to_num works fine on scalars.

>>> import numpy as np
>>> np.nan_to_num(float('inf'))
1.7976931348623157e+308
>>> np.nan_to_num(float('nan'))
0.0
>>> np.nan_to_num(float('-inf'))
-1.7976931348623157e+308
https://en.xdnf.cn/q/120320.html

Related Q&A

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput

Python to create a find-replace dictionary from a word vba macro

I have a very big macro Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFin…

Converting many .txt files into csv and combining them

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv. ### Folder Name: text_files python_gramm.py aadd01.txt aaxx02.txt aaff03.txt hhd…

Calculation between two columns in Python?

When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be integers, not str. C…

Why does input() always return a string?

Here is my code:age = input("How old are you?: ") print (age) print (type(age))Result:How old are you?: 3535class str <<--- This is problem!But, If I use.. age = int(input("How …

Windowed mode cannot run

Why does pyinstaller exe not run in windowed mode but fine without it? I have changed over to a windows OS from Linux. Never had any issue before hand, how do I correct this.

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = Dave message = f<name> is a really great guy! message = message.replace(<name>, {name}) print(message)the variables are a little more compl…

How to allow caps in this input box program for pygame?

I found this input box module on the internet but it only allows lower case no upper. So could someone tell me what to change in the module to allow caps as im creating a small multiplayer game and i n…