numpy array 数据类型问题
大家好,我在一个下载的程序上做了一些修改,其中有个问题我print了某个变量的类型,维度及其值
print(type(A))
print(A.shape)
print(A)
结果如下:
原代码中数据的类型是
<class 'numpy.ndarray'> (3,)
[-1.48029737e-17 -1.48029737e-17 -3.08148791e-33]
而我传入的数据类型是
<class 'numpy.ndarray'> (3, 1)
[[-4.44089210e-17]
[-1.85037171e-17]
[ 1.11022302e-17]]
请问怎么把<class 'numpy.ndarray'> (3, 1) 类型转换成<class 'numpy.ndarray'> (3,)
我用你的代码:import numpy as np
A = np.array([-1.48029737e-17, -1.48029737e-17, -3.08148791e-33])
print(type(A))
print(A.shape)
print(A)结果:<class 'numpy.ndarray'>
(3,)
[-1.48029737e-17 -1.48029737e-17 -3.08148791e-33](3,) 不是你想要的吗?
楼楼,Python numpy 的数组 (3, 1) 就是 (3, )不需要更改~
本帖最后由 lqhenwunai 于 2021-9-18 13:22 编辑
谢谢大家的回复!!!
如果不嫌长,我把相关代码贴一下
for i in range(1):
# find the nearest neighbors between the current source and destination points
distances, indices = nearest_neighbor(src[:dim,:].T, dst[:dim,:].T)
# compute the transformation between the current source and nearest destination points
Transformation_Mat , Rotation, translation = best_fit_transform(src[:dim,:].T, dst[:dim,indices].T)
# update the current source
src = np.dot(Transformation_Mat, src)
# check error
mean_error = np.mean(distances)
if np.abs(prev_error - mean_error) < thresh:
break
prev_error = mean_error
# calculate final transformation
Transformation_Mat,Rotation,translation = best_fit_transform(chull1, src[:dim,:].T)
这里我只做一次循环,调用best_fit_transform 函数,并且在循环外再调用一次。
而在best_fit_transform函数中,我有这么一段话:
tmp=np.dot(R,centroid_A.T)
tmp=np.array(tmp)
t = centroid_B.T - tmp
print("check t...")
print(type(tmp),tmp.shape)
print(centroid_B.T)
print(np.dot(R,centroid_A.T))
print(t)
print("..................")
# homogeneous transformation
T = np.identity(dim+1)
T[:dim, :dim] = R
T[:dim, dim] = t
不懂为什么,两次打印出来的tmp就如一楼所显示的一样,而这造成的后果就是得到的t第一次是1×3的行向量,第二次就是个3*3的矩阵了。然后后面对T赋值就报错了。
页:
[1]