Make a numpy upper triangular matrix padded with Nan instead of zero

user3556757

I generate a matplotlib 3d surface plot. I only need to see the upper-triangular half of the matrix on the plot, as the other half is redundant.

np.triu() makes the redundant half of the matrix zeros, but I'd prefer if I can make them Nans, then those cells don't show up at all on the surface plot.

What would be a pythonic way to fill with NaN instead of zeros? I cannot do a search-and-replace 0 with NaN, as zeros will appear in the legitimate data I want to display.

AChampion

You can use numpy.tril_indices() to assign the NaN value to lower triangle, e.g.:

>>> import numpy as np
>>> m = np.triu(np.arange(0, 12, dtype=np.float).reshape(4,3))
>>> m
array([[ 0.,  1.,  2.],
       [ 0.,  4.,  5.],
       [ 0.,  0.,  8.],
       [ 0.,  0.,  0.]])
>>> m[np.tril_indices(m.shape[0], -1)] = np.nan
>>> m
array([[  0.,   1.,   2.],
       [ nan,   4.,   5.],
       [ nan,  nan,   8.],
       [ nan,  nan,  nan]])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Upper Triangular Matrix in Scala

From Dev

Upper Triangular Matrix in Scala

From Dev

Check Upper or Lower Triangular Matrix

From Dev

Linear index upper triangular matrix

From Dev

numpy merge upper and lower triangular

From Dev

copying only upper triangular values of sympy Matrix into array from numpy.triu()?

From Dev

Numpy: Efficient way to convert indices of a square matrix to its upper triangular indices

From Dev

Create a horizontically stretched upper triangular matrix

From Dev

Melt the Upper Triangular Matrix of a Pandas Dataframe

From Dev

Scala fast generation of upper triangular matrix coordinates

From Dev

Map upper triangular matrix on vector skipping the diagonal

From Dev

How to create a specific upper triangular matrix?

From Dev

Outer function in R for upper triangular matrix

From Dev

How to create a specific upper triangular matrix?

From Dev

Reshape / Transform an upper triangular matrix in MATLAB

From Dev

Processing upper triangular elements only with NumPy einsum

From Dev

Numpy: convert an array to a triangular matrix

From Dev

R: Convert upper triangular part of a matrix to symmetric matrix

From Dev

Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

From Dev

What is the best data structure for representing an upper triangular matrix in Java?

From Dev

filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

From Dev

Finding upper/lower triangular form of arbitrary matrix n*n - python

From Dev

Fastest way to compute upper-triangular matrix of geometric series (Python)

From Dev

Convert upper triangular part of a matrix to 3-column long format

From Dev

Using numpy's genfromtxt to load a triangular matrix with python

From Dev

Correlation matrix in NumPy with NaN's

From Dev

Correlation matrix in NumPy with NaN's

Related Related

  1. 1

    Get upper triangular non-zero elements of a sparse matrix in R

  2. 2

    Get upper triangular non-zero elements of a sparse matrix in R

  3. 3

    Upper Triangular Matrix in Scala

  4. 4

    Upper Triangular Matrix in Scala

  5. 5

    Check Upper or Lower Triangular Matrix

  6. 6

    Linear index upper triangular matrix

  7. 7

    numpy merge upper and lower triangular

  8. 8

    copying only upper triangular values of sympy Matrix into array from numpy.triu()?

  9. 9

    Numpy: Efficient way to convert indices of a square matrix to its upper triangular indices

  10. 10

    Create a horizontically stretched upper triangular matrix

  11. 11

    Melt the Upper Triangular Matrix of a Pandas Dataframe

  12. 12

    Scala fast generation of upper triangular matrix coordinates

  13. 13

    Map upper triangular matrix on vector skipping the diagonal

  14. 14

    How to create a specific upper triangular matrix?

  15. 15

    Outer function in R for upper triangular matrix

  16. 16

    How to create a specific upper triangular matrix?

  17. 17

    Reshape / Transform an upper triangular matrix in MATLAB

  18. 18

    Processing upper triangular elements only with NumPy einsum

  19. 19

    Numpy: convert an array to a triangular matrix

  20. 20

    R: Convert upper triangular part of a matrix to symmetric matrix

  21. 21

    Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

  22. 22

    What is the best data structure for representing an upper triangular matrix in Java?

  23. 23

    filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

  24. 24

    Finding upper/lower triangular form of arbitrary matrix n*n - python

  25. 25

    Fastest way to compute upper-triangular matrix of geometric series (Python)

  26. 26

    Convert upper triangular part of a matrix to 3-column long format

  27. 27

    Using numpy's genfromtxt to load a triangular matrix with python

  28. 28

    Correlation matrix in NumPy with NaN's

  29. 29

    Correlation matrix in NumPy with NaN's

HotTag

Archive