"""
numpy下的常見方法:
all,any,argmax, argmin,argpartition,argsort,choose, clip,
compress,copy,cumprod, cumsum,diagonal,imag,max, mean,min,
nonzero,partition, prod,ptp,put,ravel,real, repeat,reshape,
round, searchsorted,sort,squeeze,std, sum,swapaxes,take,trace,
transpose,var。
ndarray屬性:
ndarray.flags有關陣列的內存佈局的信息。
ndarray.shape數組維度的元組。
ndarray.strides在遍歷數組時在每個維度上步進的字節元組。
ndarray.ndim數組維數。
ndarray.data指向數組數據開頭的Python緩衝區對象。
ndarray.size數組中元素的數量。
ndarray.itemsize一個數組元素的長度(以字節爲單位)。
ndarray.nbytes數組元素消耗的總字節數。
ndarray.base如果內存來自其他對象,則爲基礎對象。
ndarray.dtype數組元素的數據類型。
ndarray.T轉置數組。
ndarray.real數組的實部。
ndarray.imag數組的虛部。
ndarray.flat數組上的一維迭代器。
ndarray.ctypes一個用於簡化數組與ctypes模塊的交互的對象。
ndarray轉換:
ndarray.item(*args)將數組的元素複製到標準Python標量並返回。
ndarray.tolist()將數組作爲a.ndimPython標量的-levels深度嵌套列表返回。
ndarray.itemset(*args)將標量插入數組中(如果可能,將標量強制轉換爲數組的dtype)
ndarray.tobytes([order])在數組中構造包含原始數據字節的Python字節。
ndarray.tofile(fid[, sep, format])將數組以文本或二進制形式寫入文件(默認)。
ndarray.dumps()以字符串形式返回數組的泡菜。
ndarray.astype(dtype[, order, casting, …])數組的副本,強制轉換爲指定的類型。
ndarray.byteswap([inplace])交換數組元素的字節
ndarray.copy([order])返回數組的副本。
ndarray.view([dtype, type])具有相同數據的數組的新視圖。
ndarray.getfield(dtype[, offset])以給定類型返回給定數組的字段。
ndarray.fill(value)用標量值填充數組。
ndarray形狀操作:
ndarray.reshape(shape[, order])返回一個數組,其中包含具有相同形狀的相同數據。
ndarray.resize(new_shape[, refcheck])就地更改數組的形狀和大小。
ndarray.transpose(*axes)就地更改數組的形狀和大小。
ndarray.swapaxes(axis1, axis2)返回軸1和軸2互換的數組視圖。
ndarray.flatten([order])返回摺疊成一維的數組副本。
ndarray.ravel([order])返回一個展平的數組。
ndarray.squeeze([axis])從形狀除去單維輸入一個。
ndarray選擇和操作:
ndarray.take(indices, axis=None, out=None, mode='raise')返回來自的元素構成的數組一個在給定的索引。
ndarray方法:
ndarray.max([axis, out, keepdims, initial, …])沿給定軸返回最大值。
ndarray.argmax([axis, out])沿給定軸返回最大值的索引。
ndarray.min([axis, out, keepdims, initial, …])沿給定軸返回最小值。
ndarray.argmin([axis, out])返回最小值的索引沿給定軸線一個。
ndarray.ptp([axis, out, keepdims])沿給定軸的峯到峯(最大值-最小值)值。
ndarray.clip([min, max, out])返回值限制爲的數組。[min, max]
ndarray.conj()將所有元素複數共軛
ndarray.round([decimals, out])返回一個與舍入到小數的給定數目的每個元素。
ndarray.trace([offset, axis1, axis2, dtype, out])返回數組對角線的和。
ndarray.sum([axis, dtype, out, keepdims, …])返回給定軸上數組元素的總和。
ndarray.cumsum([axis, dtype, out])返回沿給定軸的元素的累加和。
ndarray.mean([axis, dtype, out, keepdims])返回沿給定軸的數組元素的平均值。
ndarray.var([axis, dtype, out, ddof, keepdims])返回沿給定軸的數組元素的方差。
ndarray.std([axis, dtype, out, ddof, keepdims])返回沿給定軸的數組元素的標準偏差。
ndarray.prod([axis, dtype, out, keepdims, …])返回給定軸上數組元素的乘積
ndarray.cumprod([axis, dtype, out])返回沿給定軸的元素的累積積。
ndarray.all([axis, out, keepdims])如果所有元素的評估結果爲True,則返回True。
ndarray.any([axis, out, keepdims])如果任何元素,則返回true 一個評估爲True。
"""
<class 'numpy.ndarray'>
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
(2, 3)
(12, 4)
2
<memory at 0x00000188A8DCAE48>
6
4
24
int32
<numpy.core._internal._ctypes object at 0x00000188A9E152C8>
[[1 4]
[2 5]
[3 6]]
[[1 2 3]
[4 5 6]]
[[0 0 0]
[0 0 0]]
import numpy as np
x=np.array([[1,2,3],[4,5,6]],np.int32)
print(x)
'''
[[1 2 3]
[4 5 6]]
'''
print(x.flags)
'''
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
'''
print(x.shape)
'''
(2, 3)
'''
print(x.strides)
'''
(12, 4)
'''
print(x.ndim)
'''
2
'''
print(x.data)
'''
<memory at 0x0000025B7C2A9C88>
'''
print(x.size)
'''
6
'''
print(x.itemsize)
'''
4
'''
print(x.nbytes)
'''
24
'''
print(x.dtype)
'''
int32
'''
print(x.ctypes)
'''
<numpy.core._internal._ctypes object at 0x0000025B7D2BDF88>
'''
print(x.T)
'''
[[1 4]
[2 5]
[3 6]]
'''
print(x.real)
'''
[[1 2 3]
[4 5 6]]
'''
print(x.imag)
'''
[[0 0 0]
[0 0 0]]
'''
import numpy as np
x=np.array([[1,2,3],[4,5,6]],np.int32)
print(x.item(1))
'''
2
'''
print(x.item((1,2)))
'''
6
'''
print(x.tolist())
print(type(x.tolist()))
'''
[[1, 2, 3], [4, 5, 6]]
<class 'list'>
'''
x.itemset(5,9)
print(x)
'''
[[1 2 3]
[4 5 9]]
'''
print(x.tobytes())
'''
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00'
'''
x.tofile("x.bin")
tmp=np.fromfile("x.bin",dtype=np.int32)
print(tmp)
'''
[1 2 3 4 5 6]
'''
tmp=x.astype(np.int64)
print(tmp.dtype)
'''
int64
'''
tmp=x.copy()
print(tmp)
tmp[0]=99
print(tmp)
print(x)
'''
[[1 2 3]
[4 5 6]]
[[99 99 99]
[ 4 5 6]]
[[1 2 3]
[4 5 6]]
'''
tmp=x.view()
print(tmp)
tmp[0]=99
print(tmp)
print(x)
'''
[[1 2 3]
[4 5 6]]
[[99 99 99]
[ 4 5 6]]
[[99 99 99]
[ 4 5 6]]
'''
map(hex,x)
print(x)
print(x.byteswap(True))
'''
[[1 2 3]
[4 5 6]]
[[ 16777216 33554432 50331648]
[ 67108864 83886080 100663296]]
'''
print(x)
x.fill(0)
print(x)
'''
[[1 2 3]
[4 5 6]]
[[0 0 0]
[0 0 0]]
'''
import pickle
tmp=x.dumps()
print(type(x))
print(type(tmp))
print(pickle.loads(tmp))
'''
<class 'numpy.ndarray'>
<class 'bytes'>
[[1 2 3]
[4 5 6]]
'''
x2 = np.diag([1.+1.j]*2)
x2[1, 1] = 2 + 4.j
print(x2)
print(type(x2))
print(x2.dtype)
print(x2[0,0].itemsize)
print(x2.getfield(np.float64))
print(x2.getfield(np.float64,offset=8))
'''
[[1.+1.j 0.+0.j]
[0.+0.j 2.+4.j]]
<class 'numpy.ndarray'>
complex128
16
[[1. 0.]
[0. 2.]]
[[1. 0.]
[0. 4.]]
'''
print()
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[[ 0 5 10]
[ 4 9 3]
[ 8 2 7]
[ 1 6 11]]
'''
'''
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
'''
'''
[[ 0 4 8]
[ 1 5 9]
[ 2 6 10]
[ 3 7 11]]
[[[ 0 8]
[ 1 9]
[ 2 10]
[ 3 11]]
[[ 4 12]
[ 5 13]
[ 6 14]
[ 7 15]]]
[[[ 0 4]
[ 1 5]
[ 2 6]
[ 3 7]]
[[ 8 12]
[ 9 13]
[10 14]
[11 15]]]
'''
'''
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]]
[0 1 2]
'''
print()
a = np.arange(1,6)
indices = [0, 1, 4]
print(np.take(a, indices))
print(a[indices])
indices=[[0, 1], [3, 4]]
print(np.take(a, indices))
'''
[1 2 5]
[1 2 5]
[[1 2]
[4 5]]
'''
a = np.arange(5)
np.put(a, [0, 2], [-44, -55])
print(a)
b = np.arange(5)
np.put(b, 22, -5, mode='clip')
print(b)
'''
[1 2 5]
[1 2 5]
[[1 2]
[4 5]]
[-44 1 -55 3 4]
[ 0 1 2 3 -5]
'''
print(np.repeat(3, 4))
x = np.array([[1,2],[3,4]])
print(np.repeat(x, 2))
print(np.repeat(x, 3, axis=1))
print(np.repeat(x, [1, 2], axis=0))
'''
[3 3 3 3]
[1 1 2 2 3 3 4 4]
[[1 1 1 2 2 2]
[3 3 3 4 4 4]]
[[1 2]
[3 4]
[3 4]]
'''
choices = [[0, 1, 2, 3], [10, 11, 12, 13],[20, 21, 22, 23], [30, 31, 32, 33]]
choices=np.array(choices)
print(choices)
print(np.choose([2, 3, 1, 0], choices))
print(np.choose([2, 4, 1, 0], choices, mode='clip'))
'''
[[ 0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]]
[20 31 12 3]
[20 31 12 3]
'''
a = np.array([[1,4],[3,1]])
print(np.sort(a, axis=None))
print(np.sort(a, axis=0))
print(np.sort(a, axis=1))
'''
[1 1 3 4]
[[1 1]
[3 4]]
[[1 4]
[1 3]]
'''
x = np.array([3, 1, 2])
print(np.argsort(x))
x = np.array([[0, 3], [1, 2]])
print(np.argsort(x, axis=0))
print(np.argsort(x, axis=1))
'''
[1 2 0]
[[0 1]
[1 0]]
[[0 1]
[0 1]]
'''
print(np.searchsorted([1,2,3,4,5], 3))
print(np.searchsorted([1,2,3,4,5], 3, side='right'))
print(np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]))
'''
2
3
[0 5 1 2]
'''
x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
tmp=np.nonzero(x)
print(tmp[0])
print(tmp[1])
print(np.transpose(np.nonzero(x)))
print(x[np.nonzero(x)])
'''
[0 1 2 2]
[0 1 0 1]
[[0 0]
[1 1]
[2 0]
[2 1]]
[3 4 5 6]
'''
a = np.array([[1, 2], [3, 4], [5, 6]])
print(np.compress([0, 1], a, axis=0))
print(np.compress([False, True], a, axis=0))
print(np.compress([0, 1], a, axis=1))
print(np.compress([False, True], a, axis=1))
print(np.compress([0, 1, 1], a, axis=0))
print(np.compress([False, True, True], a, axis=0))
'''
[[3 4]]
[[3 4]]
[[2]
[4]
[6]]
[[2]
[4]
[6]]
[[3 4]
[5 6]]
[[3 4]
[5 6]]
'''
a = np.arange(9).reshape(3,3)
print(a)
print(a.diagonal())
print(a.diagonal(1))
print(a.diagonal(2))
'''
[[0 1 2]
[3 4 5]
[6 7 8]]
[0 4 8]
[1 5]
[2]
'''
a = np.arange(27).reshape(3,3,3)
print(a)
print(a.diagonal(0))
print(a.diagonal(1))
print(a.diagonal(2))
'''
[[[ 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]]]
[[ 0 12 24]
[ 1 13 25]
[ 2 14 26]]
'''
print()