list、numpy.ndarray 和 pandas.dataframe的相互转换、torch.tensor和numpy.ndarray的相互转换
1、list 转 numpy.ndarray: np.array()
2、numpy.ndarray 转 pandas.Dataframe: pandas.DataFrame()
3、pandas.Dataframe 转 numpy.ndarray: Dataframe.values
4、numpy.ndarray 转 list: array.tolist()
示例:
import numpy as np
import pandas as pda = [[1, 2, 3],[4, 5, 6]] # list
b = np.array(a) # list to ndarray
c = pd.DataFrame(b) # ndarray to dataframeprint('type a:',type(a),'\ntype b:',type(b),'\ntype c:',type(c))d = c.values # dataframe to ndarray
e = d.tolist() # ndarray to list
print('type d:',type(d),'\ntype e:',type(e))
输出结果:
type a: <class 'list'>
type b: <class 'numpy.ndarray'>
type c: <class 'pandas.core.frame.DataFrame'>
type d: <class 'numpy.ndarray'>
type e: <class 'list'>
numpy.ndarray 与 torch.tensor 的相互转换:
import torch
import numpy as npa = np.ones(10) # arrayb = torch.from_numpy(a).type(torch.FloatTensor) # array to tensorc = b.numpy() # tensor to array print('type a:',type(a),'\ntype b:',type(b),'\ntype c:',type(c))
输出结果:
type a: <class 'numpy.ndarray'>
type b: <class 'torch.Tensor'>
type c: <class 'numpy.ndarray'>
CUDA中的tensor转numpy:
np_array = tensor.data.cpu().numpy()
list、numpy.ndarray 和 pandas.dataframe的相互转换、torch.tensor和numpy.ndarray的相互转换相关推荐
- PyTorch——torch.Tensor与np.ndarray(NumPy)之间的类型转换
1 前言 今天在写 Digit Recognizer的代码~ 在对提交文件submission.csv进行写入操作的时候,总会有报错,其中一个原因是其实pandas对np.ndarray数据更加友好, ...
- python中的numpy模块和pandas模块的区别_python的numpy模块- 01.pandas基本数据类型
01.pandas基本数据类型 import pandas as pd import numpy as np from pandas import Series,DataFrame 1. Series ...
- python pandas.DataFrame.values和pandas.DataFrame.columns的用法
引用 pandas.DataFrame.values用于将pandas的DataFrame转换成numpy数组. pandas.DataFrame.columns用于输出pandas的DataFram ...
- pytorch:tensor与numpy转换 .cpu.numpy()和.numpy() torch.from_numpy VS torch.Tensor
1.tensor to numpy 1.1代码 .numpy() import torch a0 = torch.ones(6) print(a0) 运行结果: tensor([1., 1., 1., ...
- pytorch中tensor、numpy.array、list三者互相转换
1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...
- PyTorch | torch.tensor使用方法,如何使用torch.tensor
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得PyTorch学习教程pdf版 通过torch.tensor创建张量 arr = np.ones((3,3)) print("由np ...
- torch.Tensor常用数据操作汇总与自动求梯度
在深度学习中,通常会频繁地对数据进行操作,在PyTorch中,torch.Tensor是存储和变换数据的主要工具(Tensor和NumPy的多维数组非常类似,Tensor提供GPU计算和自动求梯度等更 ...
- Tensor 和 NumPy 相互转换
Tensor 和 NumPy 相互转换 我们很容易用numpy()和from_numpy()将Tensor和NumPy中的数组相互转换.但是需要注意的一点是: 这两个函数所产生的Tensor和NumP ...
- python ndarray转换为array_python ndarray与pandas series相互转换,ndarray与dataframe相互转换...
用python做科技计算时,经常需要类型转换,以下是常用类型转换 一.ndarray 转换为 series 1.如果ndarray是二维数组,如下 array([[1], [2], [3]]) 需要通 ...
最新文章
- 异步回调地狱_如何逃避异步/等待地狱
- Arduino各开发板针脚示意图
- Vi(Linux系统下的标准编辑器)学习笔记
- 【语义分割】ICCV21_Self-Regulation for Semantic Segmentation
- 15 张图,了解一下 TCP/IP 必知也必会的 10个要点
- 转:Redis使用认证密码登录
- android前置录像,Android Camera2video使用前置摄像头(Android Camera2video use front camera)
- css b加粗怎么用,CSS去掉b加粗和strong加粗标签样式.doc
- 2021年美容师(初级)考试题及美容师(初级)报名考试
- 计算机唤醒休眠蓝屏,分享win10睡眠唤醒就蓝屏的解决办法
- 任正非:华为没有 996,更没有 007!
- uniapp canvas 图片加上文字合成一张新图并保存到手机相册
- android viewpager标题,ViewPager顶部导航栏联动效果(标题栏条目多)
- 二维码活码的诞生、技术原理及使用场景
- 人生只有一件事:坚持正确改正错误
- 重温与解析《最后生还者》的互动叙事精髓(下)
- 粉丝让我爬取热搜话题,结果做成了实时热搜『跑马灯』可视化
- 网络计划--时间参数的计算和优化
- leetcode-simple-共25题-02
- zrender基础入门,简单的案例图形绘制