在NumPy中出现的奇怪索引问题消耗了太多内存。

huangapple go评论76阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月6日 17:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定