Questions tagged [numpy-ndarray]

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy. Use this tag for questions related to this array type.

Numpy Ndarray refers to the N-dimensional array type that describes the collection of the same type in the Python library NumPy.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

3757 questions
82
votes
6 answers

What is an intuitive explanation of np.unravel_index?

I have read the documentation for np.unravel_index and played around with the function, but I can't figure out what it is doing.
austinkjensen
  • 956
  • 1
  • 8
  • 11
71
votes
6 answers

Zero pad numpy array

What's the more pythonic way to pad an array with zeros at the end? def pad(A, length): ... A = np.array([1,2,3,4,5]) pad(A, 8) # expected : [1,2,3,4,5,0,0,0] In my real use case, in fact I want to pad an array to the closest multiple of…
Basj
  • 41,386
  • 99
  • 383
  • 673
64
votes
1 answer

Deprecation status of the NumPy matrix class

What is the status of the matrix class in NumPy? I keep being told that I should use the ndarray class instead. Is it worth/safe using the matrix class in new code I write? I don't understand why I should use ndarrays instead.
59
votes
4 answers

what does numpy ndarray shape do?

I have a simple question about the .shape function, which confused me a lot. a = np.array([1, 2, 3]) # Create a rank 1 array print(type(a)) # Prints "" print(a.shape) # Prints "(3,)" b =…
Pumpkin C
  • 1,452
  • 6
  • 21
  • 27
52
votes
6 answers

How does the axis parameter from NumPy work?

Can someone explain exactly what the axis parameter in NumPy does? I am terribly confused. I'm trying to use the function myArray.sum(axis=num) At first I thought if the array is itself 3 dimensions, axis=0 will return three elements, consisting of…
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
51
votes
7 answers

How to print a Numpy array without brackets?

I want to convert a = [1,2,3,4,5] into a_string = "1 2 3 4 5". The real numpy array is quite big (50000x200) so I assume using for loops is too slow.
Framester
  • 33,341
  • 51
  • 130
  • 192
47
votes
6 answers

Transforming a row vector into a column vector in Numpy

Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy?
39
votes
3 answers

Cannot convert list to array: ValueError: only one element tensors can be converted to Python scalars

I'm currently working with the PyTorch framework and trying to understand foreign code. I got an indices issue and wanted to print the shape of a list. The only way of doing so (as far as Google tells me) is to convert the list into a numpy array…
Henning
  • 545
  • 1
  • 8
  • 16
32
votes
2 answers

Numpy mean of nonzero values

I have a matrix of size N*M and I want to find the mean value for each row. The values are from 1 to 5 and entries that do not have any value are set to 0. However, when I want to find the mean using the following method, it gives me the wrong mean…
HimanAB
  • 2,443
  • 8
  • 29
  • 43
32
votes
2 answers

How to convert a 3d numpy array to 2d

I have a 3d matrix like this np.arange(16).reshape((4,2,2)) array([[[ 0, 1], [ 2, 3]], [[ 4, 5], [ 6, 7]], [[ 8, 9], [10, 11]], [[12, 13], [14, 15]]]) and would like to stack them in…
27
votes
1 answer

Specific type annotation for NumPy ndarray using mypy

Support for type annotations was added in NumPy 1.20. I'm trying to figure out how to tell mypy that an array is filled with elements of a particular type, the annotation np.ndarray[np.dcomplex] gives the mypy error "ndarray" expects no type…
John
  • 723
  • 1
  • 5
  • 12
25
votes
5 answers

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "", line 1, in t3 =…
ricky_hehe
  • 295
  • 1
  • 3
  • 5
24
votes
2 answers

Pytorch: Why is the memory occupied by the `tensor` variable so small?

In Pytorch 1.0.0, I found that a tensor variable occupies very small memory. I wonder how it stores so much data. Here's the code. a = np.random.randn(1, 1, 128, 256) b = torch.tensor(a, device=torch.device('cpu')) a_size = sys.getsizeof(a) b_size…
laridzhang
  • 1,531
  • 3
  • 11
  • 16
23
votes
3 answers

How convert a list of tuples to a numpy array of tuples?

I have a list like this: l=[(1,2),(3,4)] I want to convert it to a numpy array,and keep array item type as tuple: array([(1,2),(3,4)]) but numpy.array(l) will give: array([[1,2],[3,4)]]) and item type has been changed from tuple to…
Alex Luya
  • 9,412
  • 15
  • 59
  • 91
20
votes
7 answers

how to copy numpy array value into higher dimensions

I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it arr = np.expand_dims(arr,…
user1871528
  • 1,655
  • 3
  • 27
  • 41