NumPy mmap:"ValueError:可用数据的大小不是数据类型大小的倍数." [英] NumPy mmap: "ValueError: Size of available data is not a multiple of data-type size."

查看:376
本文介绍了NumPy mmap:"ValueError:可用数据的大小不是数据类型大小的倍数."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从"data.txt"获取到一个numpy数组中,并使用matplotlib对其进行绘制.数据的每一行都是这样的:

I'm trying to get data from "data.txt" into a numpy array and plot it with matplotlib. This is what each line of the data looks like:

"1" 11.658870417634 4.8159509459201

"1" 11.658870417634 4.8159509459201

大约有一千万行.

我正在尝试将其放入内存映射,但始终会收到此错误:

I'm trying to get it into a memory map, but keep getting this error:

ValueError: Size of available data is not a multiple of data-type size.

这是我正在使用的代码:

Here is the code I am using:

import numpy
import matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt

datatype=[('index',numpy.int), ('floati',numpy.float32), ('floatq',numpy.float32)]
filename='data.txt'

def main():
    data = numpy.memmap(filename, datatype, 'r') 
    plt.plot(data['floati'],data['floatq'],'r,')
    plt.grid(True)
    plt.title("Signal-Diagram")
    plt.xlabel("Sample")
    plt.ylabel("In-Phase")
    plt.savefig('foo2.png')

if __name__ == "__main__":
    main()  

如果可以的话,请帮助我找出问题所在,

If you could please help me figure out where I'm going wrong, I would greatly appreciate it.

推荐答案

这是一个文本文件,而不是二进制文件. memmap仅在数据以二进制形式存储时才有效(即以数组形式存储在内存中).

That's a text file, not a binary file. memmap only works if the data is stored as binary (i.e. stored as the array would be stored in memory).

您可以逐行读取文件并将转换后的每一行存储在可写的memmap中,从而将文件转换为二进制文件.

You can convert the file to binary by reading it in, line-by-line, and storing each converted line in a writable memmap.

这篇关于NumPy mmap:"ValueError:可用数据的大小不是数据类型大小的倍数."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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