目录
ndarray属性:
ndarray.ndim | 秩,即轴的数量或维度的数量 |
ndarray.shape | 数组的维度。对于矩阵,n 行 m 列 |
ndarray.dtype | ndarray 对象的元素类型 |
dtype分类:
bool_ | 布尔型数据类型(True 或者 False) |
int_ | 默认的整数类型(类似于 C 语言中的 long,int32 或 int64) |
intc | 与 C 的 int 类型一样,一般是 int32 或 int 64 |
intp | 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64) |
int8 | 字节(-128 to 127) |
int16 | 整数(-32768 to 32767) |
int32 | 整数(-2147483648 to 2147483647) |
int64 | 整数(-9223372036854775808 to 9223372036854775807) |
uint8 | 无符号整数(0 to 255) |
uint16 | 无符号整数(0 to 65535) |
uint32 | 无符号整数(0 to 4294967295) |
uint64 | 无符号整数(0 to 18446744073709551615) |
float_ | float64 类型的简写 |
float16 | 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位 |
float32 | 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位 |
float64 | 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位 |
complex_ | complex128 类型的简写,即 128 位复数 |
complex64 | 复数,表示双 32 位浮点数(实数部分和虚数部分) |
complex128 | 复数,表示双 64 位浮点数(实数部分和虚数部分) |
一、数组的创建:
01-在python数据类型基础上创建:
np.asarray(object, dtype=none, order=none)
object | 支持python列表,元组,基本类型嵌套,多维数据类型。ps:集合比较特殊 |
dtype | 指定创建的数组类型 |
order | "C"和"F", 行优先和列优先,在计算机内存中的存储元素的顺序 |
list001 = [i // 2 for i in range(20)]
source_list = [list001, set(list001), tuple(list001)]
np_lists = [np.asarray(i) for i in source_list]
for i in range(len(np_lists)):
print(f'{i}: {np_lists[i]}\t{type(np_lists[i])}'
f'\t{np_lists[i].dtype}\t{np_lists[i].shape}')
结果:
0: [0 0 1 1 2 2 3 3 4 4] <class 'numpy.ndarray'> int32 (10,)
1: {0, 1, 2, 3, 4} <class 'numpy.ndarray'> object ()
2: [0 0 1 1 2 2 3 3 4 4] <class 'numpy.ndarray'> int32 (10,)
02-生成数组:
指定范围
numpy.arange(start=0, stop, step=1, dtype=None)
start | 起始值 |
stop | 终止值 |
step | 步长 |
dtype | ndarray类型 |
等差数列生成
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
等比数列生成
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
TBD