np.vectorize
使用and后我遇到了数组问题np.meshgrid
以下是我的终端的结果
我是怎么得到的
def test_func(x, y):
"""
some calc here:
arr = np.linspace(1,100, num=y)
res = another_func(x, arr)
return np.sum(res, axis=-1)
"""
return # (2,2)-np.ndarray
X = np.array([1, 2, 3, 4])
Y = np.array([1, 2, 3])
X, Y = np.meshgrid(X, Y)
# res = test_func(X, Y) ---> TypeError: only size-1 arrays can be converted to Python scalars
func = np.vectorize(test_func, otypes=[object])
res = func(X, Y)
会发生什么
用 (x, y) 调用函数后得到一个 (2, 2)-array
在用一系列 (x, y) 调用函数后得到一个多维-array
切片结果,结合 Array
3d-plot 中的第一个元素(x, y, 结果)
res = \
[[array([[1, 2],
[3, 4]]),
array([[11, 12],
[13, 14]]),
array([[111, 122],
[133, 144]]),
array([[1111, 1222],
[1333, 1444]])],
[array([[1, 2],
[3, 4]]),
array([[11, 12],
[13, 14]]),
array([[111, 122],
[133, 144]]),
array([[1111, 1222],
[1333, 1444]])],
[array([[1, 2],
[3, 4]]),
array([[11, 12],
[13, 14]]),
array([[111, 122],
[133, 144]]),
array([[1111, 1222],
[1333, 1444]])]]
type <class 'numpy.ndarray'>
shape (3, 4)
切片后得到
res =
[[1 11 11 1111],
[1 11 11 1111],
[1 11 11 1111]]
然后plot_3d(X,Y,res)
问题
为什么类型res
是数组?它看起来像一个数组列表。
我试过用
np.array(res)
->什么都不改变,和图一样,shape(3, 4)
np.array(res.tolst())
-> 形状为 (3, 4, 2, 2) 的 np.ndarray