w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Why does np.median() return multiple rows?

That's really strange, I think you should get (16026,), are we missing something here:

In [241]:

X_train=np.random.random((1000,16026)) #1000 can be any int.
indices = np.random.randint(0, 60, 100) #60 can be any int.
tempArray = X_train[indices, ]
medArray = np.median(tempArray, axis=0)
print(medArray.shape)

(16026,)

And the only way you can get a 2d array result is:

In [243]:

X_train=np.random.random((100,2,16026))
indices = np.random.randint(0, 60, 100)
tempArray = X_train[indices, ]
medArray = np.median(tempArray, axis=0)
print(medArray.shape)


(2, 16026)

When you have a 3d array input.

When it is a sparse array, a dumb way to get around this might be:

In [319]:

X_train = sparse.rand(112, 16026, 0.5, 'csr') #just make up a random sparse
array
indices = np.random.randint(0, 60, 100)
tempArray = X_train[indices, ]
medArray = np.median(tempArray.toarray(), axis=0)
print(medArray.shape)
(16026,)

.toarray() might also go to the 3rd line instead. But either way, this means the 0's are also counted as @zhangxaochen pointed out.

Out of ideas, there may be better explanations for it.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.