我尝试为numpy数组的每一列转换dtype,但我没有运气。我尝试了在stackoverflow上找到的一些解决方案(例如使用astype()
函数为每一列作为here或尝试显式声明np.array(..., dtype = [])
的数据类型作为建议here,但没有任何效果...
特别是对于第二个解决方案,如果我尝试这个代码
with open('posizioni_mm.txt') as f:
a, b, c, d, e = [x for x in next(f).split()]
dt = [('A', 'int'), ('B', 'float'), ('C', 'float'), ('D', 'float'), ('E', '<U32')]
array = np.array([([x for x in line.split()]) for line in f], dtype = dt )
我得到这个错误ValueError: could not convert string to float: 'link_2'
因为数组被移动了一列('link_2'应该是E列,它的dtype应该是string,但它被放在D列),如果我试图生成没有数据类型的数组,然后生成一个有正确dtype的空数组array2 = np.zeros(np.shape(array), dtype = dt)
它生成一个数组,每行包含5个元素的5元组
[[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]
[(0, 0., 0., 0., '') (0, 0., 0., 0., '') (0, 0., 0., 0., '')
(0, 0., 0., 0., '') (0, 0., 0., 0., '')]]
怎么了?
编辑:这是文件posizioni_mm.txt的内容
30 0.78 227.31 270.7 link_1
30 0.73 310.79 268.72 link_2
30 0.32 141.41 276.87 link_3
35 0.45 72.47 212.11 link_4
30 0.18 163.22 205.95 link_5
8 0.25 109.69 207.49 link_6
24 0.3 295.15 197.14 link_7
45 0.3 390.97 179.96 link_8
8 0.45 153.08 101.1 link_9
33 0.36 87.44 114.32 link_10
8 0.24 285.9 94.27 link_11
30 0.42 326.87 90.97 link_12
33 0.4 221.15 96.92 link_13
18 0.2 257.49 56.61 link_14
51 0.16 194.71 53.96 link_15
4条答案
按热度按时间pn9klfpd1#
使用
numpy.loadtxt
从文本文件简单加载数据:现在您可以通过名称访问列,例如
'E'
:6tr1vspr2#
一旦你有了数组中的数据,就很容易计算字段(列)的平均值和标准差。这个答案展示了如何扩展@RomanPerekhrest上面发布的解决方案。
数据的打印值:
vulvrdjw3#
为了方便复制粘贴,我用一个字符串列表替换你的文件:
您的复合dtype:
要从数据中创建结构化数组,您需要提供一个元组列表(而不是列表列表)。只需对代码进行微小更改即可:
loadtxt/genfromtxt也是一样:
我不知道你想用组做什么,只要记住结构化数组,你有字段,而不是列,所以二维索引不起作用,你不能,一般来说,跨字段做数学。
可以定义一个dtype,将3个float列组合在一起:
或者将所有数值列加载为float,跳过字符串1。这将得到2d float数组:
ekqde3dh4#
谢谢你们,我用一种很不优雅的方式解决了这个问题,但是…它很有效
简而言之,我使用massives.astype(...)方法将字符串(临时)转换为int或float
1.在从文件.txt创建数组后,我使用argsort和astype(int)作为array[:,0]
1.然后我对array[:,1]进行操作,使用.astype(float)计算平均值和标准值
1.然后我使用group_stats来处理其他事情(我必须选择3个累积置信水平〉0.35且标准偏差〈0.2的类)