## Fix Python – Counting the number of non-NaN elements in a numpy ndarray in Python

I need to calculate the number of non-NaN elements in a numpy ndarray matrix. How would one efficiently do this in Python? Here is my simple code for achieving this:
import numpy as np

def numberOfNonNans(data):
count = 0
for i in data:
if not np.isnan(i):
count += 1
return count

Is there a built-in function for….

## Fix Python – how does multiplication differ for NumPy Matrix vs Array classes?

The numpy docs recommend using array instead of matrix for working with matrices. However, unlike octave (which I was using till recently), * doesn’t perform matrix multiplication, you need to use the function matrixmultipy(). I feel this makes the code very unreadable.
Does anybody share my views, and has found a solution?
….

## Fix Python – numpy get index where value is true

>>> ex=np.arange(30)
>>> e=np.reshape(ex,[3,10])
>>> e
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]])
>>> e>15
array([[False, False, False, False, False, False, False, False, False,
False],
[False, False, False, False, False, False, ….

## Fix Python – How to get element-wise matrix multiplication (Hadamard product) in numpy?

I have two matrices
a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])

and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling
[[5,12], [21,32]]
I have tried
print(np.dot(a,b))

and
print(a*b)

but both give the result
[[19 22], [43 50]]
which is the matrix product, not the element-wise product. How can I get the ….

## Fix Python – How do I find the length (or dimensions, size) of a numpy matrix in python? [duplicate]

Numpy array dimensions

Closed 9 years ago.

For a numpy matrix in python
from numpy import matrix
A = matrix([[1,2],[3,4]])

How can I find the length of a row (or column) o….

## Fix Python – Convert a 1D array to a 2D array in numpy

I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this:
> import numpy as np
> A = np.array([1,2,3,4,5,6])
> B = vec2matrix(A,ncol=2)
> B
array([[1, 2],
[3, 4],
[5, 6]])

Does numpy have a function that works like my made-up function ….

## Fix Python – Numpy matrix to array

I am using numpy. I have a matrix with 1 column and N rows and I want to get an array from with N elements.
For example, if i have M = matrix([, , , ]), I want to get A = array([1,2,3,4]).
To achieve it, I use A = np.array(M.T). Does anyone know a more elegant way to get the same result?
Thanks!
….

## Fix Python – numpy matrix vector multiplication [duplicate]

how does multiplication differ for NumPy Matrix vs Array classes?

Closed 8 years ago.

When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x….

## Fix Python – Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if we want to do numpy.dot(M[:,0], numpy.ones((1, R))) where R is the number of rows (of course, the same issue also occurs column-wise). We will get matr….