如何使用 NumPy 在 Python 中读取二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39762019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:40:25  来源:igfitidea点击:

How to read binary files in Python using NumPy?

pythonnumpymachine-learningdata-mining

提问by Suyash Shetty

I know how to read binary files in Python using NumPy's np.fromfile()function. The issue I'm faced with is that when I do so, the array has exceedingly large numbers of the order of 10^100 or so, with random nanand infvalues.

我知道如何使用 NumPy 的np.fromfile()函数在 Python 中读取二进制文件。我面临的问题是,当我这样做时,数组有非常大的数量,大约 10^100 左右,带有随机naninf值。

I need to apply machine learning algorithms to this dataset and I cannot work with this data. I cannot normalise the dataset because of the nanvalues.

我需要将机器学习算法应用于此数据集,但我无法使用此数据。由于这些nan值,我无法对数据集进行标准化。

I've tried np.nan_to_num()but that doesn't seem to work. After doing so, my min and max values range from 3e-38 and 3e+38 respectively, so I could not normalize it.

我试过了,np.nan_to_num()但这似乎不起作用。这样做之后,我的最小值和最大值的范围分别为 3e-38 和 3e+38,因此我无法对其进行标准化。

Is there any way to scale this data down? If not, how should I deal with this?

有没有办法缩小这些数据?如果没有,我该如何处理?

Thank you.

谢谢你。

EDIT:

编辑:

Some context. I'm working on a malware classification problem. My dataset consists of live malware binaries. They are files of the type .exe, .apk etc. My idea is store these binaries as a numpy array, convert to a grayscale image and then perform pattern analysis on it.

一些上下文。我正在处理恶意软件分类问题。我的数据集由实时恶意软件二进制文件组成。它们是 .exe、.apk 等类型的文件。我的想法是将这些二进制文件存储为一个 numpy 数组,转换为灰度图像,然后对其进行模式分析。

回答by John1024

If you want to make an image out of a binary file, you need to read it in as integer, not float. Currently, the most common format for images is unsigned 8-bit integers.

如果要从二进制文件中制作图像,则需要将其作为整数读入,而不是浮点数。目前,最常见的图像格式是无符号 8 位整数。

As an example, let's make an image out of the first 10,000 bytes of /bin/bash:

例如,让我们用 /bin/bash 的前 10,000 个字节制作一个图像:

>>> import numpy as np
>>> import cv2
>>> xbash = np.fromfile('/bin/bash', dtype='uint8')
>>> xbash.shape
(1086744,)
>>> cv2.imwrite('bash1.png', xbash[:10000].reshape(100,100))

In the above, we used the OpenCV library to write the integers to a PNG file. Any of several other imaging libraries could have been used.

在上面,我们使用 OpenCV 库将整数写入 PNG 文件。可以使用其他几个成像库中的任何一个。

This what the first 10,000 bytes of bash"looks" like:

这是bash“看起来”的前 10,000 个字节的样子:

enter image description here

在此处输入图片说明

回答by Sayali Sonawane

EDIT 2

编辑 2

Numpy integer nan
Accepted answer states:NaNcan't be stored in an integer array. A nanis a special value for float arrays only. There are talks about introducing a special bit that would allow non-float arrays to store what in practice would correspond to a nan, but so far (2012/10), it's only talks. In the meantime, you may want to consider the numpy.mapackage: instead of picking an invalid integer like -99999, you could use the special numpy.ma.maskedvalue to represent an invalid value.

Numpy integer nan
接受的答案状态:NaN不能存储在整数数组中。Anan用于浮点数组的特殊值。有关于引入一个特殊位的讨论,该位将允许非浮点数组存储实际上对应于 a 的内容 nan,但到目前为止(2012/10),这只是讨论。同时,您可能需要考虑 numpy.ma包:您可以使用特殊numpy.ma.masked值来表示无效值,而不是选择像 -99999 这样的无效整数。

a = np.ma.array([1,2,3,4,5], dtype=int)
a[1] = np.ma.masked
masked_array(data = [1 -- 3 4 5],
             mask = [False  True False False False],
       fill_value = 999999)

EDIT 1

编辑 1

To read binary file:

读取二进制文件:

  1. Read the binary file content like this:

    with open(fileName, mode='rb') as file: # b is important -> binary
        fileContent = file.read()
    

    After that you can "unpack" binary data using struct.unpack

  2. If you are using np.fromfile()function:

    numpy.fromfile, which can read data from both text and binary files. You would first construct a data type, which represents your file format, using numpy.dtype, and then read this type from file using numpy.fromfile.

  1. 像这样读取二进制文件内容:

    with open(fileName, mode='rb') as file: # b is important -> binary
        fileContent = file.read()
    

    之后,您可以使用struct.unpack“解包”二进制数据

  2. 如果您正在使用np.fromfile()功能:

    numpy.fromfile,它可以从文本和二进制文件中读取数据。您将首先使用 构造一个数据类型,它表示您的文件格式, numpy.dtype然后使用numpy.fromfile.