将复杂数据读入numpy数组 [英] Reading complex data into numpy array

查看:399
本文介绍了将复杂数据读入numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将复杂的数字从文本文件读取到numpy数组中.我的问题与此类似.写作和阅读使用numpy.savetxt和numpy.loadtxt 复数,但是,这里的解决方案是更改数据保存的格式.由于文本文件是由其他软件生成的,因此我无法更改,因此我没有那么奢侈.文本文件的示例如下:

I need to read complex numbers from a text file into a numpy array. My question is similar to this one Writing and reading complex numbers using numpy.savetxt and numpy.loadtxt however, the solution here is to alter the format the data is saved in. I don't have this luxury as the text file is generate by other software which I cannot change. A sample of the text file is a follows:

25 + 0i
8.43818 + -4.94194i
4.46817 + -5.08305i
4.55764 + -3.02201i
2.69138 + -5.43104i
-0.151334 + -4.83717i
1.98336 + -1.3339i
3.59002 + -0.932973i
1.42727 + -0.617317i
1.02005 + -1.14214i
-0.15564 + 2.74564i

我尝试了以下操作:

np.loadtxt('file.txt',delimiter='\n',dtype=np.complex128)

但是我得到了错误:

ValueError: complex() arg is a malformed string

我读过的帖子暗示这是某些行中的+ -表示法的问题,但是,即使删除了多余的+符号,我也会遇到相同的错误.

The posts I have read suggest this is an issue with the + - notation in some lines, however, I get the same error even if the extra + signs are removed.

推荐答案

但是,这里的解决方案是更改数据保存的格式

however, the solution here is to alter the format the data is saved in

好消息,您不必!

numpy.loadtxt可以采用任何可重复的行,而不仅仅是文件对象.

numpy.loadtxt can take any iterable of lines, not just a file object.

因此,您可以将文件对象包装在一个简单的生成器中,该生成器可以动态转换行,并将其提供给loadtxt,每个人都会很高兴.

So, you can wrap your file object in a simple generator that transforms the lines on the fly, and feed that to loadtxt, and everyone will be happy.

赞:

def transform_complex(line):
    # insert your code here

with open('file.txt', 'rb') as f:
    lines = map(transform_complex, f)
    arr = np.loadtxt(lines, dtype=np.complex128)

(如果您使用的是Python 2.x,并且文件很大,则可能要使用itertools.imap而不是map.)

(If you're using Python 2.x, and the file is large, you probably want to use itertools.imap rather than map.)

在在此处插入代码"部分中,您输入了行之有效的答案,但这不是可接受的解决方案,因为它需要修改文件.由于在您的链接中没有看到这样的答案,因此我不确定这是什么,但是例如,也许是这样:

The "insert your code here" part, you fill in from the answer that worked, but wasn't an acceptable solution because it required modifying the files. Since I don't see such an answer in your link, I'm not sure what that is, but for example, maybe it's this:

def transform_complex(line):
    return line.replace(b'+ -', b'- ')


在本地进行测试,看来您的输入实际上存在三件事.


Testing things out locally, it looks like there are actually three things wrong with your input.

您可以使用savetxt测试输出应该的外观.例如:

You can test what the output should look like using savetxt. For example:

>>> arr = np.array([1-2j])
>>> f = io.BytesIO()
>>> np.savetxt(f, arr)
>>> f.getvalue()
b' (1.000000000000000000e+00-2.000000000000000000e+00j)\n'

(在Python 2.x中,您不会看到b前缀.)

(In Python 2.x, you won't see the b prefix.)

并非所有这些差异都是相关的-您不必使用指数表示法,不需要括号,等等-但看起来这三个是:

Not all of those differences turn out to be relevant—you don't have to use exponential notation, you don't need parens, etc.—but it looks like these three are:

  • +周围不能有空格,不能使用复数.
  • 虚数单位必须为j,而不是i.
  • 不允许+-.
  • No spaces allowed around the + in complex numbers.
  • The imaginary unit has to be j, not i.
  • No +- allowed.

所以:

def transform_complex(line):
    return line.replace(b' ', b'').replace(b'+-', b'-').replace(b'i', b'j')

这篇关于将复杂数据读入numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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