PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Python数据类型简介之numpy

WBOY
WBOY 转载
2022-07-19 13:55:47 2611浏览

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

【相关推荐: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视频教程

声明:本文转载于:CSDN,如有侵犯,请联系admin@php.cn删除