numpy[3]

數組的運算

  • 基本運算
運算 對應函數
y = x1 + x2 add(x1, x2 [, y])
y = x1 - x2 subtract(x1, x2 [, y])
y = x1 * x2 multiply (x1, x2 [, y])
y = x1 / x2 divide (x1, x2 [, y]), 如果兩個陣列的元素為整數,那麼用整數除法
y = x1 / x2 true_divide(x1, x2 [, y]), 總是返回精確的商
y = x1 // x2 floor_divide (x1, x2 [, y]), 總是返回商值取整
y = -x negative(x [,y])
y = x1**x2 power(x1, x2 [, y])
y = x1 % x2 remainder(x1, x2 [, y]), mod(x1, x2, [, y])
數學運算
add(x1, x2, /[, out, where, casting, order, …]) 按元素添加參數。
subtract(x1, x2, /[, out, where, casting, …]) 從元素方面減去參數。
multiply(x1, x2, /[, out, where, casting, …]) 元素相乘的論證。
divide(x1, x2, /[, out, where, casting, …]) 以元素方式返回輸入的真正除法。
logaddexp(x1, x2, /[, out, where, casting, …]) 輸入的取冪之和的對數。
logaddexp2(x1, x2, /[, out, where, casting, …]) base-2中輸入的取冪之和的對數。
true_divide(x1, x2, /[, out, where, …]) 以元素方式返回輸入的真正除法。
floor_divide(x1, x2, /[, out, where, …]) 返回小於或等於輸入除法的最大整數。
negative(x, /[, out, where, casting, order, …]) 數字否定,元素方面。
positive(x, /[, out, where, casting, order, …]) 數字正面,元素方面。
power(x1, x2, /[, out, where, casting, …]) 第一個數組元素從第二個數組提升到冪,逐個元素。
remainder(x1, x2, /[, out, where, casting, …]) 返回除法元素的餘數。
mod(x1, x2, /[, out, where, casting, order, …]) 返回除法元素的餘數。
fmod(x1, x2, /[, out, where, casting, …]) 返回除法的元素餘數。
divmod(x1, x2[, out1, out2], / [, out, …]) 同時返回逐元素的商和余數。
absolute(x, /[, out, where, casting, order, …]) 逐個元素地計算絕對值。
fabs(x, /[, out, where, casting, order, …]) 以元素方式計算絕對值。
rint(x, /[, out, where, casting, order, …]) 將數組的元素舍入為最接近的整數。
sign(x, /[, out, where, casting, order, …]) 返回數字符號的元素指示。
heaviside(x1, x2, /[, out, where, casting, …]) 計算Heaviside階躍函數。
conj(x, /[, out, where, casting, order, …]) 以元素方式返回复共軛。
exp(x, /[, out, where, casting, order, …]) 計算輸入數組中所有元素的指數。
exp2(x, /[, out, where, casting, order, …]) 計算輸入數組中所有p的2 ** p。
log(x, /[, out, where, casting, order, …]) 自然對數,元素方面。
log2(x, /[, out, where, casting, order, …]) x的基數為2的對數。
log10(x, /[, out, where, casting, order, …]) 以元素方式返回輸入數組的基數10對數。
expm1(x, /[, out, where, casting, order, …]) 計算數組中的所有元素。exp(x) - 1
log1p(x, /[, out, where, casting, order, …]) 返回一個加上輸入數組的自然對數,逐個元素。
sqrt(x, /[, out, where, casting, order, …]) 以元素方式返回數組的非負平方根。
square(x, /[, out, where, casting, order, …]) 返回輸入的元素方塊。
cbrt(x, /[, out, where, casting, order, …]) 以元素方式返回數組的立方根。
reciprocal(x, /[, out, where, casting, …]) 以元素為單位返回參數的倒數。
gcd(x1, x2, /[, out, where, casting, order, …]) 返回|x1|和的最大公約數|x2|
lcm(x1, x2, /[, out, where, casting, order, …]) 返回|x1|和的最小公倍數|x2|
  • 三角函數
