numpy学习笔记2-array数据类型

博客主要围绕NumPy中ndarray数据类型展开,介绍了常见的ndarray数据类型,讲解了赋值时用dtype自定义数据类型,还说明了用astype方法改变已有array的数据类型,如float型转int型会损失小数部分等,同时提到float型转str型可能会发生截取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.常见ndarry数据类型

类型说明
int有符号整型
unit无符号整型
float浮点型
complex复数型(两个float型表示)
bool布尔型
object对象类型
string_固定长度的字符串类型
unicode_固定长度的unicode型

2.赋值时用dtype自定义数据类型

import numpy as np
array1 = np.array(([1,2,3],[4,5,6]),dtype = 'float64')
array1

array([[ 1., 2., 3.],
[ 4., 5., 6.]])

aryy1.dtype

dtype('float64')


3.用astype方法改变已有array的数据类型

array2 = np.random.randn(2,3)
array2

array([[ 0.20258299, 0.21018739, 0.03580311],
[-1.13358946, 0.20533442, 0.58866677]])

array2.astype(np.int)

array([[ 0, 0, 0],
[-1, 0, 0]])

float型转成int型时会损失小数部分

str型的数字也可以转成float型

array3 = np.array((['1.56','2.34','3.31'],['0.78','3.94','4.01']))
array3.astype(np.float64)

array([[ 1.56, 2.34, 3.31],
[ 0.78, 3.94, 4.01]])

注意float型转str型时由于str型数据大小固定会发生截取,但不会发出警告

转载于:https://www.cnblogs.com/babysteps/p/numpy_note_2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值