Create numpy matrix filled with NaNs
Create a NumPy matrix filled exactly with np.nan
using the np.full()
function:
Output: A 3x3 matrix in which every element is nan
.
Performance aspects: np.full()
vs np.empty().fill()
Using np.full()
The np.full()
function works out of the box and rightly so:
More performant way with np.empty().fill()
However, if you're dealing with large datasets and your performance need to be as slick as a Tesla Model S on a German autobahn, consider using the np.empty().fill()
combo. Here's how:
This operation is in-place — it alters the matrix itself, and hence returns None
.
Performance superiority: Benchmarking
Running a marathon between np.full()
and np.empty().fill()
. Who's gonna win?
For large sizes, expect np.empty().fill()
to have the edge. But hey, don't take my word for it... run the code and see for yourself!
Creating NaN matrices: Beyond the basics
Custom NaN matrix function
Savor the luxury of having your own function for creating NaN matrices to fit your wildest desires. Shape describes the matrix dimensions and dtype=float
defines the type of the data.
Comparing nan_matrix[:] = np.nan
and nan_matrix.fill(np.nan)
If you're left wondering "What if I just set all the elements to np.nan
?", consider this fact: nan_matrix.fill(np.nan)
is typically faster because it performs a very low-level in-place operation.
Kneading NumPy: Advanced insights
np.full()
: More like np.fun()
!
np.full()
has been around since NumPy version 1.8. So, assuming you're not coding on a dinosaur, you should have access to this nifty function.
Multiplying ones with np.nan
, because why not
One seldom used alternative method is to create an array of ones and multiply it by np.nan
.
This isn't the most efficient solution, but it's still an option if readability is your top concern.
Data type dance
If you're trying to cram your NaNs into a non-float64
type, you're gonna have a bad time. By default, NaNs in NumPy are float64
.
What's under the hood
There's a lot to learn from NumPy's documentation and the community. They can help you understand the implementation of its functions better, making you a more capable NumPy Matrix "A NaNtist".
Was this article helpful?