sin(x, /[, out, where, casting, order, …]) 三角正弦,元素。
cos(x, /[, out, where, casting, order, …]) 餘弦元素。
tan(x, /[, out, where, casting, order, …]) 計算切線元素。
arcsin(x, /[, out, where, casting, order, …]) 反正弦,元素。
arccos(x, /[, out, where, casting, order, …]) 三角反餘弦,元素方式。
arctan(x, /[, out, where, casting, order, …]) 三角反正切,逐元素。
arctan2(x1, x2, /[, out, where, casting, …]) x1/x2正確選擇象限的逐元素反正切。
hypot(x1, x2, /[, out, where, casting, …]) 給定直角三角形的“腿”,返回其斜邊。
sinh(x, /[, out, where, casting, order, …]) 雙曲正弦,元素。
cosh(x, /[, out, where, casting, order, …]) 雙曲餘弦,元素。
tanh(x, /[, out, where, casting, order, …]) 計算雙曲正切元素。
arcsinh(x, /[, out, where, casting, order, …]) 逆雙曲正弦元素。
arccosh(x, /[, out, where, casting, order, …]) 反雙曲餘弦,元素。
arctanh(x, /[, out, where, casting, order, …]) 逆雙曲正切元素。
deg2rad(x, /[, out, where, casting, order, …]) 將角度從度數轉換為弧度。
rad2deg(x, /[, out, where, casting, order, …]) 將角度從弧度轉換為度數。
  • 捨入
around(a[, decimals, out]) 均勻舍入到給定的小數位數。
round_(a[, decimals, out]) 將數組舍入到給定的小數位數。
rint(x, /[, out, where, casting, order, …]) 將數組的元素舍入為最接近的整數(四捨五入)。
fix(x[, out]) 向零舍入到最接近的整數。
floor(x, /[, out, where, casting, order, …]) 以元素方式返回輸入的下限(取下限)。
ceil(x, /[, out, where, casting, order, …]) 以元素方式返回輸入的上限(取上限)。
trunc(x, /[, out, where, casting, order, …]) 以元素方式返回輸入的截斷值。
  • 總和,乘積,差異
prod(a[, axis, dtype, out, keepdims, initial]) 返回給定軸上的數組元素的乘積。
sum(a[, axis, dtype, out, keepdims, initial]) 給定軸上的數組元素的總和。
nanprod(a[, axis, dtype, out, keepdims]) 返回給定軸上的數組元素的乘積,將非數字(NaN)視為1。
nansum(a[, axis, dtype, out, keepdims]) 返回給定軸上的數組元素的總和,將非數字(NaN)視為零。
cumprod(a[, axis, dtype, out]) 返回給定軸上元素的累積乘積。
cumsum(a[, axis, dtype, out]) 返回給定軸上元素的累積和。
nancumprod(a[, axis, dtype, out]) 返回給定軸上的數組元素的累積乘積,將非數字(NaN)視為一個。
nancumsum(a[, axis, dtype, out]) 返回給定軸上的數組元素的累積和,將非數字(NaN)視為零。
diff(a[, n, axis]) 計算沿給定軸的第n個離散差。
ediff1d(ary[, to_end, to_begin]) 數組的連續元素之間的差異。
gradient(f, *varargs, **kwargs) 返回N維數組的漸變。
cross(a, b[, axisa, axisb, axisc, axis]) 返回兩個(數組)向量的叉積。
trapz(y[, x, dx, axis]) 使用複合梯形法則沿給定軸積分。
  • 比較運算及邏輯運算
運算子 描述 實例
== 等於 - 比較物件是否相等 (a == b) 返回 False。
!= 不等於 - 比較兩個物件是否不相等 (a != b) 返回 True。
> 大於 - 返回x是否大於y (a > b) 返回 False。
< 小於 - 返回x是否小於y。所有比較運算子返回1表示真,返回0表示假。這分別與特殊的變數True和False等價。注意,這些變數名的大寫。 (a < b) 返回 True。
>= 大於等於 - 返回x是否大於等於y。 (a >= b) 返回 False。
<= 小於等於 - 返回x是否小於等於y。 (a <= b) 返回 True。
運算子 邏輯運算式 描述 實例
and x and y 布林"與" - 如果 x 為 False,x and y 返回 False,否則它返回 y 的計算值。 (a and b) 返回 20。
or x or y 布林"或" - 如果 x 是 True,它返回 x 的值,否則它返回 y 的計算值。 (a or b) 返回 10。
not not x 布林"非" - 如果 x 為 True,返回 False 。如果 x 為 False,它返回 True。 not(a and b) 返回 False
比較運算及邏輯運算
greater(x1,x2,/ [,out,where,cast,…]) 以元素方式返回(x1 > x2)的布林值。
greater_equal(x1,x2,/ [,out,where,…]) 以元素方式返回(x1 >= x2)的布林值。
less(x1,x2,/ [,out,where,cast,…]) 以元素方式返回(x1 < x2)的布林值。
less_equal(x1,x2,/ [,out,where,cast,…]) 以元素方式返回(x1 =< x2)的布林值。
not_equal(x1,x2,/ [,out,where,cast,…]) 以元素方式返回(x1 != x2)的布林值。
equal(x1,x2,/ [,out,where,cast,…]) 以元素方式返回(x1 == x2)的布林值。

