1.pytorch中变量类型转换
将numpy矩阵转换为Tensor张量:sub_ts = torch.from_numpy(sub_img) #sub_img为numpy类型
将Tensor张量转化为numpy矩阵:sub_np1 = sub_ts.numpy() #sub_ts为tensor张量
将numpy转换为Variable:sub_va = Variable(torch.from_numpy(sub_img))
将Variable张量转化为numpy:sub_np2 = sub_va.data.numpy()
(1)CPU或GPU张量之间的转换
一般只要在Tensor后加long(), int(), double(),float(),byte()等函数就能将Tensor进行类型转换;
例如:Torch.LongTensor--->Torch.FloatTensor, 直接使用data.float()即可
还可以使用type()函数,data为Tensor数据类型,data.type()为给出data的类型,如果使用data.type(torch.FloatTensor)则强制转换为torch.FloatTensor类型张量。
当你不知道要转换为什么类型时,但需要求a1,a2两个张量的乘积,可以使用a1.type_as(a2)将a1转换为a2同类型。
(2)CPU张量 ----> GPU张量, 使用data.cuda()
(3)GPU张量 ----> CPU张量 使用data.cpu()
(4)Variable变量转换成普通的Tensor,其实可以理解Variable为一个Wrapper,里头的data就是Tensor. 如果Var是Variable变量,使用Var.data获得Tensor变量
(5)Tensor与Numpy Array之间的转换
Tensor---->Numpy 可以使用 data.numpy(),data为Tensor变量
Numpy ----> Tensor 可以使用torch.from_numpy(data),data为numpy变量
参考:https://blog.csdn.net/hustchenze/article/details/79154139 ,https://blog.csdn.net/pengge0433/article/details/79459679 .
2.碰到一个问题:
pbb为<type 'tuple'>: (234, 5),
pbb1=pbb[0]为<type 'tuple'>: (5,)成为一个数组,降维了,
pbb2=pbb[0:1]为<type 'tuple'>: (1, 5)还是一个二维矩阵
pbb3=np.delete(pbb2,[0],axis=0)为<type 'tuple'>: (0, 5)还是一个二维矩阵.
总结:要注意矩阵A 的A[0]和A[0:1]是不同的.
3.(1)类型type
不变,数值value
取整。(矩阵取整)
截取整数部分 np.trunc
向上取整 np.ceil
向下取整np.floor
四舍五入取整np.rint
(2)类型type
改变
AA = np.array
AA.astype(np.int)
ps:这个好像不是太管用可能是我用错了,或版本问题??
用下面这个管用:
a=np.array(a, dtype=np.int16)
(3)分别用list,np.array 存储数据导致的不同点
# 为了看不同点,生成一个不变的数组
# 如果用list,那么astype就有点麻烦
In [245]: customersAge = [70 * np.random.rand(20)]
In [250]: np.trunc(customersAge)
Out[250]:
array([[ 62., 33., 47., 25., 57., 64., 0., 50., 66., 34., 44.,
45., 14., 40., 48., 45., 5., 50., 29., 35.]])
In [251]: np.ceil(customersAge)
Out[251]:
array([[ 63., 34., 48., 26., 58., 65., 1., 51., 67., 35., 45.,
46., 15., 41., 49., 46., 6., 51., 30., 36.]])
In [252]: np.floor(customersAge)
Out[252]:
array([[ 62., 33., 47., 25., 57., 64., 0., 50., 66., 34., 44.,
45., 14., 40., 48., 45., 5., 50., 29., 35.]])
In [253]: np.rint(customersAge)
Out[253]:
array([[ 62., 33., 47., 25., 58., 64., 0., 50., 67., 35., 44.,
45., 14., 41., 49., 45., 6., 51., 29., 36.]])
# 但这样list不能直接用astype,要把格式换成array...呵呵呵
In [254]: customersAge.astype(np.int)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-254-a648fd813d6e> in <module>()
----> 1 customersAge.astype(np.int)
AttributeError: 'list' object has no attribute 'astype'
In [256]: np.array(customersAge).astype(np.int)
Out[256]:
array([[62, 33, 47, 25, 57, 64, 0, 50, 66, 34, 44, 45, 14, 40, 48, 45, 5,
50, 29, 35]])
# 既然用numpy,最好就是np.array用到底
In [264]: customersAge = np.array( 70 * np.random.rand(20))
In [265]: customersAge.astype(np.int)
Out[265]:
array([57, 31, 59, 0, 27, 6, 25, 23, 54, 18, 33, 17, 67, 66, 24, 57, 45,
64, 62, 47])
参考:https://www.jianshu.com/p/23a9224780e8
4.Python取numpy矩阵中的不连续的某几行某几列方法
原始矩阵和想要选取的目标如下:
C_A = c[[0,2]] #先取出想要的行数据
C_A = C_A[:,[2,3]] #再取出要求的列数据
print(C_A) #输出最终结果
参考:https://blog.csdn.net/qq_34734303/article/details/80631831