【Numpy】解决:关于 dtype=object 的含义及坑点

0.直接上两段代码:

【Numpy】解决:关于 dtype=object 的含义及坑点

import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a)
print(type(a[0, 3]))

【Numpy】解决:关于 dtype=object 的含义及坑点
<class 'numpy.str_'>

【Numpy】解决:关于 dtype=object 的含义及坑点

import numpy as np
a = []
e = 0.3
a.append(['s1', 's2', 's3', float(e)])
a = np.array(a,dtype=object)
print(type(a[0, 3]))

【Numpy】解决:关于 dtype=object 的含义及坑点
<class 'float'>

1.解释:

【Numpy】解决:关于 dtype=object 的含义及坑点数组存储为连续的内存块。它们通常有单一的数据类型(例如整数、浮点数或固定长度的字符串),然后内存中的位被解释为具有该数据类型的值。
创建【Numpy】解决:关于 dtype=object 的含义及坑点的数组是不同的。数组占用的内存现在充满了存储在内存其他地方的【Numpy】解决:关于 dtype=object 的含义及坑点对象的指针(很像【Numpy】解决:关于 dtype=object 的含义及坑点列表实际上只是对象指针的列表,而不是对象本身)。

2.文档原话:

【Numpy】解决:关于 dtype=object 的含义及坑点 arrays are stored as contiguous blocks of memory. They usually have a single datatype (e.g. integers, floats or fixed-length strings) and then the bits in memory are interpreted as values with that datatype.
Creating an array with 【Numpy】解决:关于 dtype=object 的含义及坑点 is different. The memory taken by the array now is filled with pointers to 【Numpy】解决:关于 dtype=object 的含义及坑点 objects which are being stored elsewhere in memory (much like a 【Numpy】解决:关于 dtype=object 的含义及坑点 list is really just a list of pointers to objects, not the objects themselves).

3.存在的坑点:

【Numpy】解决:关于 dtype=object 的含义及坑点所示,如果多【Numpy】解决:关于 dtype=object 的含义及坑点几行后要对【Numpy】解决:关于 dtype=object 的含义及坑点矩阵按照第三列排序时使用np.argsort()函数时【Numpy】解决:关于 dtype=object 的含义及坑点会默认按照str类型的字典序排序,影响最终排序结果!!!!且你如果不懂的话很难发现!!

【Numpy】解决:关于 dtype=object 的含义及坑点
【Numpy】解决:关于 dtype=object 的含义及坑点

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年3月5日
下一篇 2023年3月5日

相关推荐