如何将常规numpy数组转换为记录数组?(how to convert regular numpy array to record array?)

编程入门 行业动态 更新时间:2024-10-26 03:22:25
如何将常规numpy数组转换为记录数组?(how to convert regular numpy array to record array?)

我读了一系列数字

np.array(f.read().split(),dtype=np.float64)

然后我使用np.reshape()将其转换为二维数组。

在此之后,如何将arr转换为记录数组? 我尝试了(类似)以下内容:

filename = 'unstructured-file.txt' nfields = 3 names = ('r','g','b') with open(filename,'r') as f: arr = np.array(f.read().split(),dtype=np.float64) arr = arr.reshape(-1,nfields) out = np.array(arr,dtype=zip(names,['float64']*length(names))

但是说TypeError: expected a readable buffer object

有什么建议么?

编辑:我想要做的主要是命名我的列。

代替

out = np.array(arr,dtype=zip(names,['float64']*length(names))

如果我用这个,

out = np.core.records.fromrecords(arr.reshape(-1,nfields),names=','.join(names))

我可以使用out['r']等,但out.dtype.names是None`。 到底是怎么回事?

EDIT2

非结构化文件看起来像

Some text More text 100 1.000000E-01 46 -1.891701E+04 1.702921E+02 -2.323660E+04 4.547841E+03 -2.778444E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -2.149862E+04 1.753467E+02 3.410277E+03 -1.034898E+05 2.778692E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 1.492281E+04 0.000000E+00 0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01 0.000000E+00 -4.774939E-01 0.000000E+00 0.000000E+00 0.000000E+00 -2.243495E-01 3.513048E-01 -2.678782E-01 3.513048E-01 -7.155493E-01 5.690034E-01 -2.678782E-01 5.690034E-01 -4.783123E-01 2.461974E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.461974E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.461974E+01 200 2.000000E-01 46 -1.891815E+04 1.421984E+02 -2.424678E+04 5.199451E+03 -2.944623E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -2.174561E+04 1.274613E+02 -6.004790E+01 -1.139308E+05 2.944807E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 1.445855E+04 0.000000E+00 0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01 0.000000E+00 7.785923E-01 0.000000E+00 0.000000E+00 0.000000E+00 8.123304E-01 3.023486E-01 -5.891595E-01 3.023486E-01 -8.560144E-02 -3.830618E-01 -5.891595E-01 -3.830618E-01 1.608437E+00 2.436174E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.436174E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.436174E+01

I read in a sequence of numbers with

np.array(f.read().split(),dtype=np.float64)

Then I convert this to a 2-D array using np.reshape().

After this, how do to convert arr to a record array? I've tried (something like) the following:

filename = 'unstructured-file.txt' nfields = 3 names = ('r','g','b') with open(filename,'r') as f: arr = np.array(f.read().split(),dtype=np.float64) arr = arr.reshape(-1,nfields) out = np.array(arr,dtype=zip(names,['float64']*length(names))

but says TypeError: expected a readable buffer object

Any suggestions?

Edit: The main thing I want to do is to name my columns.

Instead of

out = np.array(arr,dtype=zip(names,['float64']*length(names))

If I use this,

out = np.core.records.fromrecords(arr.reshape(-1,nfields),names=','.join(names))

I can use out['r'] and so on, but out.dtype.names is None`. What is going on?

Edit2

The unstructured file looks like

Some text More text 100 1.000000E-01 46 -1.891701E+04 1.702921E+02 -2.323660E+04 4.547841E+03 -2.778444E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -2.149862E+04 1.753467E+02 3.410277E+03 -1.034898E+05 2.778692E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 1.492281E+04 0.000000E+00 0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01 0.000000E+00 -4.774939E-01 0.000000E+00 0.000000E+00 0.000000E+00 -2.243495E-01 3.513048E-01 -2.678782E-01 3.513048E-01 -7.155493E-01 5.690034E-01 -2.678782E-01 5.690034E-01 -4.783123E-01 2.461974E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.461974E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.461974E+01 200 2.000000E-01 46 -1.891815E+04 1.421984E+02 -2.424678E+04 5.199451E+03 -2.944623E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -2.174561E+04 1.274613E+02 -6.004790E+01 -1.139308E+05 2.944807E+04 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 1.445855E+04 0.000000E+00 0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01 0.000000E+00 7.785923E-01 0.000000E+00 0.000000E+00 0.000000E+00 8.123304E-01 3.023486E-01 -5.891595E-01 3.023486E-01 -8.560144E-02 -3.830618E-01 -5.891595E-01 -3.830618E-01 1.608437E+00 2.436174E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.436174E+01 0.000000E+00 0.000000E+00 0.000000E+00 2.436174E+01

最满意答案

要将普通numpy数组转换为结构化数组,请使用view :

import numpy as np filename = 'unstructured-file.txt' nfields = 3 names = ('r','g','b') with open(filename,'r') as f: arr = np.array(f.read().split(),dtype=np.float64) arr = arr.reshape(-1,nfields) out = arr.view(dtype=zip(names,['float64']*len(names))).copy()

To convert a plain numpy array to a structured array, use view:

import numpy as np filename = 'unstructured-file.txt' nfields = 3 names = ('r','g','b') with open(filename,'r') as f: arr = np.array(f.read().split(),dtype=np.float64) arr = arr.reshape(-1,nfields) out = arr.view(dtype=zip(names,['float64']*len(names))).copy()

更多推荐

本文发布于:2023-07-29 15:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   如何将   常规   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!