Python Numpy Tutorials: Broadcasting
来源:互联网 发布:开户银行数据 编辑:程序博客网 时间:2024/05/25 01:36
Broadcasting typically makes your code more concise and faster, so you should strive to use it where possible.
# -*- coding: utf-8 -*-"""Python Version: 3.5Created on Fri May 12 12:50:49 2017E-mail: Eric2014_Lv@sjtu.edu.cn@author: DidiLv"""import numpy as np# Compute outer product of vectorsv = np.array([1,2,3]) # v has shape (3,)w = np.array([4,5]) # w has shape (2,)# To compute an outer product, we first reshape v to be a column# vector of shape (3, 1); we can then broadcast it against w to yield# an output of shape (3, 2), which is the outer product of v and w:# [[ 4 5]# [ 8 10]# [12 15]]print(np.reshape(v, (3, 1)) * w)x = np.array([[1,2,3], [4,5,6]])# x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3),# giving the following matrix:# [[2 4 6]# [5 7 9]]print(x + v) #这里注意reshape()函数调用并不改变原数组的形状print((x.T + w).T)# Another solution is to reshape w to be a row vector of shape (2, 1);# we can then broadcast it directly against x to produce the same# output.print(x + np.reshape(w, (2, 1)))# Multiply a matrix by a constant:# x has shape (2, 3). Numpy treats scalars as arrays of shape ();# these can be broadcast together to shape (2, 3), producing the# following array:# [[ 2 4 6]# [ 8 10 12]]print(x * 2)
0 0
- Python Numpy Tutorials: Broadcasting
- Python Numpy Tutorials: 集合
- Python Numpy Tutorials: 函数
- Python Numpy Tutorials: 类
- Python Numpy Tutorials: 数组
- Python Numpy Tutorials: 数据类型
- numpy broadcasting
- numpy Broadcasting
- Python库numpy中的Broadcasting机制解析
- Python Numpy Tutorials: 元组
- Python Numpy Tutorials: 数组--1
- Python Numpy Tutorials: 数组--2
- Python Numpy Tutorials: 数组--3
- Python Numpy Tutorials: 数组--4
- Python的numpy中的 broadcasting(广播)机制
- Python Numpy Tutorials: 改变数组的维度
- Python Scipy&NUmpy Tutorials:Scipy与Matplot
- Numpy中的广播(Broadcasting)
- PHP生成短链接方法
- HDU 2504 JAVA
- Android缓存数据清理
- Android 实现微信分享好友和朋友圈
- Fix "the ECDSA host key for 'your server' differs from the key for the IP address 'the ip'"
- Python Numpy Tutorials: Broadcasting
- 设计模式:抽象集合(组合,迭代器)
- ANDROID设计模式之单例模式的五种实现方式
- HDU 2520 JAVA
- 常用的正则表达式
- 在Windows上安装TensorFlow官方文档——翻译
- socket返回10013
- 导航控制器 导航栏的基本使用
- 网络流dinic模板