英文:
Weird Indexing problem in NumPy consuming too much memory
问题
I am just learning indexing in NumPy and I am seeing these weird indexing issues with NumPy arrays. Here for instance, when I try to index this simple 1-D array;
import numpy as np
arr = np.ndarray([5, 4, 10, 20, 25, 81, 9, 7])
print(arr[2])
I get the following weird output
[[[[[[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
with tons and tons of zero matrices. And when I index a slightly bigger array, it gives me this problem;
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 229. PiB for an array with shape (5, 4, 10, 20, 25, 81, 9, 7, 914, 23, 5, 7, 86) and data type float64
Why is NumPy behaving so weirdly and consuming so much memory just of a simple 1-D array and unable to perform indexing properly? For the IDE, I am using IntelliJ with a Conda venv.
英文:
I am just learning indexing in NumPy and I am seeing these weird indexing issues with NumPy arrays. Here for instance, when I try to index this simple 1_D array;
import numpy as np
arr = np.ndarray([5, 4, 10, 20, 25, 81, 9, 7])
print(arr[2])
I get the following weird output
[[[[[[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
with tons and tons of zero matrices. And when I index a slightly bigger array, it gives me this problem;
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 229. PiB for an array with shape (5, 4, 10, 20, 25, 81, 9, 7, 914, 23, 5, 7, 86) and data type float64
Why is NumPy behaving so weirdly and consuming so much memory just of a simple 1_D array and unable to perform indexing properly? For the IDE, I am using IntelliJ with a Conda venv.
答案1
得分: 3
你正在使用 np.ndarray
,可能你想使用 np.array
。
你在这里做的是创建一个有 5 * 4 * 10 * 20 * 25 * 81 * 9 * 7
元素的8维数组。
英文:
You're using np.ndarray
, when you probably meant to use np.array
.
What you're doing here is creating an 8-dimensional array of 5 * 4 * 10 * 20 * 25 * 81 * 9 * 7
elements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论