这篇文章我们玩玩numpy的数值数据类型转换

导入numpy

>>> import numpy as np

一、随便玩玩

生成一个浮点数组

>>> a = np.random.random(4)

看看信息

>>> a
array([ 0.0945377 , 0.52199916, 0.62490646, 0.21260126])
>>> a.dtype
dtype('float64')
>>> a.shape
(4,)

改变dtype,发现数组长度翻倍!

>>> a.dtype = 'float32'
>>> a
array([ 3.65532693e+20, 1.43907535e+00, -3.31994873e-25,
1.75549972e+00, -2.75686653e+14, 1.78122652e+00,
-1.03207532e-19, 1.58760118e+00], dtype=float32)
>>> a.shape
(8,)

改变dtype,数组长度再次翻倍!

>>> a.dtype = 'float16'
>>> a
array([ -9.58442688e-05, 7.19000000e+02, 2.38159180e-01,
1.92968750e+00, nan, -1.66034698e-03,
-2.63427734e-01, 1.96875000e+00, -1.07519531e+00,
-1.19625000e+02, nan, 1.97167969e+00,
-1.60156250e-01, -7.76290894e-03, 4.07226562e-01,
1.94824219e+00], dtype=float16)
>>> a.shape
(16,)

改变dtype='float',发现默认就是float64,长度也变回最初的4

>>> a.dtype = 'float'
>>> a
array([ 0.0945377 , 0.52199916, 0.62490646, 0.21260126])
>>> a.shape
(4,)
>>> a.dtype
dtype('float64')

把a变为整数,观察其信息

>>> a.dtype = 'int64'
>>> a
array([4591476579734816328, 4602876970018897584, 4603803876586077261,
4596827787908854048], dtype=int64)
>>> a.shape
(4,)

改变dtype,发现数组长度翻倍!

>>> a.dtype = 'int32'
>>> a
array([ 1637779016, 1069036447, -1764917584, 1071690807, -679822259,
1071906619, -1611419360, 1070282372])
>>> a.shape
(8,)

改变dtype,发现数组长度再次翻倍!

>>> a.dtype = 'int16'
>>> a
array([-31160, 24990, 13215, 16312, 32432, -26931, -19401, 16352,
-17331, -10374, -197, 16355, -20192, -24589, 13956, 16331], dtype=int16)
>>> a.shape
(16,)

改变dtype,发现数组长度再次翻倍!

>>> a.dtype = 'int8'
>>> a
array([ 72, -122, -98, 97, -97, 51, -72, 63, -80, 126, -51,
-106, 55, -76, -32, 63, 77, -68, 122, -41, 59, -1,
-29, 63, 32, -79, -13, -97, -124, 54, -53, 63], dtype=int8)
>>> a.shape
(32,)

改变dtype,发现整数默认int32!

>>> a.dtype = 'int'
>>> a.dtype
dtype('int32')
>>> a
array([ 1637779016, 1069036447, -1764917584, 1071690807, -679822259,
1071906619, -1611419360, 1070282372])
>>> a.shape
(8,)

二、换一种玩法

很多时候我们用numpy从文本文件读取数据作为numpy的数组,默认的dtype是float64。
但是有些场合我们希望有些数据列作为整数。如果直接改dtype='int'的话,就会出错!原因如上,数组长度翻倍了!!!

下面的场景假设我们得到了导入的数据。我们的本意是希望它们是整数,但实际上是却是浮点数(float64)

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

用 astype(int) 得到整数,并且不改变数组长度

>>> c = b.astype(int)
>>> c
array([1, 2, 3, 4])
>>> c.shape
(8,)
>>> c.dtype
dtype('int32')

如果直接改变b的dtype的话,b的长度翻倍了,这不是我们想要的(当然如果你想的话)

>>> b
array([ 1., 2., 3., 4.]) >>> b.dtype = 'int'
>>> b.dtype
dtype('int32')
>>> b
array([ 0, 1072693248, 0, 1073741824, 0,
1074266112, 0, 1074790400])
>>> b.shape
(8,)

三、结论

numpy中的数据类型转换,不能直接改原数据的dtype!  只能用函数astype()。

