so I have created this array as an example:
a = np.array([[1, 1, 1, 1, 2], [2, 2, 2, 3], [3, 3, 3, 4], [13, 49, 13, 49], [10, 10, 2, 2],
[11, 1, 1, 1, 2], [22, 2, 2, 3], [33, 3, 3, 4], [133, 49, 13, 49], [100, 10, 2, 2],
[5, 1, 1, 1, 2], [32, 2, 2, 3], [322, 3, 3, 4], [13222, 49, 13, 49], [130, 10, 2, 2]])
I wanted to create a 2d array. So for example in this case, 15*5 array.
However, when I use the a.shape, it returns (15,)
What is wrong with my array definition?
Numpy arrays can only be defined when each axis has the same number of elements. Otherwise, you are left with an 1D array of objects.

This is what is happening with your array. You have a list of lists, which contains a variable number of elements (some 4 and some 5). This during conversion turns it into a (15,) numpy arrays where the arrays has 15 separate list-objects.
a = np.array([[1, 1, 1, 1, 2], [2, 2, 2, 3], [3, 3, 3, 4], [13, 49, 13, 49]
## |______________| |__________|
## | |
## 5 length 4 length
#Variable length sublists
print(np.array([[1,2,3], [4,5]]))
#Fixed length sublists
print(np.array([[1,2,3], [4,5,6]]))
array([list([1, 2, 3]), list([4, 5])], dtype=object) #This is (2,)
array([[1, 2, 3], #This is (2,3)
[4, 5, 6]])
Check what your array really is:
import numpy as np
a = np.array([[1, 1, 1, 1, 2], [2, 2, 2, 3], [3, 3, 3, 4], [13, 49, 13, 49], [10, 10, 2, 2],
[11, 1, 1, 1, 2], [22, 2, 2, 3], [33, 3, 3, 4], [133, 49, 13, 49], [100, 10, 2, 2],
[5, 1, 1, 1, 2], [32, 2, 2, 3], [322, 3, 3, 4], [13222, 49, 13, 49], [130, 10, 2, 2]])
print(repr(a))
It is a an array of list objects:
array([list([1, 1, 1, 1, 2]), list([2, 2, 2, 3]), list([3, 3, 3, 4]),
list([13, 49, 13, 49]), list([10, 10, 2, 2]),
list([11, 1, 1, 1, 2]), list([22, 2, 2, 3]), list([33, 3, 3, 4]),
list([133, 49, 13, 49]), list([100, 10, 2, 2]),
list([5, 1, 1, 1, 2]), list([32, 2, 2, 3]), list([322, 3, 3, 4]),
list([13222, 49, 13, 49]), list([130, 10, 2, 2])], dtype=object)
The problem is that the number of elements in each sublist varies (some of them 4, some of them 5), so it can only be stored as a 1D array of lists. To get a 2D array, the number of elements in your sublists must be equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With