本文介绍了当用genfromtxt指定dtype时,2D数组变成1D-如何防止这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如此处所示: http://library.isr.ist.utl.pt/docs/numpy/user/basics.io.genfromtxt.html#choosing-the-data-type 问题是我该如何解决?我想将genfromtxt与带有列的数据文件一起使用,例如整数,字符串,整数.The problem is how do I get around this? I want to use genfromtxt with a data file with columns that are, e.g. int, string, int.如果我这样做:dtype=(int, "|S5|", int)然后整个形状从(x,y)变为仅(x,),当我尝试使用蒙版时出现索引过多"错误.Then the entire shape changes from (x, y) to merely (x, ) and I get 'too many indices' errors when I try to use masks.当我使用dtype = None时,我会保留2D结构,但是如果该列的第一行看起来像是一个数字(它经常出现在我的数据集中),这经常会出错.When I use dtype=None I get to keep the 2D structure, but it often makes mistakes if the 1st row the column looks like it could be a number (this often occurs in my data set).我如何最好地解决这个问题?How am I best to get around this?推荐答案您不能拥有2D数组,这意味着要为每一行使用带有混合dtype的1D数组,这是不可能的.You cannot have a 2D array, it would mean having 1D arrays with mixed dtype for each row, which is not possible.拥有记录数组应该不是问题:Having an array of records shouldn't be a problem:In [1]: import numpy as npIn [2]: !cat test.txt42 foo 4140 bar 39In [3]: data = np.genfromtxt('test.txt', ..: dtype=np.dtype([('f1', int), ('f2', np.str_, 5), ('f3', int)]))In [4]: dataOut[4]:array([(42, 'foo', 41), (40, 'bar', 39)], dtype=[('f1', '<i8'), ('f2', '<U5'), ('f3', '<i8')])In [5]: data['f3']Out[5]: array([41, 39])In [6]: data['f3'][1]Out[6]: 39如果您需要屏蔽的数组,请看这里:如何在Numpy中屏蔽记录数组的元素? If you need a masked array, look here: How can I mask elements of a record array in Numpy?要按第一列的值进行屏蔽:To mask by 1st column value:In [7]: data['f1'] == 40Out[7]: array([False, True], dtype=bool)In [8]: data[data['f1'] == 40]Out[8]:array([(40, 'bar', 39)], dtype=[('f1', '<i8'), ('f2', '<U5'), ('f3', '<i8')]) 这篇关于当用genfromtxt指定dtype时,2D数组变成1D-如何防止这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 08:25