1. 程式人生 > >NumPy 陣列方法

NumPy 陣列方法

– Start 下面是 NumPy 提供的一些方法,更多方法參見**官網**。

import numpy as np

a = np.fromfunction(lambda x, y: 10*x+y, (5, 4), dtype=int)
print(a)


# 統計
print(f'minimum of an array: {np.min(a)}')
print(f'minimum along an axis: {np.min(a, axis=1)}')

print(f'maximum of an array: {np.max(a)}')
print(f'maximum along an axis: {np.max(a, axis=1)}')

print(f'Sum of an array: {np.sum(a)}')
print(f'Sum over a given axis: {np.sum(a, axis=1)}')

print(f'average of an array: {np.average(a)}')
print(f'average over a given axis: {np.average(a, axis=1)}')


# 測試真假
print(f'are all elements true? {np.all(a)}')
print(f'is each row true? {np.all(a, axis=1)}')

print(f'is any element true? {np.any(a)}')
print(f'is any element true for each row? {np.any(a, axis=0)}')


# 查詢元素
print(a[np.nonzero(a)])
print(a[np.where(a > 11)])
print(np.where(a > 11, a, -1))


# 四捨五入函式
x = np.array([[1.3, 2.5, 3.7], [-4.2, -5.5, -6.7]])
print(np.ceil(x))
print(np.round(x))
print(np.floor(x))

– 更多參見: – 聲 明:轉載請註明出處 – Last Updated on 2018-10-21 – Written by ShangBo on 2018-10-21 – End