Home  >  Article  >  Backend Development  >  Introduction to Python data types - numpy

Introduction to Python data types - numpy

WBOY
WBOYforward
2022-07-19 13:55:472824browse

This article brings you relevant knowledge about Python, which mainly organizes issues related to numpy data types, including numpy's basic data types, numpy custom composite data types, usage ndarray saves the date data type and so on. Let’s take a look at it. I hope it will be helpful to everyone.

Introduction to Python data types - numpy

[Related recommendations: Python3 video tutorial ]


1. Basic data of numpy Type

##Character typestr, each character is represented by 32-bit Unicode encoding
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)
Type name Type indicator
Boolean bool
Signed integer type int8 / int16 / int32 / int64
Unsigned integer type uint8 / uint16 / uint32 / uint64
Float type float16 / float32 / float64
Complex type complex64 / complex128

Introduction to Python data types - numpy

2. numpy custom composite data type

If you want to store object types in ndarray, numpy recommends

Use tuples to store the object's attribute field values, and then add the tuples to ndarray. ndarray provides syntax to facilitate processing of these data.

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])

Introduction to Python data types - numpy

When the amount of data is large, the above method is not convenient for data access.

ndarray provides data types and column aliases that can be defined in the form of a

dictionary or list . When accessing data, you can access it through subscript indexes or through column names.

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'])

Introduction to Python data types - numpy

3. Use ndarray to save date data type

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])

Introduction to Python data types - numpy

1. The date string does not support

2011/11/11, and the use of spaces to separate dates does not support 2011 11 11, but 2011-11 is supported. -11 2. There needs to be a space to separate the date and time.
2011-04-01 10:10:10 3. The writing format of time
10: 10:10

4. Type character code (abbreviation of data type)

numpy provides type character code to process data types more conveniently.

TypeType indicatorCharacter codeBoolean typebool?Signed integer typeint8 / int16 / int32 / int64i1 / i2 / i4 / i8Unsigned integer typeuint8 / uint16 / uint32 / uint64u1 / u2 / u4 / u8##Floating point type Complex type Character typeDate
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)
float16 / float32 / float64 f2 / f4 / f8
complex64 / complex128 c8 / c16
str, each character is encoded with 32-bit Unicode Represents U
datatime64 M8[Y] / M8[M] / M8[D] / M8 [h] / M8[m] / M8[s]

Introduction to Python data types - numpy

5. Case

Select fields and use ndarray to store data.


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)
Introduction to Python data types - numpy

Introduction to Python data types - numpy[Related recommendations:

Python3 video tutorial

]

The above is the detailed content of Introduction to Python data types - numpy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete