欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中l(wèi)ist,ndarray,Tensor間的轉(zhuǎn)換小結(jié)

 更新時(shí)間:2024年02月02日 11:30:58   作者:tao355667  
數(shù)據(jù)類型轉(zhuǎn)換是常見的功能,本文主要介紹了python中l(wèi)ist,ndarray,Tensor間的轉(zhuǎn)換小結(jié),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、list,ndarray,Tensor間的轉(zhuǎn)化

廢話不多說,看表格就行

數(shù)據(jù)類型所屬包
listpython
ndarraynumpy
Tensorpytorch
轉(zhuǎn)化類型對(duì)應(yīng)API注意點(diǎn)
list轉(zhuǎn)換為ndarraynumpy.array()
ndarray轉(zhuǎn)換為listndarray對(duì)象.tolist()
list轉(zhuǎn)換為Tensortorch.tensor()list中的int類型數(shù)據(jù)轉(zhuǎn)換后會(huì)變?yōu)閒olat,若需要保持int,則轉(zhuǎn)換時(shí)需要加上類型
Tensor轉(zhuǎn)換為listTensor對(duì)象.tolist()
ndarray轉(zhuǎn)換為Tensortorch.from_numpy()torch.tensor()
Tensor(CPU)轉(zhuǎn)換為ndarrayTensor對(duì)象.numpy()GPU上的Tensor不能直接轉(zhuǎn)換為numpy,需要間接轉(zhuǎn)換
Tensor(GPU)轉(zhuǎn)換為ndarrayTensor對(duì)象.cpu().numpy()GPU上的Tensor不能直接轉(zhuǎn)換為numpy,需要間接轉(zhuǎn)換

ndarray --> PILimage

From PIL import Image
y = Image.fromarray(array)

PILimage --> ndarray

From PIL import Image
image = Image.open(“…..”)
img = np.asarray(image)

Tensor --> ndarray

import numpy as np
yy = np.array(tensor) 

ndarray --> Tensor

tensor = torch.from_numpy(ndarray)

tip:返回的張量和ndarray共享同一內(nèi)存。對(duì)張量的修改將反映在ndarray中,反之亦然。

ndarray數(shù)據(jù)轉(zhuǎn)換數(shù)據(jù)類型

array.astype(np.uint8)  

將array復(fù)制,并將數(shù)據(jù)類型強(qiáng)制轉(zhuǎn)化為int8

ndarray --> List

import numpy as np
n = np.array([[1,2],[3,4],[5,6]])
m = n.tolist()

List --> Tensor

tensor = torch.tensor(list)

二、例程

import numpy as np
import torch

#list轉(zhuǎn)換為ndarray
li=[[1,2,3],[4,5,6],[7,8,9]]
a=np.array(li)  #list轉(zhuǎn)換為ndarray
print(a) 
print(type(a),'\n')

#ndarray轉(zhuǎn)換為list
b=a.tolist()#ndarray轉(zhuǎn)換為list
print(b) 
print(type(b),'\n')

#list轉(zhuǎn)換為Tensor
li=[[1,2,3],[4,5,6],[7,8,9]]
c=torch.tensor(li)  #list轉(zhuǎn)換為Tensor
print(c)
print(type(c),'\n')

#Tensor轉(zhuǎn)換為list
d=c.tolist() #Tensor轉(zhuǎn)換為list
print(d) 
print(type(d),'\n')

#ndarray轉(zhuǎn)換為Tensor
nd=np.arange(0,12).reshape(3,4)
e=torch.from_numpy(nd)  #ndarray轉(zhuǎn)換為Tensor
# e=torch.tensor(nd)    #ndarray轉(zhuǎn)換為Tensor
print(e) 
print(type(e),'\n')

#Tensor轉(zhuǎn)換為ndarray
f=e.numpy()
print(f) 
print(type(f),'\n')

運(yùn)行結(jié)果

[[1 2 3]
 [4 5 6]
 [7 8 9]]
<class 'numpy.ndarray'> 

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
<class 'list'> 

tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
<class 'torch.Tensor'> 

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
<class 'list'> 

tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]], dtype=torch.int32)
<class 'torch.Tensor'> 

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
<class 'numpy.ndarray'> 

到此這篇關(guān)于python中l(wèi)ist,ndarray,Tensor間的轉(zhuǎn)化小結(jié)的文章就介紹到這了,更多相關(guān)python list,ndarray,Tensor轉(zhuǎn)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python騷操作完美實(shí)現(xiàn)短視頻偽原創(chuàng)

    Python騷操作完美實(shí)現(xiàn)短視頻偽原創(chuàng)

    剪輯的視頻上傳到某平臺(tái)碰到降權(quán)怎么辦?視頻平臺(tái)都有一套自己的鑒別算法,專門用于處理視頻的二次剪輯,本篇我們來用python做一些特殊處理
    2022-02-02
  • 在Django中創(chuàng)建動(dòng)態(tài)視圖的教程

    在Django中創(chuàng)建動(dòng)態(tài)視圖的教程

    這篇文章主要介紹了在Django中創(chuàng)建動(dòng)態(tài)視圖的教程,Django是Python重多人氣框架中最為著名的一個(gè),需要的朋友可以參考下
    2015-07-07
  • 詳解python文件的操作和異常的處理

    詳解python文件的操作和異常的處理

    這篇文章主要為大家介紹了python文件的操作和異常的處理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 使用python 將圖片復(fù)制到系統(tǒng)剪貼中

    使用python 將圖片復(fù)制到系統(tǒng)剪貼中

    今天小編就為大家分享一篇使用python 將圖片復(fù)制到系統(tǒng)剪貼中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作

    python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作

    這篇文章主要為大家介紹了python爬取B站關(guān)注列表及數(shù)據(jù)庫的設(shè)計(jì)與操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解Python中的字符串常識(shí)

    詳解Python中的字符串常識(shí)

    這篇文章主要為大家介紹了Python中的字符串常識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 在python的類中動(dòng)態(tài)添加屬性與生成對(duì)象

    在python的類中動(dòng)態(tài)添加屬性與生成對(duì)象

    這篇文章給大家介紹了如何在python的類中動(dòng)態(tài)添加屬性和生成對(duì)象,文中通過幾個(gè)方面來進(jìn)行介紹,對(duì)這感興趣的朋友們可以學(xué)習(xí)學(xué)習(xí)。
    2016-09-09
  • python數(shù)學(xué)建模(SciPy+?Numpy+Pandas)

    python數(shù)學(xué)建模(SciPy+?Numpy+Pandas)

    這篇文章主要介紹了python數(shù)學(xué)建模(SciPy+?Numpy+Pandas),文章基于python的相關(guān)資料緊接上一篇文章內(nèi)容展開主題詳情,需要的小伙伴可以參考一下
    2022-07-07
  • Python中six模塊基礎(chǔ)用法

    Python中six模塊基礎(chǔ)用法

    在本篇文章里小編給大家分享的是關(guān)于Python中six模塊基礎(chǔ)用法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-12-12
  • python安裝pandas庫不成功原因分析及解決辦法

    python安裝pandas庫不成功原因分析及解決辦法

    Pandas是python中非常常用的數(shù)據(jù)分析庫,在數(shù)據(jù)分析、機(jī)器學(xué)習(xí)、深度學(xué)習(xí)等領(lǐng)域經(jīng)常被使用,下面這篇文章主要給大家介紹了關(guān)于python安裝pandas庫不成功原因分析及解決辦法的相關(guān)資料
    2023-11-11

最新評(píng)論