警告
不要使用Python關鍵字“and”和“or”來組合邏輯數組表達式。 這些關鍵字將測試整個數組的真值(不是你想像的逐個元素)。 使用按位運算符“&”和“|” 代替。

logical_and(x1,x2,/ [,out,where,…]) 計算x1 and x2元素的布林值。
logical_or(x1,x2,/ [,out,where,cast,…]) 計算x1 or x2元素的布林值。
logical_xor(x1,x2,/ [,out,where,…]) 以元素方式計算x1 xor x2的布林值。
logical_not(x,/ [,out,where,cast,…]) 計算not x元素的布林值。

警告
按位運算符“&”和“|” 是執行逐元素數組比較的正確方法。 確保您理解運算符優先級:“(a> 2)&(a <5)”是正確的語法,因為“a> 2&a <5”將導致錯誤,因為“2&a” 首先評估。

maximum(x1,x2,/ [,out,where,cast,…]) 元素最大數組元素。
minimum(x1,x2,/ [,out,where,cast,…]) 元素最小的數組元素。

提示
Python函數“max()”將在一維數組中找到最大值,但它將使用較慢的序列接口來實現。 maximum ufunc的reduce方法要快得多。 此外,“max()”方法不會給出具有多個維度的數組所期望的答案。 reduce的minimal方法還允許您計算數組的總最小值。
警告
“maximum(a,b)”的行為與“max(a,b)”的行為不同。 作為ufunc,“maximum(a,b)”執行 a和b and 的逐元素比較,根據兩個數組中的哪個元素更大來選擇結果的每個元素。
相反,“max(a,b)”將對象a和b視為一個整體,查看“a> b”的(總)真值,並使用它返回a或b(作為一個整體)。 “最小(a,b)”和“min(a,b)”之間存在類似的差異。

fmax(x1,x2,/ [,out,where,cast,…]) 元素最大數組元素。
fmin(x1,x2,/ [,out,where,cast,…]) 元素最小的數組元素。
  • 位運算
運算子 描述 實例
& 按位與運算子:參與運算的兩個值,如果兩個相應位都為1,則該位的結果為1,否則為0 (a & b) 輸出結果 12 ,二進位解釋: 0000 1100
| 按位或運算子:只要對應的二個二進位有一個為1時,結果位就為1。 (a | b) 輸出結果 61 ,二進位解釋: 0011 1101
^ 按位異或運算子:當兩對應的二進位相異時,結果為1 (a ^ b) 輸出結果 49 ,二進位解釋: 0011 0001
~ 按位取反運算子:對資料的每個二進位位元取反,即把1變為0,把0變為1。~x類似於 -x-1 (~a ) 輸出結果 -61 ,二進位解釋: 1100 0011, 在一個有符號二進位數字的補數形式。
<< 左移動運算子:運算數的各二進位元全部左移若干位元,由"<<"右邊的數指定移動的位元數,高位丟棄,低位元補0。 a << 2 輸出結果 240 ,二進位解釋: 1111 0000
>> 右移動運算子:把">>“左邊的運算數的各二進位元全部右移若干位元,”>>"右邊的數指定移動的位數 a >> 2 輸出結果 15 ,二進位解釋: 0000 1111
bitwise_and(x1, x2, /[, out, where, …]) 逐個元素地計算兩個數組的逐位AND。
bitwise_or(x1, x2, /[, out, where, casting, …]) 逐個元素地計算兩個數組的逐位OR。
bitwise_xor(x1, x2, /[, out, where, …]) 計算兩個數組的逐位XOR元素。
invert(x, /[, out, where, casting, order, …]) 按位元素計算逐位反轉或逐位NOT。
left_shift(x1, x2, /[, out, where, casting, …]) 將整數位移到左側。
right_shift(x1, x2, /[, out, where, …]) 將整數位移到右側。
  • 統計
