3

该函数numpy.array_repr可用于创建 NumPy 数组的字符串表示形式。如何将 NumPy 数组的字符串表示形式转换为 NumPy 数组?

假设字符串表示如下:

array([-0.00470366,  0.00253503,  0.00306358, -0.00354276,  0.00743946,
       -0.00313205,  0.00318478,  0.0074185 , -0.00312317,  0.00127158,
        0.00249559,  0.00140165,  0.00053142, -0.00685036,  0.01367841,
       -0.0024475 ,  0.00120164, -0.00665447,  0.00145064,  0.00128595,
       -0.00094848,  0.0028348 , -0.01571732, -0.00150459,  0.00502642,
       -0.00259262,  0.00222584,  0.00431143, -0.00379282,  0.00630756,
        0.001324  , -0.00420992, -0.00808643,  0.00180546,  0.00586163,
        0.00177767, -0.0011724 , -0.00270304,  0.00505948,  0.00627092,
       -0.00496326,  0.00460142, -0.00177408, -0.00066973,  0.00226059,
        0.00501507, -0.00261056, -0.00617777,  0.00269939, -0.01023268,
        0.00338639,  0.00483614,  0.00086805,  0.00041314, -0.0099909 ,
        0.00356182, -0.00788026,  0.00245763,  0.00371736,  0.00343493,
       -0.00037843, -0.0013632 , -0.00210518,  0.00362144,  0.00061659,
       -0.0008905 , -0.01148648, -0.00292173, -0.00206425,  0.00606295,
        0.0041656 , -0.00407792,  0.00026893,  0.00078469,  0.00186181,
        0.00067565, -0.00811732,  0.00257632,  0.00177333, -0.00602056,
        0.00853466,  0.0016037 ,  0.00094006, -0.00018953, -0.00408413,
       -0.00994886,  0.01268128,  0.0080336 ,  0.00546633,  0.00372206,
        0.00228082,  0.00445107,  0.00236268,  0.01059031, -0.00106609,
       -0.00055983,  0.00371333,  0.0004037 ,  0.00632817,  0.00145055], dtype=float32)

如何将其转换为 NumPy 数组?

4

2 回答 2

6

eval可能是最简单的。它评估给定的字符串,就好像它是代码一样。

from numpy import array, all
arr_1 = array([1,2,3])
arr_string = repr(arr_1)
arr_2 = eval(arr_string)

all(arr_1 == arr_2) # True

另见文档evalhttps ://docs.python.org/2/library/functions.html#eval

于 2016-03-02T15:04:12.517 回答
4

print我经常用语句调试。要将控制台的 numpy 输出读回 python 环境,我使用以下基于np.matrix.

def string_to_numpy(text, dtype=None):
    """
    Convert text into 1D or 2D arrays using np.matrix().
    The result is returned as an np.ndarray.
    """
    import re
    text = text.strip()
    # Using a regexp, decide whether the array is flat or not.
    # The following matches either: "[1 2 3]" or "1 2 3"
    is_flat = bool(re.match(r"^(\[[^\[].+[^\]]\]|[^\[].+[^\]])$",
                            text, flags=re.S))
    # Replace newline characters with semicolons.
    text = text.replace("]\n", "];")
    # Prepare the result.
    result = np.asarray(np.matrix(text, dtype=dtype))
    return result.flatten() if is_flat else result

这是我经常申请调试的工作流程:

1)在我的代码中的某个地方......

import numpy as np
x = np.random.random((3,5)).round(decimals=2)
print(x)
  1. 这会将数组的内容打印到控制台上,例如:
    [[0.24 0.68 0.57 0.37 0.83]
     [0.76 0.5  0.46 0.49 0.95]
     [0.39 0.37 0.48 0.69 0.25]]
  1. 为了进一步检查输出,我选择文本并将其粘贴到 ipython 会话中,如下所示:
    In [9]: s2n = string_to_numpy # Short alias

    In [10]: x = s2n("""[[0.24 0.68 0.57 0.37 0.83]
                         [0.76 0.5  0.46 0.49 0.95]
                         [0.39 0.37 0.48 0.69 0.25]]""")
    In [11]: x.shape
    Out[11]: (3, 5)

    In [12]: x.mean(axis=1)
    Out[12]: array([0.538, 0.632, 0.436])
    
    ...
于 2020-01-20T16:11:24.573 回答