鱼C论坛

 找回密码
 立即注册
查看: 1741|回复: 3

numpy array 数据类型问题

[复制链接]
发表于 2021-9-18 12:09:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大家好,我在一个下载的程序上做了一些修改,其中有个问题

我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,)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-18 12:28:43 | 显示全部楼层
我用你的代码:
  1. import numpy as np
  2. A = np.array([-1.48029737e-17, -1.48029737e-17, -3.08148791e-33])

  3. print(type(A))
  4. print(A.shape)
  5. print(A)
复制代码
结果:
  1. <class 'numpy.ndarray'>
  2. (3,)
  3. [-1.48029737e-17 -1.48029737e-17 -3.08148791e-33]
复制代码
(3,) 不是你想要的吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-18 12:35:25 | 显示全部楼层


楼楼,Python numpy 的数组 (3, 1) 就是 (3, )  不需要更改~

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-18 13:19:52 | 显示全部楼层
本帖最后由 lqhenwunai 于 2021-9-18 13:22 编辑

谢谢大家的回复!!!
如果不嫌长,我把相关代码贴一下
  1.         for i in range(1):
  2.               # find the nearest neighbors between the current source and destination points
  3.                 distances, indices = nearest_neighbor(src[:dim,:].T, dst[:dim,:].T)
  4.                 # compute the transformation between the current source and nearest destination points
  5.                 Transformation_Mat , Rotation, translation = best_fit_transform(src[:dim,:].T, dst[:dim,indices].T)

  6.                 # update the current source
  7.                 src = np.dot(Transformation_Mat, src)
  8.                 # check error
  9.                 mean_error = np.mean(distances)
  10.                 if np.abs(prev_error - mean_error) < thresh:
  11.                         break
  12.                 prev_error = mean_error

  13.         # calculate final transformation
  14.         Transformation_Mat,Rotation,translation = best_fit_transform(chull1, src[:dim,:].T)
复制代码

这里我只做一次循环,调用best_fit_transform 函数,并且在循环外再调用一次。

而在best_fit_transform函数中,我有这么一段话:
  1.         tmp=np.dot(R,centroid_A.T)
  2.         tmp=np.array(tmp)
  3.         t = centroid_B.T - tmp

  4.         print("check t...")
  5.         print(type(tmp),tmp.shape)
  6.         print(centroid_B.T)
  7.         print(np.dot(R,centroid_A.T))
  8.         print(t)
  9.         print("..................")
  10.         # homogeneous transformation
  11.         T = np.identity(dim+1)
  12.         T[:dim, :dim] = R
  13.         T[:dim, dim] = t
复制代码


不懂为什么,两次打印出来的tmp就如一楼所显示的一样,而这造成的后果就是得到的t第一次是1×3的行向量,第二次就是个3*3的矩阵了。然后后面对T赋值就报错了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 12:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表