Removing NaN Values from a NumPy Array
NumPy arrays often contain missing or invalid data represented as NaN (Not-a-Number). Removing these values is essential for data manipulation or analysis. Here's how to accomplish this using NumPy:
Using Numpy.isnan and Array Indexing
To remove NaN values from an array x:
<code class="python">x = x[~numpy.isnan(x)]</code>
Explanation:
Example:
<code class="python">array = [1, 2, NaN, 4, NaN, 8] # Remove NaN values array_cleaned = array[~numpy.isnan(array)] print(array_cleaned) # Output: [1, 2, 4, 8]</code>
以上がNumPy 配列から NaN (非数値) 値を削除するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。