Многомерный массив NumPy от str - PullRequest
0 голосов
/ 22 сентября 2019

Я бы хотел извлечь уже сохраненные np.ndarray s различных форм из string, но я получил дополнительное измерение 1 на самом нижнем уровне, и я не знаю, откуда оно и как его получить.избавиться от него.

# should get the shape (3, 3, 3, 3)
read_out = '[[[[0.5562924070613091, 0.6502387025560746, 0.8589945193141699], [0.745434087628389, 0.2839537874351449, 0.03809252102495819], [0.7924138563598314, 0.9169053450309742, 0.853874481956428]], [[0.7932366987537574, 0.9270341358787771, 0.7979137828792163], [0.21169537076434064, 0.9047995195143969, 0.7610965829951569], [0.2273292430731535, 0.025055546411747343, 0.10895968663978628]], [[0.7485314203406056, 0.5235002475356629, 0.09844971386485046], [0.47273757152689344, 0.5570205245706452, 0.19063043491178255], [0.10860384056609551, 0.7360276804783111, 0.036808683985101176]]], [[[0.30514473426478783, 0.6878130899033149, 0.6529575274469709], [0.03473941277854564, 0.2533269436893407, 0.7462227773617784], [0.9635242478303033, 0.13703666091728384, 0.4846497201696739]], [[0.4106180318282153, 0.20194661946359826, 0.7042115597349452], [0.17334848666705216, 0.37601758801076246, 0.7587396782070276], [0.3936434771371614, 0.8051518371265323, 0.6243391449870649]], [[0.5486720345569843, 0.9255395054950093, 0.3557838522147303], [0.4125915757789903, 0.10150055989884654, 0.8842070836684554], [0.18532428454853844, 0.5649926167552375, 0.017550792110417657]]], [[[0.3733816735426635, 0.44431764632812853, 0.41161271030235547], [0.8123177774092832, 0.08276180938089117, 0.06568208066586567], [0.9537713940020132, 0.7900356085602556, 0.6091867878104841]], [[0.4230710409151044, 0.6932683271182497, 0.7611846255447452], [0.34562761722203394, 0.2993339196500324, 0.5635093087612782], [0.012160932003125069, 0.956902853234382, 0.48372665810371474]], [[0.11894027819209363, 0.06303587824747203, 0.541374501254322], [0.566663083134262, 0.3257259084253359, 0.6815276852985156], [0.6919355500261126, 0.6506718477193915, 0.28767181538458997]]]]'

def str_to_np (strlist, number = 0, typ = np.float128) :
    if '[' == strlist [0] and ']' == strlist [-1] : # the str begins and ends with brackets
        return str_to_np (strlist [ 1 : -1 ], number + 1)
    elif not strlist.count ('[') and not strlist.count (']') : # one-dimensional array
        return np.fromstring (strlist, dtype = typ, sep = ',' ), number
    else : # multi-dimensional array; actually works only with two-dimensional arrays.
        splitter = number * ']' + ', ' + number * '[' # split at the separation area of betwen the outest list elements
        part_list = strlist.split (splitter)
        md_array = []
        for part in part_list : # put them together as a list of str
            md_array.append ( str_to_np (part, number - 1) [0] )
        return np.array (md_array, dtype = typ), number

if '[' == read_out [0] and ']' == read_out [-1] :
    floats, depth = str_to_np (read_out) # should only have floats as entries
    ints, depth = str_to_np (read_out, typ = np.int64) # should only have ints as entries
    if ints.all () == floats.all () and not ints.all () == np.zeros (floats.size).all () :
        read_out = ints [0]
    else :
        read_out = floats [0]
...