Python3-笔记-numpy学习指南-002-基础
import numpy
# 创建一维的数组对象
a = numpy.arange(5)
print(a)
print(a.dtype) # 类型
print(a.shape) # 数据维度
[0 1 2 3 4]
int32
(5,)
# 创建二维的数组对象
print('-------')
m = numpy.array([numpy.arange(2), numpy.arange(2)])
print(m.shape) # 数据维度
print(m, m[0], m[1], m[1][1], sep='---') # 元素的选取
(2, 2)
[[0 1]
[0 1]]---[0 1]---[0 1]---1
# numpy的数据类型
# numpy的数据类型,类型转换,占用内存字节数
f64 = numpy.float64(42)
print(f64, type(f64), f64.dtype, f64.dtype.itemsize)
i8 = numpy.int8(42.0)
print(i8, type(i8), i8.dtype, i8.dtype.itemsize)
b = numpy.bool(42)
print(b, type(b))
b = numpy.bool(0)
print(b, type(b))
b = numpy.bool(42.0)
print(b, type(b))
f = numpy.float(True)
print(f, type(f))
f = numpy.float(False)
print(f, type(f))
u16 = numpy.arange(7, dtype = numpy.uint16)
print(u16, type(u16), u16.dtype, u16.dtype.itemsize)
42.0 <class 'numpy.float64'> float64 8
42 <class 'numpy.int8'> int8 1
True <class 'bool'>
False <class 'bool'>
True <class 'bool'>
1.0 <class 'float'>
0.0 <class 'float'>
[0 1 2 3 4 5 6] <class 'numpy.ndarray'> uint16 2
创建自定义数据类型
t = numpy.dtype([('name', str, 40), ('numitems', numpy.int32), ('price',numpy.float32)])
print(t)
[('name', '<U40'), ('numitems', '<i4'), ('price', '<f4')]
创建自定义数据类型的数组
itemz = numpy.array([('Meaning of life DVD', 42, 3.14), ('Butter', 13, 2.72)], dtype=t)
print(itemz)
[('Meaning of life DVD', 42, 3.1400001 ) ('Butter', 13, 2.72000003)]
一维数组的索引和切片
import numpy as np
a = np.arange(9) # 创建数组
a
Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])
a[3:7] # 取中段
Out[5]: array([3, 4, 5, 6])
a[:7:2] # 有step
Out[6]: array([0, 2, 4, 6])
a[::-1] # 翻转
Out[7]: array([8, 7, 6, 5, 4, 3, 2, 1, 0])
多维数组的索引和切片
import numpy as np
import pprint
# 将一维数组变为 -> 两个数组,每个数组3行,每行4列(确保几个参数的乘积等于原数组size)
b = np.arange(24).reshape(2, 3, 4)
print(b)
'''
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
'''
print(b.shape) # (2, 3, 4) 维度
print(b[0, 0, 0]) # 0 取第一个数组的第一行第一列
print(b[0, 0]) # [0 1 2 3] 取第一个数组的第一行
print(b[0]) # 取第一个数组
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
'''
print(b[:, 0, 0]) # [ 0 12] 取所有数组的第一行第一列
print(b[:, :, 0]) # 取所有数组的所有行的第一列
'''
[[ 0 4 8]
[12 16 20]]
'''
print(b[:, :, :]) # 同print(b)
print(b[0, :, :]) # 同print(b[0])
print(b[0, ...]) # 同print(b[0])
print('-----------------')
print(b[0, 1]) # [4 5 6 7]
print(b[0, 1, ::2]) # [4 6] 使用间隔
print(b[:, :, 1]) # 取所有数组所有行的第二列
'''
[[ 1 5 9]
[13 17 21]]
'''
print(b[..., 1]) # 同print(b[:, :, 1])
print(b[:, 1]) # 取所有数组的第二行
'''
[[ 4 5 6 7]
[16 17 18 19]]
'''
print(b[0, :, 1]) # 取第一个数组的所有行的第二列 [1 5 9]
print(b[0, :, -1]) # 取第一个数组的所有行的最后一列 [ 3 7 11]
print(b[0, ::2, -1]) # 取第一个数组的所有行的最后一列且使用间隔 [ 3 11]
print(b[0, ::-1, -1]) # 反向取第一个数组的所有行的最后一列 [11 7 3]
print(b[::-1]) # 反向取所有数组
'''
[[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]]
'''
print(b.ravel()) # 展平[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
print(b.flatten()) # 展平[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
b.shape = (6, 4) # 重新设置维度
print(b)
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
'''
print(b.transpose()) # 矩阵转置
'''
[[ 0 4 8 12 16 20]
[ 1 5 9 13 17 21]
[ 2 6 10 14 18 22]
[ 3 7 11 15 19 23]]
'''
b.resize(2, 12) # 类似reshape,但会改变自身数据,而非返回赋值形式
print(b)
'''
[[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23]]
'''
组合
a = np.arange(9).reshape(3, 3)
print(a)
'''
[[0 1 2]
[3 4 5]
[6 7 8]]
'''
b = a * 2
print(b)
'''
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
print(np.hstack((a, b))) # 水平组合
'''
[[ 0 1 2 0 2 4]
[ 3 4 5 6 8 10]
[ 6 7 8 12 14 16]]
'''
print(np.concatenate((a, b), axis = 1)) # 同np.hstack((a, b))
print(np.vstack((a, b))) # 垂直组合
'''
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 0 2 4]
[ 6 8 10]
[12 14 16]]
'''
print(np.concatenate((a, b), axis = 0)) # 同np.vstack((a, b))
print(np.dstack((a, b))) # 深度组合,没搞懂干啥用
'''
[[[ 0 0]
[ 1 2]
[ 2 4]]
[[ 3 6]
[ 4 8]
[ 5 10]]
[[ 6 12]
[ 7 14]
[ 8 16]]]
'''
print('--------------')
c = np.arange(2) # [0 1]
d = c * 2 # [0 2]
print(np.column_stack((c, d))) # 列组合
'''
[[0 0]
[1 2]]
'''
print(np.column_stack((a, b)) == np.hstack((a, b))) # 同np.hstack((a, b))
print(np.row_stack((c, d))) # 行组合
'''
[[0 1]
[0 2]]
'''
print(np.row_stack((a, b)) == np.vstack((a, b))) # 同np.vstack((a, b))
a = np.arange(9).reshape(3, 3)
print(a)
'''
[[0 1 2]
[3 4 5]
[6 7 8]]
'''
pprint.pprint(np.hsplit(a, 3)) # 水平分割
'''
[array([[0],
[3],
[6]]),
array([[1],
[4],
[7]]),
array([[2],
[5],
[8]])]
'''
pprint.pprint(np.split(a, 3, axis=1)) # 同np.hsplit(a, 3)
print('--------')
pprint.pprint(np.vsplit(a, 3)) # 垂直分割
# [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
pprint.pprint(np.split(a, 3, axis=0)) # 同np.vsplit(a, 3)
print('--------')
a = np.arange(27).reshape(3, 3, 3)
pprint.pprint(a)
'''
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
'''
pprint.pprint(np.dsplit(a, 3)) # 深度分割
'''
[array([[[ 0],
[ 3],
[ 6]],
[[ 9],
[12],
[15]],
[[18],
[21],
[24]]]),
array([[[ 1],
[ 4],
[ 7]],
[[10],
[13],
[16]],
[[19],
[22],
[25]]]),
array([[[ 2],
[ 5],
[ 8]],
[[11],
[14],
[17]],
[[20],
[23],
[26]]])]
'''
print('-----------')
# ndim维度,看输出是几个中括号就是几维
# size元素个数
# itemsize元素占用字节数
# nbytes数组总字节数,就是 itemsize 和 size 属性值的乘积
a = np.arange(20) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
print(a.ndim, a.size, a.itemsize) # 1, 20, 4
a.shape = (4, 5)
'''
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
'''
print(a.ndim, a.size, a.itemsize) # 1, 20, 4
a.shape = (2, 2, 5)
'''
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]]
[[10 11 12 13 14]
[15 16 17 18 19]]]
'''
print(a.ndim, a.size, a.itemsize) # 1, 20, 4
print(a.nbytes) # 80
a.shape = (4, 5)
print(a)
'''
[[ 0 5 10 15]
[ 1 6 11 16]
[ 2 7 12 17]
[ 3 8 13 18]
[ 4 9 14 19]]
'''
print(a.T) # 等于转置
'''
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
'''
f = a.flat # 返回numpy.flatiter 对象,用于遍历一维数组形式
print(f) # <numpy.flatiter object at 0x00000034C35B7B70>
for index in f:
print(index) # 逐个打印
print(a.flat[2]) # 2, 直接获取一个数组元素
print(a.flat[[2, 10]]) # [ 2 10] 直接获取多个数组元素
''''
a.flat = 3 # 直接覆盖到整个数组
print(a)
[[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]
[3 3 3 3 3]]
'''
a.flat[[2, 3]] = 99 # 也可用于赋值
print(a)
'''
[[ 0 1 99 99 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
'''
a = np.arange(20).reshape(2, 2, 5)
print(a)
'''
[[[ 0 1 2 3 4]
[ 5 6 7 8 9]]
[[10 11 12 13 14]
[15 16 17 18 19]]]
'''
print(a.tolist()) # 转换成列表
# [[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]]
print(a.astype(str)) # 转换数组时指定数据类型
'''
[[['0' '1' '2' '3' '4']
['5' '6' '7' '8' '9']]
[['10' '11' '12' '13' '14']
['15' '16' '17' '18' '19']]]
'''