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的相互转换相关推荐

  1. PyTorch——torch.Tensor与np.ndarray(NumPy)之间的类型转换

    1 前言 今天在写 Digit Recognizer的代码~ 在对提交文件submission.csv进行写入操作的时候,总会有报错,其中一个原因是其实pandas对np.ndarray数据更加友好, ...

  2. python中的numpy模块和pandas模块的区别_python的numpy模块- 01.pandas基本数据类型

    01.pandas基本数据类型 import pandas as pd import numpy as np from pandas import Series,DataFrame 1. Series ...

  3. python pandas.DataFrame.values和pandas.DataFrame.columns的用法

    引用 pandas.DataFrame.values用于将pandas的DataFrame转换成numpy数组. pandas.DataFrame.columns用于输出pandas的DataFram ...

  4. 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., ...

  5. pytorch中tensor、numpy.array、list三者互相转换

    1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...

  6. PyTorch | torch.tensor使用方法,如何使用torch.tensor

    公众号[计算机视觉联盟]后台回复[PyTorch]可以获得PyTorch学习教程pdf版 通过torch.tensor创建张量 arr = np.ones((3,3)) print("由np ...

  7. torch.Tensor常用数据操作汇总与自动求梯度

    在深度学习中,通常会频繁地对数据进行操作,在PyTorch中,torch.Tensor是存储和变换数据的主要工具(Tensor和NumPy的多维数组非常类似,Tensor提供GPU计算和自动求梯度等更 ...

  8. Tensor 和 NumPy 相互转换

    Tensor 和 NumPy 相互转换 我们很容易用numpy()和from_numpy()将Tensor和NumPy中的数组相互转换.但是需要注意的一点是: 这两个函数所产生的Tensor和NumP ...

  9. python ndarray转换为array_python ndarray与pandas series相互转换,ndarray与dataframe相互转换...

    用python做科技计算时,经常需要类型转换,以下是常用类型转换 一.ndarray 转换为 series 1.如果ndarray是二维数组,如下 array([[1], [2], [3]]) 需要通 ...

最新文章

  1. 异步回调地狱_如何逃避异步/等待地狱
  2. Arduino各开发板针脚示意图
  3. Vi(Linux系统下的标准编辑器)学习笔记
  4. 【语义分割】ICCV21_Self-Regulation for Semantic Segmentation
  5. 15 张图,了解一下 TCP/IP 必知也必会的 10个要点
  6. 转:Redis使用认证密码登录
  7. android前置录像,Android Camera2video使用前置摄像头(Android Camera2video use front camera)
  8. css b加粗怎么用,CSS去掉b加粗和strong加粗标签样式.doc
  9. 2021年美容师(初级)考试题及美容师(初级)报名考试
  10. 计算机唤醒休眠蓝屏,分享win10睡眠唤醒就蓝屏的解决办法
  11. 任正非:华为没有 996,更没有 007!
  12. uniapp canvas 图片加上文字合成一张新图并保存到手机相册
  13. android viewpager标题,ViewPager顶部导航栏联动效果(标题栏条目多)
  14. 二维码活码的诞生、技术原理及使用场景
  15. 人生只有一件事:坚持正确改正错误
  16. 重温与解析《最后生还者》的互动叙事精髓(下)
  17. 粉丝让我爬取热搜话题,结果做成了实时热搜『跑马灯』可视化
  18. 网络计划--时间参数的计算和优化
  19. leetcode-simple-共25题-02
  20. zrender基础入门,简单的案例图形绘制

热门文章

  1. JS移除对象中为空的属性
  2. Stream.max
  3. python之路,用我的方法,也就那样吧!
  4. EDA三大头的恩怨细节
  5. 榆林国号和计算机学校,榆林小学排名2021最新排名 附前十排行榜
  6. 【逆水寒】多方任务系统的信息架构与行为引导设计——以悬赏系统为例
  7. 为什么python注释不能中文_比亚迪全新SUV正式定名为宋PLUS
  8. 常见医学图像处理深度学习方法与热点应用【附参考文献】
  9. Vue 高频面试题总结05 内附答案
  10. solidworks绘制空间曲线的几种常见方法