加载npz文件并遍历所有数组

时间:2018-09-19 15:43:04

标签: python-3.x numpy

我有一个压缩的.npz,我按如下方式加载:

results=np.load('/homes/`enter code here`scores060000.npz', encoding='bytes')

但是,我无法遍历文件以逐个数组访问

我尝试了什么?

results.files()

['分数']

无论何时我做

results['scores'][0] # to get the first array

*** IndexError:数组的索引过多

results['scores'].keys()

*** AttributeError:'numpy.ndarray'对象没有属性'keys'

这里是文件样本

'/ file4 / 4.png':array([-1.651477,-1.907163,-3.1426308,3.8437457,-1.6413991,         3.9418564,-2.097484,-4.1060033,-5.8090167,-0.6079367,        -1.1268529,-1.6963539,-0.76549244,-1.3052248、2.2658308,        -1.247977,7.906589,4.1645093,-1.2772448,2.9711807,        -0.68575585,-4.08245,-4.802993,-1.2243137,-3.3159957,        -5.2699194、0.8371297、0.80230814,-5.1045756,-2.7776072,        -2.5447376,-3.2206695,-1.6834608、0.03849944,-0.24658614,         0.84565806、0.8182453,-0.79316556、1.1238738,-2.5781393,        -1.7877787,-7.7516017,-0.0954666,-0.2568078,-5.9535336,         4.600506,-0.6839486、1.101945,-4.871244、0.45752206,        -1.5437831,0.39374897,-0.5242709,-4.308458,8.083314,         3.428661,-4.207958、8.915849,-0.86354035,-0.8921649,        -3.0371819,-1.4496443,-2.869159,-2.0725305,-3.3047957,        -2.6443558,-1.6881287,-2.1690493,-1.6784476,-3.871248,         1.2377933,-4.456587,-7.346478、1.8070494,-1.5207015,         2.2107837,5.3954268,0.44746792,-2.7442515,-5.163432,         0.25762084,-2.7383866,-3.5220423,-1.6739469,-5.2574334,         0.19439769,-0.66933227,-3.4044476、1.0006926、8.179258,        -0.3292276,-2.4944222、0.6352347,-4.9328766,-3.6732411,        -4.645098,-3.7336316,-4.650411,-1.1193942,-2.4021673,         9.60228],dtype = float32),'/file3/3.png':array([-4.950835,-4.869176,-1.6227134,-0.83657277,-3.1601653,         0.8242829、1.6684622,-3.7665265、2.0375075,-6.6443377,         1.5364946,-1.1193827,-3.0785651,-5.0486474,-1.0922289,         2.1312854,-2.9272187,0.9454415,-0.13194042,-0.3385026,        -4.4860635,-0.80675066,5.478806,-1.9646504,-1.3469064,        -0.98173124,-6.078044,-0.6684527、4.5297656、2.5620239,        12.816556、2.2342243,-1.8920712,-2.387616、0.77336925,         1.0052404,-2.7040641、5.7424645,-1.1701487,-1.2083752,         2.7204783,-5.1847005,-3.7228272,-0.22128446、1.5531254,        -4.013649,-1.1326185,-2.685383,-1.6641728,-0.36011285,        -0.7170791,-1.417088,-2.8416286,-1.0756694,2.9484415,        -3.2145221、0.18724221,-4.410981,-0.20475249,-4.284647,        -0.0297867,2.5260768,0.8918827,-1.1881785,-3.121708,        -5.6494484,-2.044363,-4.5841136,-4.6920342,-2.0244665,        -1.060093,-0.6903727,-0.720652,-4.2343864,-1.9107593,         6.3486543,-0.15267012,-1.0452555、0.9056848,-1.1271408,         4.928084、2.3845124,-0.28718042、0.53633213、6.3739076,        -2.0438914,1.20196,1.1123928,-4.3619947,-1.1515617,         0.18415602,-0.06180171,-0.20822835,-3.0902917,-3.1353855,         1.4377578,-0.8310707,-2.603929,2.903148,-0.6100564,        -1.8161871],dtype = float32),'/file2/2.png':数组([-2.8043437e-01,-6.2239873e-01,-2.0357392e + 00,4.9808624e-01,        -1.7960895e + 00,-1.8575675e + 00,-1.2165427e + 00,-3.6179656e-01,        -4.3200240e + 00,-2.4468222e + 00、1.3260385e + 00,-2.7656777e + 00,        -2.0749898e + 00,-2.5266984e + 00、1.3110629e + 00、3.9533191e + 00,         4.8012048e-01,-2.8147533e + 00,-2.6557796e + 00,-1.4515022e + 00,        -8.9944059e-01,-9.6252346e-01、1.0777255e + 00、2.0491233e-01,        -6.6335851e-01,-3.9736774e + 00,-3.4321864e + 00、5.4393544e + 00,        -1.0569165e + 00,-2.4446402e + 00,-1.8671986e + 00,-1.6519018e + 00,         1.7538584e + 00,-2.5712433e + 00,-7.4690372e-01、4.6145740e-01,        -5.5915272e-01,-7.8732646e-01,-3.3801985e + 00,-2.5600529e + 00,        -3.5273430e + 00,-1.5424553e + 00、1.9753126e + 00、8.4598374e-01,        -1.5159348e + 00,-3.1205835e + 00、2.8564734e + 00,-1.5107436e + 00,        -3.4941616e + 00,-1.3126992e + 00,-1.8536948e-01,1.7078512e + 00,        -4.2180629e + 00,-3.2771609e + 00、8.9760866e + 00,-4.4880909e-01,        -6.3466039e + 00、2.0050712e + 00、2.1895382e + 00,-1.9819100e + 00,        -2.4119055e + 00、9.2510238e-02,-1.9337451e + 00,-1.4222538e + 00,         2.2197738e + 00,-3.6818323e + 00,-6.6456646e-02,-2.4740572e + 00,        -3.4280837e + 00,-3.3387463e + 00、1.1384126e + 00,-1.53​​73305e + 00,        -3.4580667e + 00,-2.2388813e + 00,-4.6271877e + 00,-3.0023365e + 00,         4.5120735e-03,-6.9383731e + 00、5.4922276e + 00、3.6206059e + 00,         2.6019840e + 00,-1.53​​82010e + 00,-1.5167688e + 00、5.4298296e + 00,        -7.8890306e-01,-3.5365894e + 00,-5.7465988e-01,-2.9401925e-01,        -3.0376801e + 00、3.3693345e + 00、8.3250761e + 00、5.0635014e + 00,         2.7871721e + 00,-3.2907157e + 00,-3.4527814e + 00,-4.3827205e + 00,        -6.0047526e + 00、2.0345461e-01、1.5018797e + 00、1.5115099e + 00,        -2.3368890e + 00],dtype = float32),'/file1/1.png':array([-2.880651,-3.5033705,3.7906199,-0.30735028,1.7049406,        -2.4601865,-2.432671、3.573076,-0.66925764,-4.9513187,         3.5004294,-3.1082163,-2.1569846,-4.655545,-2.6699617,        -2.0030148,-2.5035274,-2.0025814,-0.37288237,-1.2053664,        -1.1685671,0.422138,0.5124443,0.7623316,-0.8360331,        -1.1486149,-5.092014、0.78394383、1.6535784、1.4055651,         2.8729122,1.0578096,-2.6003664,-4.1960926,-2.3535116,         0.46660176,-2.432323、1.2694867,-4.972548、0.74941415,         1.3963044、3.5629773,-2.4826858,-1.5813217,-1.4925203,        -1.4314067,-1.7682784,-2.3176155,-1.53​​88411,-1.4064139,         1.3307506,-2.1563005,-4.100337,-3.9060237、0.05410841,        -1.2395332、2.4205167,-0.9115535,-2.096323,-1.5538993,        -1.7992512,0.3886638,-0.8848116,-1.7380404,-1.6939284,        -2.6846013,-0.49933237,-0.03601296,-2.0352333,-1.8516303,        -3.2491822、0.88307995、1.3174123,-2.4134717,-0.78582317,         2.5116985,-2.7801507,-1.9507023,-0.8743777、5.0220346,         4.4525557、3.0789425、3.567525、1.142694,-0.6016274,         0.8162438、0.39270648、7.590226,-2.265852、0.24908264,        -1.8506968、2.3867977,-0.35229903、0.22780067,-2.0359864,         3.5617588,-1.8114059、0.6413014、0.24592055,-1.2108788,        -1.9091825],dtype = float32)},dtype = object)

