Python数据类型介绍:numpy基础概览?

2026-04-30 20:021阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1349个文字,预计阅读时间需要6分钟。

Python数据类型介绍:numpy基础概览?

本章节为家庭带来关于Python的相关知识,主要整理了numpy数据类型的相关问题。包括numpy的基本数据类型、自定义复杂数据类型、使用ndarray存储日期数据类型等内容。

Python数据类型介绍:numpy基础概览?

本篇文章给大家带来了关于Python的相关知识,其中主要整理了numpy数据类型的相关问题,包括了numpy的基本数据类型、numpy自定义复合数据类型、使用ndarray保存日期数据类型等等内容,下面一起来看一下,希望对大家有帮助。

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


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)登录后复制

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

本文共计1349个文字,预计阅读时间需要6分钟。

Python数据类型介绍:numpy基础概览?

本章节为家庭带来关于Python的相关知识,主要整理了numpy数据类型的相关问题。包括numpy的基本数据类型、自定义复杂数据类型、使用ndarray存储日期数据类型等内容。

Python数据类型介绍:numpy基础概览?

本篇文章给大家带来了关于Python的相关知识,其中主要整理了numpy数据类型的相关问题,包括了numpy的基本数据类型、numpy自定义复合数据类型、使用ndarray保存日期数据类型等等内容,下面一起来看一下,希望对大家有帮助。

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


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)登录后复制

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