--------------------numpy-------------------setup--------
a = [ [1,2], [1,3] ] #list
b = np.array(a, dtype=float) #ndarray -- array是一个函数 参数为一个python序列对象
c = np.arange(start, end, step)
d = np.linspace(start, end, num)
np.random.randint(l,h,size=())
np.random.randn(r,c) N(0,1) #For random samples from , use: sigma*np.random.randn(...)+mu
random.shuffle()
def func(i, or i,j): return i^2, or i*j
np.frommfuction(func, (3,)or (i,j) )
np.tile(ndarray,( i, j ) #堆砌ndarray(看出一个元素), i 行, j列
numpy.vstack( (ndarray-a, ndarray-b) )
--------------------np.array.shape---------------------------
c = np.array()
c.shape = 3, -1
d = c.reshape( (3, -1))
-----------------------------std------average---------------------------------------------------
r = np.random.binomial(1000, 0.8, 1000) # 生成二项分布的随机数
std = np.std(r) # 计算标准差
avg = np.average(r) # 计算平均数
np.max(x,0 or 1) np.min(x,0 or 1)
f = r[r>=(avg-std)] # 选出落在一个平均值标准差范围内的元素
f = f[f<=(avg+std)]
-----------------------numpy.linalg-----------------
cos距离
norm = np.linalg.norm(sumx[1]) * np.linalg.norm(sumx[3]) #norm L2范数
dis = np.dot(sumx[1].transpose(), sumx[3]) / norm
dis = 0.5 + 0.5*dis #归一化
print dis
欧式距离
dist = linalg.norm(A - B)
sim = 1.0 / (1.0 + dist) #归一化
------------------------------np.pad----------------------------------
np.pad(ndarr, ( (1,2), (2,1) ), mode) # axis-i (begine,end) mode- 'constant', 'edge' ...
np.pad(ndarray, (1,3), 'constant', constant_values=(0,0) )
http://docs.scipy.org/doc/numpy/reference/generated/numpy.pad.html
------------------save---load--------------
np.save('path-to-save.npy', ndarray)
ndarray = np.load('path')
np.savetxt('path-to-save.txt'., ndarray) # 1 or 2
ndarray = np.loadxt('path')
---------------large-data-than-memory------------------
numpy.memmap( ) # similar to np.array( )
PyTables hdf5
-----------------random----------------------
np.random.shuffle(ndarray)
idx = np.random.permutation(n) #np.range(n)
a[idx,:] ...
------------------------numpy--label---------broadcast----------------------------------
labels = [1 2 3 4 5 6] ....
re = (numpy.arange(label_num) == labels[ : , None]).astype(numpy.float32)
--------------------reference---------------------
http://old.sebug.net/paper/books/scipydoc/numpy_intro.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html
http://stackoverflow.com/questions/1053928/very-large-matrices-using-python-and-numpy