Home > Article > Backend Development > Introduction to Python data types - numpy
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.
[Related recommendations: Python3 video tutorial ]
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 |
str, each character is represented by 32-bit Unicode encoding |
2. numpy custom composite data typeIf 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])
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'])
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])
1. The date string does not support4. Type character code (abbreviation of data type)numpy provides type character code to process data types more conveniently.2011/11/11
, and the use of spaces to separate dates does not support
2011 11 11, but
2011-11 is supported. -112. There needs to be a space to separate the date and time.
2011-04-01 10:10:103. The writing format of time
10: 10:10
Type indicator | Character code | |
---|---|---|
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, each character is encoded with 32-bit Unicode Represents | U | |
datatime64 | M8[Y] / M8[M] / M8[D] / M8 [h] / M8[m] / M8[s] |
5. Case
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)
[Related recommendations:
Python3 video tutorialThe 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!