我试图将numpy int32数组视为int8类型.

>>> a = np.array([1, 2, 3, 4], dtype='int32')

>>> a

array([1, 2, 3, 4], dtype=int32)

>>> a.view('int8')

array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=int8)

我希望将1转换为[0,0,0,1],但是为什么原来是[1、0、0、0]?这与号码在内存中的存储方式有关吗?

谢谢.

解决方法:

Is this related to how the number is stored in memory?

Endianness refers to the sequential order in which bytes are arranged into larger numerical values, when stored in computer memory or secondary storage, or when transmitted over digital links. Endianness is of interest in computer science because two conflicting and incompatible formats are in common use: words may be represented in big-endian or little-endian format, depending on whether bits or bytes or other components are ordered from the big end (most significant bit) or the little end (least significant bit).

>>> import numpy as np

>>> a = np.array([1, 2, 3, 4], dtype='>i4')

>>> a.view('int8')

array([0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4], dtype=int8)

>>> a = np.array([1, 2, 3, 4], dtype='

>>> a.view('int8')

array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0], dtype=int8)

没有它将使用系统的字节序.使用另一种可能导致(轻微)性能下降.

标签:python,numpy

来源: https://codeday.me/bug/20191025/1930446.html

python int32 int8_python-使用numpy视图将int32转换为int8相关推荐

  1. python基础代码库-Python基础数据处理库-NumPy

    最近更新:2017-07-19 NumPy是Python做数据处理的底层库,是高性能科学计算和数据分析的基础,比如著名的Python机器学习库SKlearn就需要NumPy的支持.掌握NumPy的基础 ...

  2. Python 数据分析三剑客之 NumPy(一):理解 NumPy / 数组基础

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  3. Python 第三方模块之 NumPy - 科学计算

    NumPy 简介 NumPy 发展历史 1995年 Jim HugUNin开发了Numeric. 随后,Numarray包诞生. Travis Oliphants整合Numeric和Numarray, ...

  4. python数据分析系列教程——NumPy全解

    速查: 矩阵对象的属性: matrix.T transpose:返回矩阵的转置矩阵 matrix.H hermitian (conjugate) transpose:返回复数矩阵的共轭元素矩阵 mat ...

  5. Python数据分析学习四 NumPy基础:数组和矢量计算

    Python数据分析学习系列四NumPy基础:数组和矢量计算 资料转自(GitHub地址):https://github.com/wesm/pydata-book 有需要的朋友可以自行去github下 ...

  6. Python 数据分析三剑客之 NumPy(五):数学 / 算术 / 统计 / 排序 / 条件 / 判断函数合集

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  7. Python 数据分析三剑客之 NumPy(三):数组的迭代与位运算

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  8. Python 数据分析三剑客之 NumPy(二):数组索引 / 切片 / 广播 / 拼接 / 分割

    CSDN 课程推荐:<Python 数据分析与挖掘>,讲师刘顺祥,浙江工商大学统计学硕士,数据分析师,曾担任唯品会大数据部担任数据分析师一职,负责支付环节的数据分析业务.曾与联想.亨氏.网 ...

  9. 用python画股票分时图 github_用python的matplotlib和numpy库绘制股票K线均线和成交量的整合效果(含量化验证交易策略代码)...

    在用python的matplotlib和numpy库绘制股票K线均线的整合效果(含从网络接口爬取数据和验证交易策略代码)一文里,我讲述了通过爬虫接口得到股票数据并绘制出K线均线图形的方式,在本文里,将 ...

最新文章

  1. 《精通Unix下C语言编程与项目实践》目录
  2. 雷林鹏分享:PHP 魔术常量
  3. Opengl-基本概念-对象(很关键啊兄弟这章)
  4. Condition中的transferForSignal()方法的不解
  5. 分享一批国内常用的tracker地址
  6. mssql 远程无法连接mysql_在本地 怎么远程连接MSSQL数据库
  7. [js] XML与JSON有什么的区别?
  8. 【CodeForces - 304B】Calendar (前缀和,水题)
  9. 计算机网络——物理层基本概念
  10. jquery复选框组清空选中的值_防火墙基本配置:配置策略组
  11. led linux定时闪烁心跳,Linux驱动——LED闪烁
  12. C++的STL标准模板库思维导图
  13. android html片段,详解Android WebView加载html片段
  14. 安卓桌面软件哪个好_有哪些好用的手机记事本软件?安卓手机便签哪个好用?
  15. jQuery 三级联动
  16. Google的地球日图标08版
  17. Android wifi开发介绍
  18. 交换机路由器端口配置
  19. Word文件总页码不含首页和目录设置方法
  20. DB2性能优化 – 如何通过db2优化工具提升SQL查询效率

热门文章

  1. 面试官问:断网了,还能ping通 127.0.0.1 吗?为什么?
  2. java面向对象课件_《JAVA面向对象基础》PPT课件.ppt
  3. Spring Validation校验
  4. Java 核心五个类(File、Outputstream、Inputstream、Reader、Writer)一个接口(Serializable)...
  5. bzoj 4031 [HEOI2015]小Z的房间
  6. eclipse生成boolean型变量的getter是is开头
  7. 【Python学习笔记】
  8. 常用的第三方库(OC)
  9. 51nod 1836:战忽局的手段
  10. 【日本語勉強】日本語四級文法突撃(11~20)