在Python的NumPy中,将列表作为二维数组的元素。

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

Put a list as element in 2D array in Python numpy

问题

  1. 我从numpy得到了一个2D矩阵np.zeros),我希望这个矩阵的每个元素都是一个具有2个元素的1D列表这是我的代码
  2. ```python
  3. import numpy as np
  4. N = 3
  5. x = np.linspace(0, 10, N)
  6. y = np.linspace(0, 10, N)
  7. temp = np.zeros((N, N, 2))
  8. for i in range(len(x)):
  9. for j in range(len(y)):
  10. temp[i, j] = [x[i], y[j]]

但是我得到了错误:

  1. TypeError: float() argument must be a string or a real number, not 'list'
  2. 上述异常直接导致了以下异常
  3. Traceback (most recent call last):
  4. File (...), line 15, in <module>
  5. temp[i, j] = [x[i], y[j]]
  6. ValueError: setting an array element with a sequence.

我理解了这个问题,但是不知道如何解决它。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;ve got a 2D matrix from numpy (np.zeros) and I would like that every element of this matrix is a 1D list with 2 elements, this is my code:

import numpy as np

N = 3
x = np.linspace(0,10,N)
y = np.linspace(0,10,N)

temp = np.zeros((N, N))

for i in range(len(x)):
for j in range(len(y)):
temp[i, j] = [x[i], y[j]]

  1. but I&#39;ve got error:

TypeError: float() argument must be a string or a real number, not 'list'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File (...), line 15, in <module>
temp[i, j] = [x[i], y[j]]
ValueError: setting an array element with a sequence.

  1. And okay, I understand this problem, but I don&#39;t know how to solve it.
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 你可以在构建数组时设置`dtype=object`并存储列表,但这样效率较低。也可以定义自定义的dtype
  6. ```python
  7. import numpy as np
  8. # 定义包含两个float64字段的自定义类型
  9. pair_type = np.dtype("f8, f8")
  10. x = np.zeros((3, 3), dtype=pair_type)
  11. x[0, 0] = (1.5, 2.5)

你可以将x[i, j]检索为一个元组。

英文:

You can set the dtype=object when constructing the array and store lists, but that's quite inefficient. It's possible to define a custom dtype:

  1. import numpy as np
  2. # custom type of 2 float64 fields
  3. pair_type = np.dtype(&quot;f8, f8&quot;)
  4. x = np.zeros((3, 3), dtype=pair_type)
  5. x[0, 0] = (1.5, 2.5)

You can retrieve x[i, j] as a tuple.

答案2

得分: 0

以下是翻译好的部分:

使用numpy.meshgrid + numpy.stack,可以方便地获得您正在搜索的结果:

  1. np.stack(np.meshgrid(x, y, indexing='ij'), axis=-1)
  1. array([[[ 0., 0.],
  2. [ 0., 5.],
  3. [ 0., 10.]],
  4. [[ 5., 0.],
  5. [ 5., 5.],
  6. [ 5., 10.]],
  7. [[10., 0.],
  8. [10., 5.],
  9. [10., 10.]]])
英文:

The result you are searching for can be conveniently achieved with numpy.meshgrid + numpy.stack:

  1. np.stack(np.meshgrid(x, y, indexing=&#39;ij&#39;), axis=-1)

  1. array([[[ 0., 0.],
  2. [ 0., 5.],
  3. [ 0., 10.]],
  4. [[ 5., 0.],
  5. [ 5., 5.],
  6. [ 5., 10.]],
  7. [[10., 0.],
  8. [10., 5.],
  9. [10., 10.]]])

huangapple
  • 本文由 发表于 2023年2月27日 01:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573682.html
匿名

发表评论

匿名网友

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

确定