一、单个数据转换为其他数据格式
1.1 单个数据 -> list
直接在数据上进行列表化操作,或者直接接入已经有的列表
语法:
list_name = [single_data] 或者 list_name.append(single_data)
- 1
1.2 单个数据 -> numpy.ndarray
直接使用初始化语法就可以
语法:
numpyarr_name = np.array(single_data)
- 1
举例:
import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
- 1
- 2
- 3
- 4
- 5
- 6
1.3 单个数据 ->torch.tensor
直接使用初始化语法就可以
方法一语法:(但这种方法新建的是维度为0)
tensor_name = torch.tensor(single_data)
- 1
方法二语法:通过列表的方式进行新建tensor
tensor_name = torch.Tensor([single_data])
- 1
方法三语法:新建一个空张量然后赋值进去(这种方法新建的维度为1)
tensor_name = torch.empty(1)
tensor_name[0] = single_data
- 1
- 2
举例:
import torch
single_data = 100
tensor_test = torch.tensor(single_data)
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.Tensor([single_data])
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.empty(1)
tensor_test[0] = single_data
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
二、list 转换为其他数据格式
2.1 list -> 单个数据
直接使用python中的强制类型转换
2.2 list -> numpy.ndarray
注意:转换时list里面的类型必须一样
语法:
numpyarr_name = np.array(list_name)
- 1
举例:
import numpy as np
list_test = [1,2,3,4]
print(type(list_test))
numpyarr_test = np.array(list_test)
print(type(numpyarr_test))
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.3 list -> torch.tensor
注意:转换时list里面所有的元素的类型(包括多维度列表的元素中元素)必须一样
语法:
tensor_name = torch.Tensor(list_name)
- 1
举例:
import torch
list_test = [[1,2,3],[4,5,6]]
print(type(list_test))
tensor_test = torch.Tensor(list_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
三、numpy.ndarray 转换为其他数据格式
3.1 numpy.ndarry -> 单个数据
直接使用python中的强制类型转换
举例:
import numpy as np
single_data = float(np.array([100]))
print(single_data)
print(type(single_data))
- 1
- 2
- 3
- 4
3.2 numpy.ndarry -> list
语法:
numpyarr_name.tolist()
- 1
举例:
import numpy as np
numpyarr_test = np.ones((1,2))
list_test = numpyarr_test.tolist()
print(type(list_test))
print(len(list_test))
print(len(list_test[0]))
- 1
- 2
- 3
- 4
- 5
- 6
3.3 numpy.ndarry -> torch.tensor
注意1:只能转换成CPU张量
注意2:shape不会改变
语法:
tensor_name = torch.from_numpy(numpyarr_name)
- 1
举例:
import torch
import numpy as np
numpyarr_test = np.ones((1,2))
print(type(numpyarr_test))
print(numpyarr_test.dtype)
tensor_test = torch.from_numpy(numpyarr_test)
print(type(tensor_test))
print(tensor_test.type())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
四、torch.tensor 转换为其他数据格式
4.1 torch.tensor -> 单个数据
直接使用python中的强制类型转换
举例:
import torch
tensor_test = torch.ones(1)
single_data = float(tensor_test)
print(single_data)
print(type(single_data))
- 1
- 2
- 3
- 4
- 5
4.2 torch.tensor -> list
语法:
list_name = tensor_name.tolist()
- 1
举例:
import torch
tensor_test = torch.ones((1,3))
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
list_test = tensor_test.tolist()
print(list_test)
print(type(list_test))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.3 torch.tensor -> numpy.ndarry
注意:只有CPU张量能进行如此转换
注意:shape不会改变
语法:
numpyarr_name = tensor_name.numpy()
# 直接使用不进行赋值的话不会改变类型
- 1
- 2
举例:
import torch
import numpy as np
tensor_test = torch.Tensor(1,2)
print(type(tensor_test))
print(tensor_test.type())
numpyarr_test = tensor_test.numpy()
print(type(numpyarr_test))
print(numpyarr_test.dtype)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
LAST、参考文献
pytorch: tensor类型的构建与相互转换_JNing-CSDN博客
NumPy - 数据类型 · TutorialsPoint NumPy 教程
Pytorch 基本的数据类型_洪流之源-CSDN博客_pytorch 数据类型
[numpy] ndarray 与 list 互相转换_doufuxixi的博客-CSDN博客_ndarray转list
python3 list, np.array, torch.tensor相互转换_黑白德芙Sani的博客-CSDN博客
Python——array与list相互转换_Haiyang_Duan-CSDN博客
python数据分析之numpy初始化(一)_野孩子的专栏-CSDN博客
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树科学计算基础软件包NumPyNumPy概述416632 人正在系统学习中
评论记录:
回复评论: