python教程:Python数据类型简介之numpy

 所属分类:php教程

 浏览:100次-  评论: 0次-  更新时间:2022-09-30
描述:更多教程资料进入php教程获得。 本篇文章给大家带来了关于Python的相关知识,其中主要整理了numpy数据类型的相关问题,包括了numpy的基本数...
更多教程资料进入php教程获得。 本篇文章给大家带来了关于Python的相关知识,其中主要整理了numpy数据类型的相关问题,包括了numpy的基本数据类型、numpy自定义复合数据类型、使用ndarray保存日期数据类型等等内容,下面一起来看一下,希望对大家有帮助。

程序员必备接口测试调试工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
后端、前端、测试,同时在线协作,内容实时同步

【相关推荐:Python3视频教程 】


1. numpy 的基本数据类型

类型名类型表示符
布尔型bool
有符号整数型int8 / int16 / int32 / int64
无符号整数型uint8 / uint16 / uint32 / uint64
浮点型float16 / float32 / float64
复数型complex64 / complex128
字符型str,每个字符用 32 位 Unicode 编码表示
import numpy as np

arr = np.array([1, 2, 3])
print(arr, arr.dtype)

arr = arr.astype('int64')
print(arr, arr.dtype)

arr = arr.astype('float32')
print(arr, arr.dtype)

arr = arr.astype('bool')
print(arr, arr.dtype)

arr = arr.astype('str')
print(arr, arr.dtype)

在这里插入图片描述

2. numpy 自定义复合数据类型

如果希望 ndarray 中存储对象类型,numpy 建议使用元组存储对象的属性字段值,然后把元组添加到 ndarray 中,ndarray 提供了语法方便处理这些数据。

import numpy as np

data = [
    ('zs', [99, 98, 90], 17),
    ('ls', [95, 95, 92], 16),
    ('ww', [97, 92, 91], 18)
]
# 姓名 2 个字符
# 3 个 int32 类型的成绩
# 1 个 int32 类型的年龄
arr = np.array(data, dtype='2str, 3int32, int32')
print(arr)
print(arr.dtype)
# 可以通过索引访问
print(arr[0], arr[0][2])

在这里插入图片描述

当数据量大时,采用上述方法不便于数据的访问。

ndarray 提供可以采用字典或列表的形式定义数组元素的数据类型和列的别名。访问数据时,可以通过下标索引访问,也可以通过列名进行数据访问。

import numpy as np

data = [
    ('zs', [99, 98, 90], 17),
    ('ls', [95, 95, 92], 16),
    ('ww', [97, 92, 91], 18)]# 采用字典定义列名和元素的数据类型arr = np.array(data, dtype={
    # 设置每列的别名
    'names': ['name', 'scores', 'age'],
    # 设置每列数据元素的数据类型
    'formats': ['2str', '3int32', 'int32']})print(arr, arr[0]['age'])# 采用列表定义列名和元素的数据类型arr = np.array(data, dtype=[
    # 第一列
    ('name', 'str', 2),
    # 第二列
    ('scores', 'int32', 3),
    # 第三列
    ('age', 'int32', 1)])print(arr, arr[1]['scores'])# 直接访问数组的一列print(arr['scores'])

在这里插入图片描述

3. 使用 ndarray 保存日期数据类型

import numpy as np

dates = [
    '2011',
    '2011-02',
    '2011-02-03',
    '2011-04-01 10:10:10'
]

ndates = np.array(dates)
print(ndates, ndates.dtype)

# 数据类型为日期类型,采用 64 位二进制进行存储,D 表示日期精确到天
ndates = ndates.astype('datetime64[D]')
print(ndates, ndates.dtype)

# 日期运算
print(ndates[-1] - ndates[0])

在这里插入图片描述

1.日期字符串支持不支持 2011/11/11,使用空格进行分隔日期也不支持 2011 11 11,支持 2011-11-11
2.日期与时间之间需要有空格进行分隔 2011-04-01 10:10:10
3.时间的书写格式 10:10:10

4. 类型字符码(数据类型简写)

numpy 提供了类型字符码可以更加方便的处理数据类型。

类型类型表示符字符码
布尔型bool?
有符号整数型int8 / int16 / int32 / int64i1 / i2 / i4 / i8
无符号整数型uint8 / uint16 / uint32 / uint64u1 / u2 / u4 / u8
浮点型float16 / float32 / float64f2 / f4 / f8
复数型complex64 / complex128c8 / c16
字符型str,每个字符用 32 位 Unicode 编码表示U
日期datatime64M8[Y] / M8[M] / M8[D] / M8[h] / M8[m] / M8[s]
import numpy as np

data = [
    ('zs', [99, 98, 90], 17),
    ('ls', [95, 95, 92], 16),
    ('ww', [97, 92, 91], 18)
]
# 采用字典定义列名和元素的数据类型
arr = np.array(data, dtype={
    # 设置每列的别名
    'names': ['name', 'scores', 'age'],
    # 设置每列数据元素的数据类型
    'formats': ['2U', '3i4', 'i4']
})

print(arr)
print(arr[1]['scores'])
print(arr['scores'])
print(arr.dtype)

在这里插入图片描述

5. 案例

选取字段,使用 ndarray 存储数据。
在这里插入图片描述

import numpy as np

datas = [
    (0, '4室1厅', 298.79, 2598, 86951),
    (1, '3室2厅', 154.62, 1000, 64675),
    (2, '3室2厅', 177.36, 1200, 67659),]arr = np.array(datas, dtype={
    'names': ['index', 'housetype', 'square', 'totalPrice', 'unitPrice'],
    'formats': ['u1', '4U', 'f4', 'i4', 'i4']})print(arr)print(arr.dtype)# 计算 totalPrice 的均值sum_totalPrice = sum(arr['totalPrice'])print(sum_totalPrice/3)

在这里插入图片描述

【相关推荐:Python3视频教程 】

以上就是Python数据类型简介之numpy的详细内容,更多请关注zzsucai.com其它相关文章!

 标签: python,
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!