如何使用某些列作为特定类型加载numpy数组 [英] How to load numpy array with certain columns as specific type

查看:48
本文介绍了如何使用某些列作为特定类型加载numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下操作:

 >>>arr2 = [[0,0,0,-0.9,0.3],[0,0,1,0.9,0.6],[0,1,0,-0.2,0.6],[0,1,1,0.8,0.3],[1、0、1、0.2、1.0],[1、1、0,-0.8、1.0]]>>>narr2 = np.array(arr2)>>>narr2array([[0.,0.,0.,-0.9,0.3],[0.,0.,1.,0.9,0.6],[0.,1.,0.,-0.2,0.6],[0.,1.,1.,0.8,0.3],[1.,0.,1.,0.2,1.],[1.,1.,0.,-0.8,1.]]) 

如何使前三列的类型为 int ?这就是我的关注方式:

 >>>narr2数组([[0,0,0,-0.9,0.3],[0,0,1,0.9,0.6],[0,1,0,-0.2,0.6],[0,1,1,0.8,0.3],[1,0,1,0.2,1],[1,1,0,-0.8,1.]]) 

我尝试了以下操作:

 >>>narr2 = np.array(arr2,dtype ='i4,i4,i4,f8,f8')>>>narr2array([[(0,0,0,0.,0.),(0,0,0,0.,0.),(0,0,0,0.,0.),(0,0,0,-0.9,-0.9),(0,0,0,0.3,0.3)],[(0,0,0,0.,0.),(0,0,0,0.,0.),(1,1,1,1.,1.),(0,0,0,0.9,0.9),(0,0,0,0.6,0.6)],[(0,0,0,0.,0.),(1,1,1,1,1.,1.),(0,0,0,0.,0.),(0,0,0,-0.2,-0.2),(0,0,0,0.6,0.6)],[(0,0,0,0.,0.),(1,1,1,1,1.,1.),(1,1,1,1.,1.),(0,0,0,0.8,0.8),(0,0,0,0.3,0.3)],[(1,1,1,1,1.,1.),(0,0,0,0.,0.),(1,1,1,1,1.,1.),(0,0,0,0.2,0.2),(1,1,1,1.,1.)],[(1,1,1,1. 1.,1.),(1,1,1,1. 1.,1.),(0,0,0,0.,0.),(0,0,0,-0.8,-0.8),(1,1,1,1.,1.)]],dtype = [('f0','< i4'),('f1','< i4'),('f2','< i4'),('f3','< f8'),('f4','< f8')]) 

可以看出,我没有得到想要的输出.似乎我不了解在创建数组时如何指定类型以及哪里出错了.

我建议您将列表转换为元组,然后分配数据类型.这是我的解决方案:

 将numpy导入为nparr2 = [[0,0,0,-0.9,0.3],[0,0,1,0.9,0.6],[0,1,0,-0.2,0.6],[0,1,1,0.8,0.3],[1,0,1,0.2,1.0],[1,1,0,-0.8,1.0]]tupp2 = [arr2中l的元组(l)]数据类型= [('A',np.int),('B',np.int),('C',np.int),('D',np.float),('E',np.漂浮)]narr2 = np.array(tupp2,dtype = datatype) 

检查每一列的数据类型:

对于narr2 [0]中的i,

 :打印(i.dtype) 

赠予:

  int32int32int32float64float64 

I tried following:

>>> arr2 = [[0, 0, 0, -0.9, 0.3], [0, 0, 1, 0.9, 0.6], [0, 1, 0, -0.2, 0.6], [0, 1, 1, 0.8, 0.3], [1, 0, 1, 0.2, 1.0], [1, 1, 0, -0.8, 1.0]]
>>> narr2 = np.array(arr2)
>>> narr2
array([[ 0. ,  0. ,  0. , -0.9,  0.3],
       [ 0. ,  0. ,  1. ,  0.9,  0.6],
       [ 0. ,  1. ,  0. , -0.2,  0.6],
       [ 0. ,  1. ,  1. ,  0.8,  0.3],
       [ 1. ,  0. ,  1. ,  0.2,  1. ],
       [ 1. ,  1. ,  0. , -0.8,  1. ]])

How can I make first three column have type int? That is how can I get following:

>>> narr2
array([[ 0 ,  0 ,  0 , -0.9,  0.3],
       [ 0 ,  0 ,  1 ,  0.9,  0.6],
       [ 0 ,  1 ,  0 , -0.2,  0.6],
       [ 0 ,  1 ,  1 ,  0.8,  0.3],
       [ 1 ,  0 ,  1 ,  0.2,  1. ],
       [ 1 ,  1 ,  0 , -0.8,  1. ]])

I tried following:

>>> narr2 = np.array(arr2,dtype='i4,i4,i4,f8,f8')
>>> narr2
array([[(0, 0, 0,  0. ,  0. ), (0, 0, 0,  0. ,  0. ),
        (0, 0, 0,  0. ,  0. ), (0, 0, 0, -0.9, -0.9),
        (0, 0, 0,  0.3,  0.3)],
       [(0, 0, 0,  0. ,  0. ), (0, 0, 0,  0. ,  0. ),
        (1, 1, 1,  1. ,  1. ), (0, 0, 0,  0.9,  0.9),
        (0, 0, 0,  0.6,  0.6)],
       [(0, 0, 0,  0. ,  0. ), (1, 1, 1,  1. ,  1. ),
        (0, 0, 0,  0. ,  0. ), (0, 0, 0, -0.2, -0.2),
        (0, 0, 0,  0.6,  0.6)],
       [(0, 0, 0,  0. ,  0. ), (1, 1, 1,  1. ,  1. ),
        (1, 1, 1,  1. ,  1. ), (0, 0, 0,  0.8,  0.8),
        (0, 0, 0,  0.3,  0.3)],
       [(1, 1, 1,  1. ,  1. ), (0, 0, 0,  0. ,  0. ),
        (1, 1, 1,  1. ,  1. ), (0, 0, 0,  0.2,  0.2),
        (1, 1, 1,  1. ,  1. )],
       [(1, 1, 1,  1. ,  1. ), (1, 1, 1,  1. ,  1. ),
        (0, 0, 0,  0. ,  0. ), (0, 0, 0, -0.8, -0.8),
        (1, 1, 1,  1. ,  1. )]],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<f8'), ('f4', '<f8')])

As can be seen, I am not getting the desired output. Seems that I am not understanding how do I specify type while creating array and where I am going wrong.

解决方案

I propose that you convert lists to tuples, and then assign the data types. Here's my solution:

import numpy as np

arr2 = [[0, 0, 0, -0.9, 0.3], [0, 0, 1, 0.9, 0.6], 
[0, 1, 0, -0.2, 0.6], [0, 1, 1, 0.8, 0.3], [1, 0, 1, 0.2, 1.0], [1, 1, 0, -0.8, 1.0]]

tupp2 = [tuple(l) for l in arr2]

datatype = [('A', np.int), ('B', np.int), ('C', np.int), ('D', np.float), ('E', np.float)]
narr2 = np.array(tupp2, dtype=datatype)

Checking the data types for each column:

for i in narr2[0]:
    print(i.dtype)

Gives:

int32
int32
int32
float64
float64

这篇关于如何使用某些列作为特定类型加载numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