numpy数据类型dtype转换的更多相关文章

  1. Numpy 数据类型和基本操作

    Numpy 数据类型 bool 用一位存储的布尔类型(值为TRUE或FALSE) inti 由所在平台决定其精度的整数(一般为int32或int64) int8 整数,范围为128至127 int1 ...

  2. NumPy数据类型

    NumPy - 数据类型 NumPy 支持比 Python 更多种类的数值类型. 下表显示了 NumPy 中定义的不同标量数据类型. 序号 数据类型及描述 1. bool_存储为一个字节的布尔值(真或 ...

  3. 2、NumPy 数据类型

    1.NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型.下表列举了常用 NumP ...

  4. Lesson3——NumPy 数据类型

    NumPy 教程目录 NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型. 下表 ...

  5. java中数据类型的转换

    数据类型的转换,分为自动转换和强制转换. 自动转换是程序执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换 强制转换必须在代码中声明,转换顺序不受限制 自动数据类 ...

  6. Java的基本数据类型与转换

    1.1 Java为什么需要保留基本数据类型 http://www.importnew.com/11915.html 基本数据类型对大多数业务相关或网络应用程序没有太大的用处,这些应用一般是采用客户端/ ...

  7. java的数据类型的转换

    一:java的数据类型转换除布尔类型boolean(不能转换)有两种:<一> 自动转换: <二> 强制转换 <一>.自动转换:就是将小的数据类型自动转换成大的数据类 ...

  8. JavaScript学习笔记——数据类型强制转换和隐式转换

    javascript数据类型强制转换 一.转换为数值类型 Number(参数) 把任何的类型转换为数值类型 A.如果是布尔值,false为0,true为1 B.如果是数字,转换成为本身.将无意义的后导 ...

  9. JAVA数据类型自动转换,与强制转换

    一.数据类型自动转换 public class Test{ public static void main(String[] args){ int a = 1; double b = 1.5; dou ...

随机推荐

  1. cxf 整合 spring 时 java.lang.VerifyError异常

    异常信息主要有两个,Falling off the end of the code 和 illegal instruction found at offset 1: java.lang.VerifyE ...

  2. 我的第一个MyBatis

    (1)步骤:1.新建实体类对象---根据数据库字段来设计,有多少字段设多少变量,变量名要字段名一致.   2.新建配置文件config.xml---主要用来获取数据源,里面内容大致需要填写:数据库驱动 ...

  3. GitHub的Repository权限将public转为private

    2019年1月7日,GitHub CEO Nat Friedman 于官方博客公开发文,称“New year, new GitHub”,宣布从此将免费无限地为普通用户提供私有仓库服务. 因此,我们可以 ...

  4. .NET Core 性能分析: xUnit.Performance 简介

    xunit-performance 是xUnit的一个扩展, 使用它可以对.NET Core项目进行性能测试. 官网:https://github.com/Microsoft/xunit-perfor ...

  5. Kubernetes的DaemonSet(上篇)

    背景 静儿作为美团容器化团队HULK的一员,经常需要和Kubernetes(k8s)打交道.第一次登陆node(宿主机)的时候,发现连续登陆几台都看到了Prometheus-Node-Exporter ...

  6. org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.Integer

    如图: 详细错误信息如下: org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.l ...

  7. 利用shell脚本生成CHANGELOG.md(包含git提交规范)

    前言 我们经常看到github上面有很多CHANGELOG.MD包含版本的更新信息,如果我们的git提交能遵循一定的规范,那么使用gitlog就能很方便的生成它 生成结果  shell脚本 http ...

  8. Virtual Box虚拟机Ubuntu系统安装及基本配置

    Linux简介 什么是 Linux? Linux:世界上不仅只有一个 Windows 操作系统,还有 Linux.mac.Unix 等操作系统.桌面操作系统下 Windows 是霸主,而 Linux ...

  9. js将一个数组分成多个数组

    1,将数组array分成长度为subGroupLength的小数组并返回新数组 function group(array, subGroupLength) { let index = 0; let n ...

  10. sql 服务启动失败 SQL Server(MSSQLSERVER) 错误码126

    SQL配置管理器-->sql server 网络配置-->mssqlerver的协议-->VIA禁用服务