python-numpy
来源:互联网 发布:百度云域名转出 编辑:程序博客网 时间:2024/05/18 06:28
1.sum
a
1, 2 | axis 13, 4 |--------| axis 0
a.sum(axis=0)
array([4, 6])
a.sum(axis=1)
array([3, 7])
2. np.square
每个元素
3. 加维度
a=[1,2]
aa=a[:,None]
aa.shape=(2,1)
b=[1,2,3]
bb=b[None,:]
bb.shape=(1,3)
4. 两个基底构成矩阵
aa+bb
1,2,3 ------- 1 | 2,3,4 2 | 3,4,5
5. 计算距离矩阵(norm2, cosine)
a : array_like An NxM matrix of N samples of dimensionality M.b : array_like An LxM matrix of L samples of dimensionality M.Returns a matrix of size len(a), len(b), ret[i][j] contains the squared distance between `a[i]` and `b[j]`.
def norm2_mat(a,b): a2,b2=np.square(a).sum(axis=1),np.square(b).sum(axis=1) r2=a2[:,None]+b2[None,:] -2*np.dot(a,b.T) return r2
等价于
def norm2(a,b): n,m=a.shape l,m=b.shape ret=np.zeros((n,l),dtype=np.float) for i in range(n): for j in range(l): ret[i][j]=np.square(a[i]-b[j]).sum() return ret
cosine_mat
def _cosine_distance(a, b, data_is_normalized=False):
if not data_is_normalized:
a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True)
b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True)
return 1. - np.dot(a, b.T)
阅读全文
0 0
- Python Numpy
- [python]numpy
- python numpy
- Python Numpy
- python numpy
- python ---numpy
- Python---Numpy
- Python--numpy
- Python Numpy
- python numpy
- python numpy
- Python numpy
- Python-numpy
- python-numpy
- python numpy
- python-numpy
- python numpy
- Python-NumPy
- 故障排除:无法启动、访问或连接到 Azure 虚拟机上运行的应用程序
- windows配置FFmpeg注意要点
- 实现MyBatis Mapper XML文件增量动态刷新,自动加载,热加载,热部署
- C#中的WebBrowser控件的使用
- Java基础知识点
- python-numpy
- C语言入门第八篇,if语句练习题答案
- 1.1总说明
- ECharts曲线图形显示例子
- 音视频相关研究-雷宵骅在读博士的项目
- Bootstrap中的datetimepicker用法总结
- 【项目实战】---地址定位(百度地图)
- 1. Grails-web.xml
- ECMAscript6快速入门-函数的扩展