尺寸似乎正常,但它仍然提示(血压升高):
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)
points = np.concatenate((pointsx.T,pointsy.T), axis=1)
最佳答案
可能有两种解决方案:
(1) 使用 reshape() 改变一维向量 在这里,如果 pointx 和 pointy 是一维向量,并转置它而不是使用 .T (适用于更高维度)
import numpy as np
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)
points = np.concatenate((pointsx.reshape(-1,1),pointsy.reshape(-1,1)), axis=1)
print(points)
假设如果pointsx = [1,2,3,4],那么pointsx.reshape(-1,1)会将其转换为
[[1]
[2]
[3]
[4]
[9]]
(2) 将一维向量转换为矩阵,然后使用转置。
import numpy as np
x = np.array([1,2,3,4])
y = np.array([5,6,7,8])
m = np.array([9,10])
pointsx = np.concatenate((x,[m[0]]), axis=0)
pointsy = np.concatenate((y,[m[1]]), axis=0)
points = np.concatenate((np.matrix(pointsx).T,np.matrix(pointsy).T), axis=1)
print(points)
关于python - Numpy 连接给出错误 : axis 1 is out of bounds for array of dimension 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66609605/