Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:块和形状必须具有相同的长度/尺寸_Python_Numpy_Machine Learning_Dask - Fatal编程技术网

Python ValueError:块和形状必须具有相同的长度/尺寸

Python ValueError:块和形状必须具有相同的长度/尺寸,python,numpy,machine-learning,dask,Python,Numpy,Machine Learning,Dask,我读了一本书“介绍数据科学、大数据、机器学习等,使用Python工具” 第4章中有一个关于分块矩阵计算的代码: import dask.array as da import bcolz as bc import numpy as np import dask n = 1e4 #A ar = bc.carray(np.arange(n).reshape(n/2,2) , dtype='float64', rootdir = 'ar.bcolz', mode = 'w') #B y

我读了一本书“介绍数据科学、大数据、机器学习等,使用Python工具” 第4章中有一个关于分块矩阵计算的代码:

import dask.array as da
import bcolz as bc
import numpy as np
import dask

n = 1e4 #A

ar = bc.carray(np.arange(n).reshape(n/2,2)  , dtype='float64', rootdir     = 'ar.bcolz', mode = 'w') #B
y  = bc.carray(np.arange(n/2), dtype='float64', rootdir = 'yy.bcolz', mode = 'w') #B,

dax = da.from_array(ar, chunks=(5,5)) #C
dy = da.from_array(y,chunks=(5,5)) #C

XTX = dax.T.dot(dax) #D
Xy  = dax.T.dot(dy) #E

coefficients = np.linalg.inv(XTX.compute()).dot(Xy.compute()) #F

coef = da.from_array(coefficients,chunks=(5,5)) #G

ar.flush() #H
y.flush() #H

predictions = dax.dot(coef).compute() #I
print (predictions)
我得到一个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-4-7ae8e9cf2346> in <module>()
     10 
     11 dax = da.from_array(ar, chunks=(5,5)) #C
---> 12 dy = da.from_array(y,chunks=(5,5)) #C
     13 
     14 XTX = dax.T.dot(dax) #D

C:\Users\F\Anaconda3\lib\site-packages\dask\array\core.py in from_array(x, chunks, name, lock, fancy, getitem)
   1868     >>> a = da.from_array(x, chunks=(1000, 1000), lock=True)  # doctest: +SKIP
   1869     """
-> 1870     chunks = normalize_chunks(chunks, x.shape)
   1871     if len(chunks) != len(x.shape):
   1872         raise ValueError("Input array has %d dimensions but the supplied "

C:\Users\F\Anaconda3\lib\site-packages\dask\array\core.py in normalize_chunks(chunks, shape)
   1815             raise ValueError(
   1816                 "Chunks and shape must be of the same length/dimension. "
-> 1817                 "Got chunks=%s, shape=%s" % (chunks, shape))
   1818 
   1819     if shape is not None:

ValueError: Chunks and shape must be of the same length/dimension. Got chunks=(5, 5), shape=(5000,)
ValueError回溯(最近一次调用)
在()
10
11 dax=da.from_数组(ar,chunks=(5,5))#C
--->12 dy=da.从数组(y,chunks=(5,5))#C
13
14 XTX=dax.T.dot(dax)#D
C:\Users\F\Anaconda3\lib\site packages\dask\array\core.py from_array(x,chunks,name,lock,fancy,getitem)
1868>>a=da.from_数组(x,chunks=(10001000),lock=True)#doctest:+SKIP
1869     """
->1870块=标准化_块(块,x.shape)
1871如果len(块)!=len(x.shape):
1872 raise VALUETERROR(“输入数组有%d个维度,但提供的是”
C:\Users\F\Anaconda3\lib\site packages\dask\array\core.py中的normalize_块(块,形状)
1815升值错误(
1816“块和形状必须具有相同的长度/尺寸。”
->1817“获取块=%s,形状=%s”%(块,形状))
1818
1819如果形状不是无:
ValueError:块和形状必须具有相同的长度/维度。获取的块=(5,5),形状=(5000,)
问题是什么?

问题在于:

np.arange(n/2).reshape(n)
您创建一个大小为
n/2
的数组,然后尝试将其
重塑为大小
n
。不能使用
重塑
更改大小

这可能是一个复制/粘贴错误?它不在您的原始代码中,而且似乎您正在这样做

np.arange(n).reshape(n/2,2)

在其他地方,只要
n
是偶数,它就可以工作(小心,如果
n
不是偶数,这也会失败。)

数组
y
只有一个维度,但在
chunks=
参数中给出两个数字。