array must not contain infs or NaNs
时间: 2024-05-12 13:20:46 浏览: 21
As an AI language model, I'm not sure what context you are referring to, but in general, when working with arrays, it is important to ensure that they do not contain any infinite (inf) or Not-a-Number (NaN) values. This is because inf and NaN values can cause numerical calculations to fail or produce unexpected results.
To check if an array contains inf or NaN values, you can use the numpy functions `numpy.isinf()` and `numpy.isnan()`. If either function returns True for any element of the array, it means that the array contains inf or NaN values.
Here's an example:
```python
import numpy as np
a = np.array([1, 2, np.inf, 4])
b = np.array([1, 2, np.nan, 4])
print(np.isinf(a)) # [False False True False]
print(np.isnan(b)) # [False False True False]
```
To remove inf or NaN values from an array, you can use the `numpy.isfinite()` function, which returns a boolean mask indicating which elements of an array are finite:
```python
a = np.array([1, 2, np.inf, 4])
a = a[np.isfinite(a)]
print(a) # [1 2 4]
```
This will remove all inf values from the array and leave only the finite values.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)