統計
Order statistics
amin(a[, axis, out, keepdims, initial]) 沿軸返回數組或最小值的最小值。
amax(a[, axis, out, keepdims, initial]) 沿軸返回數組的最大值或最大值。
nanmin(a[, axis, out, keepdims]) 沿軸返回最小數組或最小值,忽略任何NaN。
nanmax(a[, axis, out, keepdims]) 沿軸返回數組的最大值或最大值,忽略任何NaN。
ptp(a[, axis, out, keepdims]) 沿軸的值範圍(最大值 - 最小值)。
percentile(a, q[, axis, out, …]) 計算沿指定軸的數據的第q個百分位數。
nanpercentile(a, q[, axis, out, …]) 計算沿指定軸的數據的第q個百分位數,同時忽略nan值。
quantile(a, q[, axis, out, overwrite_input, …]) 计算沿指定轴的数据的qth分位数…版本添加:: 1.15.0。
nanquantile(a, q[, axis, out, …]) 計算沿指定軸的數據的第q個分位數,同時忽略nan值。
Averages and variances
median(a[, axis, out, overwrite_input, keepdims]) 計算沿指定軸的中位數。
average(a[, axis, weights, returned]) 計算沿指定軸的加權平均值。
mean(a[, axis, dtype, out, keepdims]) 計算沿指定軸的算術平均值。
std(a[, axis, dtype, out, ddof, keepdims]) 計算沿指定軸的標準偏差。
var(a[, axis, dtype, out, ddof, keepdims]) 計算沿指定軸的方差。
nanmedian(a[, axis, out, overwrite_input, …]) 計算沿指定軸的中位數,同時忽略NaN
nanmean(a[, axis, dtype, out, keepdims]) 計算沿指定軸的算術平均值,忽略NaN
nanstd(a[, axis, dtype, out, ddof, keepdims]) 計算沿指定軸的標準偏差,同時忽略NaN
nanvar(a[, axis, dtype, out, ddof, keepdims]) 計算沿指定軸的方差,同時忽略NaN
Correlating
corrcoef(x[, y, rowvar, bias, ddof]) 返回Pearson乘積矩相關係數。
correlate(a, v[, mode]) 兩個1維序列的互相關。
cov(m[, y, rowvar, bias, ddof, fweights, …]) 給定數據和權重,估計協方差矩陣。
Histograms
histogram(a[, bins, range, normed, weights, …]) 計算一組數據的直方圖。
histogram2d(x, y[, bins, range, normed, …]) 計算兩個數據樣本的二維直方圖。
histogramdd(sample[, bins, range, normed, …]) 計算一些數據的多維直方圖。
bincount(x[, weights, minlength]) 計算非負的int數組中每個值的出現次數。
histogram_bin_edges(a[, bins, range, weights]) 僅計算函數使用的bin的邊緣的histogram函數。
digitize(x, bins[, right]) 返回輸入數組中每個值所屬的bin的索引。
  • 線性代數
線性代數(numpy.linalg)
Matrix and vector products
dot(a, b[, out]) 兩個數組的點積。
linalg.multi_dot(arrays) 在單個函數調用中計算兩個或多個數組的點積,同時自動選擇最快的求值順序。
vdot(a, b) 返回兩個向量的點積。
inner(a, b) 兩個數組的內積。
outer(a, b[, out]) 計算兩個向量的外積。
matmul(a, b[, out]) 兩個數組的矩陣乘積。
tensordot(a, b[, axes]) 對於數組> = 1-D,沿指定軸計算張量點積。
einsum(subscripts, *operands[, out, dtype, …]) 評估操作數上的愛因斯坦求和約定。
einsum_path(subscripts, *operands[, optimize]) 通過考慮中間數組的創建,評估einsum表達式的最低成本收縮順序。
linalg.matrix_power(M, n) 將方陣提高到(整數)冪n。
kron(a, b) 兩個陣列的Kronecker產品。
Decompositions
linalg.cholesky(a) Cholesky分解。
linalg.qr(a[, mode]) 計算矩陣的qr分解。
linalg.svd(a[, full_matrices, compute_uv]) 奇異值分解。
Matrix eigenvalues
linalg.eig(a) 計算正方形陣列的特徵值和右特徵向量。
linalg.eigh(a[, UPLO]) 返回Hermitian或對稱矩陣的特徵值和特徵向量。
linalg.eigvals(a) 計算一般矩陣的特徵值。
linalg.eigvalsh(a[, UPLO]) 計算Hermitian或實對稱矩陣的特徵值。
Norms and other numbers
linalg.norm(x[, ord, axis, keepdims]) 矩陣或矢量規範。
linalg.cond(x[, p]) 計算矩陣的條件數。
linalg.det(a) 計算數組的行列式。
linalg.matrix_rank(M[, tol, hermitian]) 使用SVD方法返回陣列的矩陣等級
linalg.slogdet(a) 計算數組行列式的符號和(自然)對數。
trace(a[, offset, axis1, axis2, dtype, out]) 返回數組對角線的總和。
Solving equations and inverting matrices
linalg.solve(a, b) 求解線性矩陣方程或線性標量方程組。
linalg.tensorsolve(a, b[, axes]) 求解x的张量方程“a x = b”。
linalg.lstsq(a, b[, rcond]) 將最小二乘解返回到線性矩陣方程。
linalg.inv(a) 計算矩陣的(乘法)逆。
linalg.pinv(a[, rcond]) 計算矩陣的(Moore-Penrose)偽逆。
linalg.tensorinv(a[, ind]) 計算N維數組的“逆”。
Exceptions
linalg.LinAlgError 由linalg函數引發的通用Python異常派生對象。
  • 離散傅立葉變換
