python語言程序設計,python 近期用到的基礎知識匯總(五)

 2023-10-06 阅读 32 评论 0

摘要: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.fro

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()即可

python語言程序設計?還可以使用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變量

python 列表,(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),

python編程,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

python和java,(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])

qpython。參考: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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/123071.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息