python-numpy-00

--------------------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 N(\mu, \sigma^2), 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值