離散傅立葉變換(numpy.fft
Standard FFTs
fft(a[, n, axis, norm]) 計算一維離散傅立葉變換。
ifft(a[, n, axis, norm]) 計算一維離散傅里葉逆變換。
fft2(a[, s, axes, norm]) 計算二維離散傅立葉變換
ifft2(a[, s, axes, norm]) 計算二維逆離散傅立葉變換。
fftn(a[, s, axes, norm]) 計算N維離散傅立葉變換。
ifftn(a[, s, axes, norm]) 計算N維逆離散傅立葉變換。
Real FFTs
rfft(a[, n, axis, norm]) 計算一維離散傅立葉變換用於實際輸入。
irfft(a[, n, axis, norm]) 計算實際輸入的n點DFT的倒數。
rfft2(a[, s, axes, norm]) 計算實數組的二維FFT。
irfft2(a[, s, axes, norm]) 計算實數組的二維逆FFT。
rfftn(a[, s, axes, norm]) 為實際輸入計算N維離散傅立葉變換。
irfftn(a[, s, axes, norm]) 計算實際輸入的N維FFT的逆。
Hermitian FFTs
hfft(a[, n, axis, norm]) 計算具有厄米對稱性的信號的FFT,即實際頻譜。
ihfft(a[, n, axis, norm]) 計算具有厄米對稱性的信號的逆FFT。
Helper routines
fftfreq(n[, d]) 返回離散傅立葉變換採樣頻率。
rfftfreq(n[, d]) 返回離散傅里葉變換採樣頻率(用於rfft,irfft)。
fftshift(x[, axes]) 將零頻率分量移到頻譜中心。
ifftshift(x[, axes]) 逆的fftshift。
  • 排序,搜索,計數
排序,搜索和計數
Sorting
sort(a[, axis, kind, order]) 返回數組的排序副本。
lexsort(keys[, axis]) 使用一系列鍵執行間接排序。
argsort(a[, axis, kind, order]) 返回對數組進行排序的索引。
ndarray.sort([axis, kind, order]) 就地對數組進行排序。
msort(a) 返回沿第一個軸排序的數組副本。
sort_complex(a) 首先使用實部對複雜數組進行排序,然後使用虛部進行排序。
partition(a, kth[, axis, kind, order]) 返回數組的分區副本。
argpartition(a, kth[, axis, kind, order]) 使用kind關鍵字指定的算法沿給定軸執行間接分區。
Searching
argmax(a[, axis, out]) 返回沿軸的最大值的索引。
nanargmax(a[, axis]) 返回指定軸中忽略NaN的最大值的索引。
argmin(a[, axis, out]) 返回沿軸的最小值的索引。
nanargmin(a[, axis]) 返回指定軸中忽略NaN的最小值的索引。
argwhere(a) 查找按元素分組的非零的數組元素的索引。
nonzero(a) 返回非零元素的索引。
flatnonzero(a) 返回a的展平版本中非零的索引。
where(condition, [x, y]) 返回元素,可以是x或y,具體取決於條件。
searchsorted(a, v[, side, sorter]) 查找應插入元素以維護順序的索引。
extract(condition, arr) 返回滿足某些條件的數組元素。
Counting
count_nonzero(a[, axis]) 计算数组a中的非零值的数量。

其他(持續補充)

掩碼

掩碼數組

讀取、儲存

numpy.save
numpy.load
numpy.loadtxt

參考閱讀:
1.https://wizardforcel.gitbooks.io/ts-numpy-tut/content/
2.https://docs.scipy.org/doc/numpy-1.14.0/index.html

猜你喜欢

转载自blog.csdn.net/qq_33768941/article/details/86512759
今日推荐