为了账号安全,请及时绑定邮箱和手机立即绑定

(x,) 在 NumPy 形状中表示什么?

(x,) 在 NumPy 形状中表示什么?

回首忆惘然 2023-06-13 10:58:56
我一直在努力寻找 (x,) 在 NumPy 形状中究竟表示什么?从外观上看,我知道数组中有“x”个列/元素,这基本上是一个一维数组。但我的问题是这里的 x 后面的逗号 (x,) 表示什么?我问这个问题是因为,我正在尝试创建一个 DataFrame 并且它给了我一个错误:ValueError: Shape of passed values is (3, 1), indices imply (1, 3)我的代码:price = np.array([10, 8, 12]) df_price = pd.DataFrame(price,                          index=(["Price"]),                         columns=(["Almond Butter","Peanut Butter", "Cashew Butter"]))谁能告诉我为什么这个“价格”数组的形状在这里是 (3,1)?它不是。它是 (3,) -- 就是这样。
查看完整描述

3 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

当尝试从平面数组创建 Pandas DataFrame 时,数组必须转换为某种二维形式,因为 Pandas DataFrame 几乎总是二维的。

出现这个问题是因为你有一行三列,所以数据数组的形状应该是(1, 3). 构造函数pd.DataFrame必须在数组末尾添加一个维度,并假定第一个维度中的每个项目都是 DataFrame 中的一行。

一个简单的解决方法是将数据数组重塑为行数乘以列数。

price = np.array([10, 8, 12]).reshape(1, -1)

上面调用-1中的.reshape告诉函数推断该轴的长度。


查看完整回答
反对 回复 2023-06-13
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

我的问题是这里的 x 后面的逗号 (x,) 表示什么?

此语法是通用的 Python,并不特定于 Numpy。当我们要创建一个元组时,我们在这种情况下添加一个逗号。您应该熟悉元组,例如(3, 4). 但是,如果我们想创建一个只有一个元素的元组怎么办。您可以尝试(3),但现在 Python 将括号解释为数学表达式中的分组运算符,就像我们使用它们时一样(3 + 4) * 5。这意味着它(3)只是整数值3,而不是元组。所以我们添加一个逗号(3,)来创建一个只有一个元素的元组。


查看完整回答
反对 回复 2023-06-13
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

错误的完整回溯表明已经DataFrame对您的输入进行了相当多的处理。


In [336]: pd.DataFrame(np.arange(1,4),  

     ...:                         index=(["Price"]), 

     ...:                         columns=(["Almond Butter","Peanut Butter", "Cashew Butter"]))      

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)

   1653                 blocks = [

-> 1654                     make_block(values=blocks[0], placement=slice(0, len(axes[0])))

   1655                 ]


/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype)

   3052 

-> 3053     return klass(values, ndim=ndim, placement=placement)

   3054 


/usr/local/lib/python3.6/dist-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)

    124             raise ValueError(

--> 125                 f"Wrong number of items passed {len(self.values)}, "

    126                 f"placement implies {len(self.mgr_locs)}"


ValueError: Wrong number of items passed 1, placement implies 3


During handling of the above exception, another exception occurred:


ValueError                                Traceback (most recent call last)

<ipython-input-336-43d59803fb0f> in <module>

      1 pd.DataFrame(np.arange(1,4), 

      2                         index=(["Price"]),

----> 3                         columns=(["Almond Butter","Peanut Butter", "Cashew Butter"]))


/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)

    462                 mgr = init_dict({data.name: data}, index, columns, dtype=dtype)

    463             else:

--> 464                 mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)

    465 

    466         # For data is list-like, or Iterable (will consume into list)


/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in init_ndarray(values, index, columns, dtype, copy)

    208         block_values = [values]

    209 

--> 210     return create_block_manager_from_blocks(block_values, [columns, index])

    211 

    212 


/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)

   1662         blocks = [getattr(b, "values", b) for b in blocks]

   1663         tot_items = sum(b.shape[0] for b in blocks)

-> 1664         construction_error(tot_items, blocks[0].shape[1:], axes, e)

   1665 

   1666 


/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py in construction_error(tot_items, block_shape, axes, e)

   1692     if block_shape[0] == 0:

   1693         raise ValueError("Empty data passed with indices specified.")

-> 1694     raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")

   1695 

   1696 


ValueError: Shape of passed values is (3, 1), indices imply (1, 3)

如果我们不指定索引,它会生成一维列框:


In [337]: pd.DataFrame(np.arange(1,4))      # (3,) input                                                         

Out[337]: 

   0

0  1

1  2

2  3

与 (3,1) 输入相同:


In [339]: pd.DataFrame(np.arange(1,4)[:,None])   # (3,1) input                                                    

Out[339]: 

   0

0  1

1  2

2  3

但你想要一个(1,3):


In [340]: pd.DataFrame(np.arange(1,4)[None,:])  # (1,3) input                                                     

Out[340]: 

   0  1  2

0  1  2  3

numpy广播可以将 (3,) 数组扩展为 (1,3),但这不是它DataFrame正在做的事情。


根据您的看法,pandas 数据框可能看起来像是 2d numpy 数组的转置。系列是 1d,但垂直显示。数据框索引优先考虑列。在探索底层数据存储和values/to_numpy(). 细节很复杂。请注意,回溯讨论了“block_manager”等。


In [342]: pd.Series(np.arange(1,4))                                                                  

Out[342]: 

0    1

1    2

2    3

dtype: int64


查看完整回答
反对 回复 2023-06-13
  • 3 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信