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))
Python3-筆記-numpy學習指南-002-基礎