我想输出什么?

访问每个数组。例如

 '/file1/1.png': array([-2.880651 , -3.5033705 , 3.7906199 , -0.30735028, 1.7049406 , -2.4601865 , -2.432671 , 3.573076 , -0.66925764, -4.9513187 , 3.5004294 , -3.1082163 , -2.1569846 , -4.655545 , -2.6699617 , -2.0030148 , -2.5035274 , -2.0025814 , -0.37288237, -1.2053664 , -1.1685671 , 0.422138 , 0.5124443 , 0.7623316 , -0.8360331 , -1.1486149 , -5.092014 , 0.78394383, 1.6535784 , 1.4055651 , 2.8729122 , 1.0578096 , -2.6003664 , -4.1960926 , -2.3535116 , 0.46660176, -2.432323 , 1.2694867 , -4.972548 , 0.74941415, 1.3963044 , 3.5629773 , -2.4826858 , -1.5813217 , -1.4925203 , -1.4314067 , -1.7682784 , -2.3176155 , -1.5388411 , -1.4064139 , 1.3307506 , -2.1563005 , -4.100337 , -3.9060237 , 0.05410841, -1.2395332 , 2.4205167 , -0.9115535 , -2.096323 , -1.5538993 , -1.7992512 , 0.3886638 , -0.8848116 , -1.7380404 , -1.6939284 , -2.6846013 , -0.49933237, -0.03601296, -2.0352333 , -1.8516303 , -3.2491822 , 0.88307995, 1.3174123 , -2.4134717 , -0.78582317, 2.5116985 , -2.7801507 , -1.9507023 , -0.8743777 , 5.0220346 , 4.4525557 , 3.0789425 , 3.567525 , 1.142694 , -0.6016274 , 0.8162438 , 0.39270648, 7.590226 , -2.265852 , 0.24908264, -1.8506968 , 2.3867977 , -0.35229903, 0.22780067, -2.0359864 , 3.5617588 , -1.8114059 , 0.6413014 , 0.24592055, -1.2108788 , -1.9091825 ], dtype=float32)

编辑

for value,key in results['scores'].item():
    print(values)

ValueError:太多值无法解包(预期2)

但是,当我执行以下操作

for value in results['scores'].item():  
    print(values)

仅打印键

/file4/4.png
/file3/3.png
/file2/2.png

谢谢

1 个答案:

答案 0 :(得分:0)

感谢@hpaulj的提示

keys=[]
vals=[]   
for value in results['scores'].item():


       key=value
       values=results['scores'].item()[value]
       keys.append(key)
       vals.append(values)
my_dic=dict(zip(keys,vals))