首页 > 解决方案 > 无法将大小为 470 的数组重塑为形状 (20)

问题描述

在陈述问题之前,让我明确一点,我是 Python 和 Pycharm 的新手。实际上,我在安装了 python 3.7.1 的 Pycharm 上运行来自 GitHub 的代码。我无法理解错误。

关于这个错误有很多相关的问题,但我没有得到所需的解决方案。人们有不同的参数。他们在数组整形括号中有某种整数。我也没有得到什么是“reshape_size”。

    for fname in filelist_in_order:
        f = open(fname, 'rb')
        complete_array_part = pickle.load(f)
        complete_array_part = complete_array_part.reshape(-1, reshape_size)
        if (first_access):
            complete_array = complete_array_part
            first_access = False
        else:
            complete_array = np.concatenate((complete_array, complete_array_part), axis=0)
    return complete_array

错误信息是:

complete_array_part = complete_array_part.reshape(-1, reshape_size)

ValueError:无法将大小为 470 的数组重新整形为形状 (20)

所以当我检查 Complete_Array_Part 时的输出是:

[ 0.17789184 0.30629522 0.27276194 0.17626782 -0.37863299 -0.25997388 -0.06388663 -0.12540221 -0.14847486 -0.34351087 0.09123761 0.29326397 0.28769037 0.18113655 -0.282704 -0.32993543 -0.09362718 -0.0762426 -0.13316527 -0.31239721 0.0888922 0.42159474 0.26748142 0.21263877 -0.35531974 -0.25320625 0.01957267 -0.08911581 0.02139289 -0.35609692 -0.02162258 0.27158457 0.24833584 0.22414273 -0.25294834 -0.25598195 -0.00261908 -0.16378632 -0.16722032 -0.28330618 0.11813667 0.4059473 0.20328876 0.19888923 -0.17746535 -0.24519044 -0.06206651 -0.1454512 -0.147276 -0.25637549 0.01985414 0.2562502 0.25700885 0.22300856 -0.26829335 -0.3002809 -0.05610409 -0.14334358 -0.13960308 -0.25650957 0.04738852 0.30026013 0.17591953 0.214241 -0.19861142 -0.33769739 0.00736059 -0.07837114 -0.19286683 -0.25786099 0。09123761 0.29326397 0.28769037 0.18113655 -0.282704 -0.32993543 -0.09362718 -0.0762426 -0.13316527 -0.31239721 0.1030243 0.30424696 0.22326529 0.17811422 -0.22068222 -0.27857596 0.00819118 -0.1030729 -0.10017776 -0.19125859 0.10184187 0.34402201 0.16981423 0.22493245 -0.26154083 -0.35094687 -0.11193486 -0.10435168 -0.11710036 -0.2646451 0.18112525 0.16479042 0.20678186 0.208013 -0.33933938 -0.39654118 0.10261163 -0.05978006 -0.09965867 -0.24144523 -0.03096133 0.25541702 0.264617 0.18827559 -0.27278233 -0.27280146 -0.01961248 0.04128763 -0.16926275 -0.25017616 0.1685964 0.2472322 0.16320953 0.11125059 -0.33302104 -0.32924467 0.06027375 0.01118627 -0.12375752 -0.36029184 0.05984636 0.40982607 0.29108658 0.24611495 -0.32725531 -0.29316714 -0.00595155 -0.16829109 -0.01524433 -0.31738156 0。14332619 0.37219125 0.35616517 0.07771102 -0.41376531 -0.29962835 -0.08480088 -0.12293065 0.04588581 -0.37282506 0.26338899 0.18212023 0.30509233 0.03429261 -0.46090066 -0.62543684 0.14560741 -0.23207924 -0.10377936 -0.34899354 0.10678266 0.31017959 0.29039884 0.18984702 -0.30641529 -0.37125492 0.00190322 -0.090801 0.00383001 -0.31131977 0.11976989 0.24804372 0.1798858 0.20221336 -0.2672568 -0.27686304 0.09393801 -0.08291434 -0.15147643 -0.26258913 0.07006255 0.24292895 0.2479758 0.12545972 -0.28571904 -0.2246163 0.02192843 -0.09310064 -0.19140819 -0.3822245 0.15050775 0.24107212 0.31406438 0.07037568 -0.28054947 -0.30401161 -0.07911987 0.02704167 -0.03337537 -0.35185724 0.08345325 0.45238137 0.24365583 0.13630277 -0.26385203 -0.27017274 -0.0053592 - 0.16803598 -0.13584027 -0.29801774 0。06169732 0.28122491 0.20148738 0.12553374 -0.32540709 -0.24335477 -0.03755248 -0.00100566 -0.0509242 -0.33147877 0.07427905 0.18317398 0.35396093 0.18327162 -0.31448454 -0.38967571 -0.02551728 -0.23432273 -0.16113353 -0.28115082 0.06879958 0.22342694 0.17293574 0.14878762 -0.34089816 -0.35571763 -0.11643556 -0.09598652 -0.00672829 -0.27351999 0.06069776 0.17189354 0.22681117 0.16899896 -0.32868099 -0.37247849 -0.1136125 -0.15183234 -0.17877081 -0.35204101 0.24152195 0.24887547 0.32604483 0.25527418 -0.35900906 -0.40607622 -0.04806738 -0.20694411 -0.05488034 -0.26493907 0.17528442 0.30049577 0.1629622 0.20871069 -0.22320881 -0.36587471 0.20252028 -0.14161371 -0.1282679 -0.24894838 0.0888922 0.42159474 0.26748142 0.21263877 - 0.35531974 -0.25320625 0.01957267 -0.08911581 0.02139289 -0.35609692 0.12388916 0.33917072 0.42795435 0.08663616 -0.3915118 -0.43263063 -0.01308431 -0.09523527 -0.08210509 -0.39892739 0.1860335 0.22147226 0.23963733 0.12970485 -0.32032195 -0.36106503 0.02424989 -0.07740933 -0.10642112 -0.30477303 0.12551595 0.30433181 0.35763353 0.28385532 -0.43477935 -0.34082446 -0.02719706 -0.44719821 0.27575466 -0.28147447 0.23355711 0.32526308 0.41331318 0.2257646 -0.40978959 -0.45557061 0.04251576 -0.07252584 -0.12531641 -0.31281373 0.18305443 0.1704032 0.24024118 0.16669753 -0.27432877 -0.38038296 0.09402332 -0.06208001 -0.18470117 -0.2516017 0.06363131 0.20514129 0.22846916 0.08167504 -0.25951183 -0.32592475 0.01168576 -0.12991063 -0.10443141 -0.26863056 0.25425208 0.31902471 0.33302572 0.22676007 -0.33653393 -0.38779891 - 0.01722381 -0.1111242 -0.22871622 -0.3331289 0。11495201 0.41839725 0.19331557 0.20344175 -0.2456654 -0.20443794 0.00504544 -0.2100333 -0.08358113 -0.33943006 0.26854891 0.30015546 0.31847724 0.18569888 -0.31109962 -0.41813236 0.03507741 -0.02907968 -0.20126076 -0.32520163 0.07898337 0.33653653 0.34216624 0.24134663 -0.29218262 -0.32460195 -0.08944514 -0.09410556 -0.01705393 -0.40615028 0.10629132 0.2604306 0.2255978 0.04821964 -0.26977044 -0.38201541 -0.06466427 -0.19278997 -0.09640036 -0.21310115 0.01773387 0.22970651 0.31858417 0.21676925 -0.23561591 -0.41310543 0.12385868 -0.14431895 -0.1570266 -0.29954869 0.0915345 0.25607604 0.23845172 0.21028796 -0.32377386 -0.33092183 -0.00443996 -0.24734242 -0.17844367 -0.2985107 0.14641258 0.33784047 0.17312077 0.20297053 -0.17508766 - 0.2666209 0.1487464 -0.08262923 -0.07993621 -0.3536256 0。27124339 0.17663571 0.29459208 0.14568396 -0.35805491 -0.45823082 0.02555789 -0.20574869 -0.1970185 -0.21216772 -0.02660971 0.18790659 0.28153318 0.18746425 -0.24937677 -0.30586433 -0.07034364 -0.05794065 -0.06758652 -0.33423638 0.2193345 0.31134152 0.33954287 0.16869812 -0.3541418 -0.35929483 -0.01552734 -0.01932855 -0.07188252 -0.34194604 0.09554033 0.31854007 0.3243461 0.15001382 -0.32146809 -0.29419503 -0.10843883 -0.11347267 -0.11110444 -0.34457517 0.22696011 0.20718208 0.37935093 0.06550272 -0.27321219 -0.38913769 -0.123006 0.01007091 0.09978335 -0.32427335]31134152 0.33954287 0.16869812 -0.3541418 -0.35929483 -0.01552734 -0.01932855 -0.07188252 -0.34194604 0.09554033 0.31854007 0.3243461 0.15001382 -0.32146809 -0.29419503 -0.10843883 -0.11347267 -0.11110444 -0.34457517 0.22696011 0.20718208 0.37935093 0.06550272 -0.27321219 -0.38913769 -0.123006 0.01007091 0.09978335 -0.32427335]31134152 0.33954287 0.16869812 -0.3541418 -0.35929483 -0.01552734 -0.01932855 -0.07188252 -0.34194604 0.09554033 0.31854007 0.3243461 0.15001382 -0.32146809 -0.29419503 -0.10843883 -0.11347267 -0.11110444 -0.34457517 0.22696011 0.20718208 0.37935093 0.06550272 -0.27321219 -0.38913769 -0.123006 0.01007091 0.09978335 -0.32427335]

完整的阵列零件形状 (470,)

完整阵列零件尺寸 470

重塑尺寸 20

标签: pythonnumpypycharmpickle

解决方案


这里有很多东西要解压,我们不能一个答案解决所有问题。

但也许这会有所帮助。

reshape改变数组的维度。例如,一个有 6 个元素的数组可以由一列 6 个元素、2 列 3 个元素、3 列 2 个元素、1 行 6 个元素组成。

您应该在 python 控制台中尝试一下,以熟悉 reshape:

import numpy as np #this loads the module numpy and assigns the name "np" to it

a=np.zeros(6) # create an array filled with 0s of 6 elements

print(a) # shows contents to the screen
# this outputs [ 0.  0.  0.  0.  0.  0.]

print(a.reshape((2,3)))
# this outputs
# [[ 0.  0.  0.]
#  [ 0.  0.  0.]]

print(a.reshape((3,2)))
# this outputs
# [[ 0.  0.]
# [ 0.  0.]
# [ 0.  0.]]

如您所见,该reshape函数更改了数组的维度,但没有更改其值。您还应该注意,我无法将其重新调整为 2x4 数组,因为该数组的元素数量与起始数组不同。

在你理解了这一点之后,试着看看你的程序为什么会失败......


推荐阅读