将dtype=64数据类型的数组转换为

2024-04-19 20:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将'feature1'数组从以下数据结构转换为numpy数组,以便将其输入sklearn。但是,我在绕圈子,因为它总是告诉我dtype=object不合适,而且我无法将其转换为所需的float64格式。在

我想将所有'feature1'提取为dtype=float64的numpy数组列表,而不是从下面的结构中提取dtype=object。在

vec是先前计算返回的对象。在

>>>vec
[{'is_Primary': 1, 'feature1': [2, 2, 2, 0, 0.03333333333333333, 0], 'object_id': ObjectId('557beda51d41c8e4d1aeac25'), 'vectorized': 1},
{'is_Primary': 0, 'feature1': [2, 2, 1, 0, 0.5, 0], 'object_id': ObjectId('557beda51d41c8e4d1aeac25'), 'vectorized': 1}]

我尝试了以下方法:

^{pr2}$

还有

>>> array = np.array([x['feature1'] for x in vec])

根据另一个用户的建议,给出了类似的输出:

>>> array
>>> array([[], [], [], ..., [], [2, 2, 0, 0], []], dtype=object)

我知道我可以使用array[i]来访问'feature1'的内容,但是我想要的是将dtype=object转换为dtype=float64,并生成一个列表/dict,其中每一行都有来自vec的相应项的'feature1'。在

我也尝试过使用pandas数据帧,但是没有用。在

    >>>>pandaseries = pd.Series(df['feature1']).convert_objects(convert_numeric=True)
    >>>>pandaseries
0     []
1     []
2     []
3     []
4     []
5     []
6     []
7     []
8     []
9     []
10    []
11    []
12    []
13    []
14    []
...
7021                                                   []
7022    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 12, 2, 24...
7023                                                   []
7024                                                   []
7025                                                   []
7026                                                   []
7027                                                   []
7028    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 12, 2, 24...
7029                                                   []
7030    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 12, 2, 24...
7031                                                   []
7032                                       [2, 2, 0.1, 0]
7033                                                   []
7034                                         [2, 2, 0, 0]
7035                                                   []
Name: feature1, Length: 7036, dtype: object
    >>> 

再次返回dtype: object。我的猜测是遍历每一行并打印出一个列表。但我做不到。也许这是个新手的问题。我做错什么了?在

谢谢。在


Tags: numpyid列表objectis数组arrayobjectid
3条回答

这个:

array = numpy.array ( [ x['feature1'] for x in ver ] )

或者你需要在你的例子中更清楚。。。在

让我们以列表列表或等效的列表对象数组作为起点:

A = [[], [], [], [1,2,1], [], [2, 2, 0, 0], []]
A = array([[], [], [], [1,2,1], [], [2, 2, 0, 0], []], dtype=object)

如果子列表的长度相同,np.array([...])将给您一个2d数组,每个子列表有一行,列与它们的公共长度相匹配。但由于它们的长度不相等,它只能使其成为一个1d数组,其中每个元素都是指向这些子列表之一的指针,即dtype=object。在

我可以想象两种构造二维阵列的方法:

  • 将每个子列表填充到公共长度
  • 将每个子列表插入适当大小的空数组中。在

基本上,它需要普通的Python迭代;这并不是一个足够常见的任务,不足以拥有wizbang-numpy函数。在

例如:

^{pr2}$

要获得稀疏矩阵:

In [352]: from scipy import sparse
In [353]: MA=sparse.coo_matrix(AA)
In [354]: MA
Out[354]: 
<7x4 sparse matrix of type '<class 'numpy.int32'>'
    with 5 stored elements in COOrdinate format>

没有什么神奇的,只是简单的稀疏矩阵构造。我想你可以绕过密集矩阵

有一个列表稀疏格式的列表看起来有点像您的数据。在

In [356]: Ml=MA.tolil()

In [357]: Ml.rows
Out[357]: array([[], [], [], [0, 1, 2], [], [0, 1], []], dtype=object)

In [358]: Ml.data
Out[358]: array([[], [], [], [1, 2, 1], [], [2, 2], []], dtype=object)

可以想象,您可以构造一个空的sparse.lil_matrix((n,m))矩阵,并直接设置它的.data属性。但是您还必须计算rows属性。在

您还可以查看datarowcol属性的coo格式矩阵,并决定从A列表中构造等价物是很容易的。在

不管怎样,您必须决定如何将非零行填充到整个长度。在

您可以使用字典项的键访问其值:

d ={'a':1}
d['a']  > 1

要访问列表中的项,可以对其进行迭代或使用其索引

^{pr2}$

map方便地将函数应用于iterable的所有项,并返回结果的列表operator.getitem返回一个函数,该函数将从对象中检索项。在

import operator
import numpy as np
feature1 = operator.getitem('feature1')
a = np.asarray(map(feature1, vec))

vec = [{'is_Primary': 1, 'feature1': [2, 2, 2, 0, 0.03333333333333333, 0], 'object_id': ObjectId('557beda51d41c8e4d1aeac25'), 'vectorized': 1},
       {'is_Primary': 0, 'feature1': [2, 2, 1, 0, 0.5, 0], 'object_id': ObjectId('557beda51d41c8e4d1aeac25'), 'vectorized': 1}]

>>> a = np.asanyarray(map(feature1, vec))
>>> a.shape
(2, 6)
>>> print a
[[ 2.          2.          2.          0.          0.03333333  0.        ]
 [ 2.          2.          1.          0.          0.5         0.        ]]
>>> 
>>> for thing in a[1,:]:
    print type(thing)

<type 'numpy.float64'>
<type 'numpy.float64'>
<type 'numpy.float64'>
<type 'numpy.float64'>
<type 'numpy.float64'>
<type 'numpy.float64'>
>>> 

相关问题 更多 >