【问题标题】:Creating a 2d NumPy array (Python)创建二维 NumPy 数组(Python)
【发布时间】:2019-02-15 03:09:57
【问题描述】:

NumPy 新手。

我在下面的 np_2d 中创建了一个简单的二维数组。效果很好。

当然,我通常需要通过附加和/或连接现有数组来创建 N 维数组,所以接下来我会尝试。

np.append 方法(有或没有轴参数)似乎没有做任何事情。

我尝试使用 .concantenate() 和/或简单地用 np 数组替换原始列表也失败了。

我确信这很简单……对我的 ATM 来说并不简单。有人可以将我推向正确的方向吗?泰。

import numpy as np

# NumPy 2d array:
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79], [65.4, 59.2, 63.6, 88.4, 68.7]])

print (np_2d) 

# [[ 1.73  1.68  1.71  1.89  1.79]
# [65.4  59.2  63.6  88.4  68.7 ]]

print (np_2d[1]) # second list

# [65.4 59.2 63.6 88.4 68.7]

np_2d_again = np.array([1.1, 2.2, 3.3])

np.append(np_2d_again, [4.4, 5.5, 6.6])
print(np_2d_again)

# wrong: [1.1 2.2 3.3], expect [1.1 2.2 3.3], [4.4, 5.5, 6.6]
# or MAYBE [1.1 2.2 3.3, 4.4, 5.5, 6.6]


np_2d_again = np.array([[1.1, 2.2, 3.3]])
np.concatenate(np_2d_again, np.array([4.4, 5.5, 6.6]))

# Nope: TypeError: only integer scalar arrays can be converted to a scalar index

print(np_2d_again)

np_height = np.array([1.73, 1.68, 1.71, 1.89, 1.79])
np_weight = np.array([65.4, 59.2, 63.6, 88.4, 68.7])

np2_2d_again = np.array(np_height, np_weight)

# Nope: TypeError: data type not understood

height = [1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]

np2_2d_again = np.array(height, weight)

# Nope: TypeError: data type not understood

【问题讨论】:

  • 使用A = np.append(B, C),append函数返回和数组并且不改变参数。 docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
  • np.concatenate:“数组必须具有相同的形状”docs.scipy.org/doc/numpy/reference/generated/…(在您的情况下不能连接 2D 和 1D 数组)。
  • 远离np.append;太容易误用了。使用np.concatenate 时,请密切注意数组形状。如果重复这样做,最好收集一个列表,并在最后做一个concatenate(或stack)。
  • 你到底想用最后一点(身高、体重)达到什么目的?
  • 谢谢大家。 @Nathan 没什么,真的。这两个列表可用于计算“最终游戏”中的 BMI,但我真的只是想弄清楚事物的行为(好奇心)与试图解决实际问题的方式

标签: python numpy


【解决方案1】:

对于此类问题,文档非常有用。在这里查看它们:

使用这些你会发现:

In [2]: np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79], [65.4, 59.2, 63.6, 88.4, 68.7]])
   ...: 
In [2]: np_2d
Out[2]: 
array([[ 1.73,  1.68,  1.71,  1.89,  1.79],
       [65.4 , 59.2 , 63.6 , 88.4 , 68.7 ]])

注意np.array的输入。它是一个列表,包含 2 个长度相等的列表。

In [3]: np_2d_again = np.array([1.1, 2.2, 3.3])
In [4]: np.append(np_2d_again, [4.4, 5.5, 6.6])
Out[4]: array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

查看np.append 文档。看看 raveling 怎么说?它将一个 (3,) 数组连接到另一个数组,结果是 (6,)。

np.append 名称不佳且经常被滥用。它不是列表附加的替代品。一方面,它不能就地运行。

在您的np.concatenate(np_2d_again, np.array([4.4, 5.5, 6.6])) 中,您会收到错误消息,因为它需要一个轴号作为第二个参数。重读文档。您需要提供要加入的阵列的列表。 np.append 可能被误导了。

concatenate的正确使用方式:

In [6]: np.concatenate([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[6]: array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])

但由于两个输入都是 (3,),它们只能在 0 轴上连接,形成 (6,) 形状。

np2_2d_again = np.array(np_height, np_weight) 也有类似的问题。第二个参数应该是一个 dtype,而不是另一个数组。你第一次正确使用了np.array

In [7]: np.array([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[7]: 
array([[1.1, 2.2, 3.3],
       [4.4, 5.5, 6.6]])

np.array 沿新轴连接组件。它将数组列表与原始列表列表基本相同。

np.stackconcatenate 的有用前端,其行为类似于np.array(在使用轴方面更灵活):

In [8]: np.stack([np_2d_again, np.array([4.4, 5.5, 6.6])])
Out[8]: 
array([[1.1, 2.2, 3.3],
       [4.4, 5.5, 6.6]])

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 2018-04-17
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多