• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python数据类型简介:numpy

武飞扬头像
PHP中文网
帮助46

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 / int64 i1 / i2 / i4 / i8
无符号整数型 uint8 / uint16 / uint32 / uint64 u1 / u2 / u4 / u8
浮点型 float16 / float32 / float64 f2 / f4 / f8
复数型 complex64 / complex128 c8 / c16
字符型 str,每个字符用 32 位 Unicode 编码表示 U
日期 datatime64 M8[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)

学新通技术网

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfchbc
系列文章
更多 icon
同类精品
更多 icon
继续加载