【问题标题】:Using Dictionary in Numpy Array dosent make that array to have single Data Type在 Numpy 数组中使用字典使该数组具有单一数据类型
【发布时间】:2020-01-13 06:50:15
【问题描述】:

我是 python 和学习 Numpy 的新手。我已经阅读和测试的是 np.array 具有单一数据类型。当我在普通代码上使用它时,它可以正常工作并且表现良好。即

import numpy as np 
np1 = np.array([1,'2' , True])

for i in np1:
   print(type(i))

答案是

<class 'numpy.str_'>
<class 'numpy.str_'>
<class 'numpy.str_'>

但是当我的代码是

np2 = np.array([{1:1 , 2:2 }, 1 , True , '1'])
for i in np2:
    print(type(i))

答案是

<class 'dict'>
<class 'int'>
<class 'bool'>
<class 'str'>

这表明元素不是 numpy 类,因为上面的答案是&lt;class 'numpy.str'&gt;。 当我打印 print(type(np2)) 时,答案是 &lt;class 'numpy.ndarray'&gt;。 你能解释为什么它们不是相同的数据类型吗?谢谢

【问题讨论】:

    标签: python arrays numpy data-science


    【解决方案1】:

    如果未给出数组所需的数据类型,则类型“将被确定为在序列中保存对象所需的最小类型。

    在第一种情况下,最小类型是str,因为每个项目都可以转换为字符串。新数组包含字符串。

    第二种情况,最小类型是object(因为&lt;class 'str'&gt;dict不能转换成字符串)。新数组包含对对象的引用。每个对象都有自己的类型。

    您可以强制 np1 成为对象数组:

    np1 = np.array([1, '2' , True], dtype=object)
    type(np1[0]))
    #<class 'int'>
    type(np1[1]))
    #<class 'str'>
    type(np1[2]))
    #<class 'bool'>
    

    【讨论】:

      【解决方案2】:

      在交互式ipython 会话中,诸如数组之类的对象以其repr 表示形式显示。我觉得这个信息量很大:

      In [41]: np1 = np.array([1,'2' , True])                                                                   
      In [42]: np1                                                                                              
      Out[42]: array(['1', '2', 'True'], dtype='<U21')
      

      注意引号和U21 dtype。两者都表明数组包含字符串,数字和布尔值都已转换为通用字符串 dtype。

      In [43]: np2 = np.array([{1:1 , 2:2 }, 1 , True , '1'])                                                   
      In [44]: np2                                                                                              
      Out[44]: array([{1: 1, 2: 2}, 1, True, '1'], dtype=object)
      In [45]: [{1:1 , 2:2 }, 1 , True , '1']                                                                   
      Out[45]: [{1: 1, 2: 2}, 1, True, '1']
      

      注意object dtype。元素显示与列表基本相同。这样的数组实际上是一个列表。有一些区别,但出于许多目的,它可以被视为一个列表。它与列表相比几乎没有优点,也有一些缺点。它没有数字 numpy 数组的计算速度。

      对象 dtype 数组的数据缓冲区类似于列表的底层缓冲区。两者都包含指向存储在内存中其他位置的对象的指针或引用。从这个意义上说,它确实有一个单一的数据类型——一个引用。

      ===

      如果我创建一个列表,然后从该列表中创建一个对象 dtype 数组:

      In [48]: alist = [{1:1 , 2:2 }, 1 , True , '1']                                                           
      In [49]: arr = np.array(alist)                                                                            
      In [50]: arr                                                                                              
      Out[50]: array([{1: 1, 2: 2}, 1, True, '1'], dtype=object)
      

      我可以证明数组中的字典与列表中的字典相同。他们有相同的id

      In [51]: id(arr[0])                                                                                       
      Out[51]: 140602595005568
      In [52]: id(alist[0])                                                                                     
      Out[52]: 140602595005568
      

      以及对列表的修改,显示在数组中:

      In [53]: alist[0][3]=3                                                                                    
      In [54]: arr                                                                                              
      Out[54]: array([{1: 1, 2: 2, 3: 3}, 1, True, '1'], dtype=object)
      

      【讨论】:

        【解决方案3】:

        请参考documentation。它支持的第一个特性是它是

        强大的 N 维数组对象

        因此,它将任何元素作为对象处理

        另一件事:

        除了其明显的科学用途外,NumPy 还可以用作通用数据的高效多维容器。可以定义任意数据类型。这使 NumPy 可以无缝、快速地与各种数据库集成。

        因此,NumPy 数组会尽可能有效地将其元素存储为相同的数据类型以优化性能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-09
          • 1970-01-01
          • 1970-01-01
          • 2016-10-23
          • 2015-10-16
          • 2014-05-18
          • 2016-01-08
          相关资源
          最近更新 更多