Python 如何解决;只有整数标量数组才能转换为标量索引;
我得到一个奇怪的错误,代码如下:Python 如何解决;只有整数标量数组才能转换为标量索引;,python,Python,我得到一个奇怪的错误,代码如下: import numpy as np w=np.random.rand(100,10) x=np.random.rand(200,100) y= np.random.rand(200,10) def compute( x, y): numpy_x = np.array(x) numpy_y = np.array(y) zeross = np.zeros(np.shape(x)[0]) oness = np.ones(np.s
import numpy as np
w=np.random.rand(100,10)
x=np.random.rand(200,100)
y= np.random.rand(200,10)
def compute( x, y):
numpy_x = np.array(x)
numpy_y = np.array(y)
zeross = np.zeros(np.shape(x)[0])
oness = np.ones(np.shape(x)[0])
result= np.zeros((np.shape(x)[0],np.shape(y)[1]))
for i in range(np.shape(x)[1]):
for j in range(np.shape(y)[1]):
result[i,j] = w[i,j]+np.max(zeross, np.subtract(oness,w[:,j].dot(np.transpose(numpy_x)))).dot(numpy_x[:,i]*(numpy_y[:,j]*-1))
return result
print(compute(x,y))
这里x
是100乘100
矩阵,y
是100乘5
矩阵w
是100乘5
矩阵
我得到的错误是:
测试失败:只能将整数标量数组转换为标量索引
编译器似乎指示错误在库中
37 return result
38
---> 39 print(compute(x,y))
2 frames
<ipython-input-36-8b5bccb531cc> in compute(x, y)
33 for i in range(np.shape(x)[1]):
34 for j in range(np.shape(y)[1]):
---> 35 result[i,j] = w[i,j]+np.max(zeross, np.subtract(oness,w[:,j].dot(np.transpose(numpy_x)))).dot(numpy_x[:,i]*(numpy_y[:,j]*-1))
36
37 return result
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial)
2503 """
2504 return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims,
-> 2505 initial=initial)
2506
2507
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
84 return reduction(axis=axis, out=out, **passkwargs)
85
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
87
88
37返回结果
38
--->39打印(计算(x,y))
2帧
在计算中(x,y)
33对于范围内的i(np.形状(x)[1]):
34对于范围内的j(np.形状(y)[1]):
--->35结果[i,j]=w[i,j]+np.max(zeross,np.subtract(oness,w[:,j)]).dot(np.transpose(numpy_x[:,i]*(numpy_y[:,j]*-1)
36
37返回结果
/amax中的usr/local/lib/python3.6/dist-packages/numpy/core/fromneric.py(a、axis、out、keepdims、initial)
2503 """
2504返回(a,np.最大值,'max',轴,无,out,keepdims=keepdims,
->2505首字母=首字母)
2506
2507
/usr/local/lib/python3.6/dist-packages/numpy/core/fromneric.py in_包装制作(obj,ufunc,method,axis,dtype,out,**kwargs)
84返回减少(轴=轴,输出=输出,**passkwargs)
85
--->86返回ufunc.reduce(对象、轴、数据类型、输出、**密码)
87
88
请帮助将来可能的副本,请不要包括异常的图像。粘贴整个异常-图像中也没有显示异常的中间部分。好的,我不知道我在哪里放置了一个数组作为另一个数组的索引?@user42493我不确定这是否为您提供了预期的计算结果,但如果您正在尝试的话ng若要在两个数组之间进行元素最大化,则应使用
np.max
而不是np.max
。np.max
的第二个参数是轴
参数,因此它试图将给定的数组转换为索引。