標籤:

np ufunc: reduce, accumulate, at; np.add, multiply, sum

import numpy as npnfrom inspect import getmodule, getsourcelinesnndef conv_single_step(a_slice_prev, W, b):n s = np.multiply(W, a_slice_prev) + bn Z = np.sum(s)nn return Znnnp.random.seed(1)na_slice_prev = np.random.randn(4, 4, 3)nW = np.random.randn(4, 4, 3)nb = np.random.randn(1, 1, 1)nZ = conv_single_step(a_slice_prev, W, b)nprint("Z =", Z)nnnp.add # ufunc?nnp.multiplynnp.sum # normal py funcnngetmodule(np.multiply) # 沒有python module!ngetmodule(np.add) # 沒有python module!ngetmodule(np.sum)nngetsourcelines(np.add) # ufunc is too special to show sourcengetsourcelines(np.sum) # oknnnp.info(np.add) # correct way to see np.add ufunc docsnnp.info(np.multiply)nnhelp(np.add)ndir(np.add) # ufunc class methodsnhelp(np.add.reduce)nhelp(np.add.accumulate)na = np.linspace(1,6,6)nb = a.reshape((3,2))nnp.add.accumulate(b, axis=0)nnp.add.accumulate(b, axis=1)nnhelp(np.add.at)na = np.array([1,2,3,4])nnp.add.at(a, [0,1,2,2], 2)nanna = np.array([1,2,3,4])nnp.add.at(a, [0,1,2], [2,4,5]) # index array must match b arraynannhelp(np.sum)nnp.sum([0.5, 0.7, 0.2, 1.5])nnp.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)nnp.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int64)nnp.sum([0.5, 0.7, 0.2, 1.5], dtype=np.float32)nnp.sum([0.5, 0.7, 0.2, 1.5], dtype=np.float64)nnp.ones(128)nnp.ones(128, dtype=np.int8)nnp.ones(128, dtype=np.int8).sum(dtype=np.int8)nnp.ones(128, dtype=np.int8).sum(dtype=np.int32)n

推薦閱讀:

數組數據類型轉換及算術運算
Python C Extesion (pyd)
復盤:隨機漫步
Python 2.7安裝常用的科學計算分析包

TAG:numpy |