Convert 4D array of floats from txt (string) file to numpy array of floats

独自空忆成欢 提交于 2021-02-07 08:54:39

问题


I have a txt (string) file looking like:

[[[[0.17262284, 0.95086717, 0.01172171, 0.79262904],
     [0.52454078, 0.16740103, 0.32694925, 0.78921072],
     [0.77886716, 0.35550488, 0.89272706, 0.36761104]],
    [[0.14336841, 0.94488079, 0.83388505, 0.02065268],
     [0.31804594, 0.22056339, 0.84088501, 0.94994676],
     [0.57845057, 0.12645735, 0.12628646, 0.05526736]]]]

The shape is (1,2,3,4). How can I easily convert it to a NumPy array that considers the data type and the shape (that may be deduced from the parenthesis)? Let's say I have the shape in the txt file, will it make it easier? I have tried using np.loadtxt, seems that it doesn't handle this 4d shape, it loads as a 1D array. I can parse the shape myself and then convert all members to floats but I believe there is np function that already does that. The file is not created by me - I would just pkl the array.


回答1:


The text string containing the array can be evaluated ('read') using the ast.literal_eval() function, then wrapped in a numpy array constructor, as:

import ast
import numpy as np


# Option 1: From a text string.
a = np.array(ast.literal_eval(txt))

# Option 2: Directly from text file.
a = np.array(ast.literal_eval(open('np_array.txt').read()))

a.shape
>>> (1, 2, 3, 4)

Setup:

txt = """[[[[0.17262284, 0.95086717, 0.01172171, 0.79262904],
[0.52454078, 0.16740103, 0.32694925, 0.78921072],
[0.77886716, 0.35550488, 0.89272706, 0.36761104]],
[[0.14336841, 0.94488079, 0.83388505, 0.02065268],
[0.31804594, 0.22056339, 0.84088501, 0.94994676],
[0.57845057, 0.12645735, 0.12628646, 0.05526736]]]]"""



回答2:


This array encoding matches the encoding scheme of json, so you can use that to load it:

import json
import numpy as np

arr = np.array(json.load(open("array.txt", "r")))

Or if the data is a string, use:

arr = np.array(json.loads(array_as_string))


来源:https://stackoverflow.com/questions/64986545/convert-4d-array-of-floats-from-txt-string-file-to-numpy-array-of-floats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!