[Python] Numpy array 高级用法(2)

前言

最近在看 Numpy文档 和 tutorialspoint Numpy Tutorial 时,发现了一下之前没用过的ndarray高级用法,加上我之前知道的方法,总结一下。以后会陆续更新。

目录

  1. 保存复数complex
  2. numpy.dtype 类型
  3. 更新 ndarray 的 shape

正文

2. numpy.dtype 类型

'''初始化dtype'''
# using array-scalar type 
import numpy as np 
dt = np.dtype(np.int32) 
print(dt)
# 输出:
int32
'''int8, int16, int32, int64 可以被 'i1', #'i2','i4' 等代替. '''
# "<" 和 ">" 代表大小端,默认小端;
import numpy as np 
dt = np.dtype('>i4')
print(dt)
# 输出:
int32
'''结构化的dtype'''
dt = np.dtype([('age',np.int8)]) # dt输出为 [('age', 'i1')] 

a = np.array([(10,),(20,),(30,)], dtype = dt) 
print(a)
#输出为:
[(10,) (20,) (30,)]
''''更进一步'''
student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')]) 
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) 
print(a)
# 输出:
[('abc', 21, 50.0), ('xyz', 18, 75.0)]

下面是类型信息表格:
[Python] Numpy array 高级用法(2)_第1张图片

你可能感兴趣的:(Python)