Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Algorithm of Broadcasting with NumPy Arrays

p = max(m, n)
if m < p:
    left-pad A's shape with 1s until it also has p dimensions

else if n < p:
    left-pad B's shape with 1s until it also has p dimensions
result_dims = new list with p elements

for i in p-1 ... 0:
    A_dim_i = A.shape[i]
    B_dim_i = B.shape[i]
    if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i:
        raise ValueError("could not broadcast")
    else:
        # Pick the Array which is having maximum Dimension
        result_dims[i] = max(A_dim_i, B_dim_i)
Comment

Broadcasting with NumPy Arrays Example

# welcome to softhunt.net
import numpy as np

v = np.array([23, 3, 43])
w = np.array([15, 5])

# To compute an outer product we first
# reshape v to a column vector of shape 3x1
# then broadcast it against w to yield an output
# of shape 3x2 which is the outer product of v and w
print(np.reshape(v, (3, 1)) * w, "
")

X = np.array([[42, 3, 21], [5, 32, 32]])

# x has shape 2x3 and v has shape (3, )
# so they broadcast to 2x3,
print(X + v, "
")

# Add a vector to each column of a matrix X has
# shape 2x3 and w has shape (2, ) If we transpose X
# then it has shape 3x2 and can be broadcast against w
# to yield a result of shape 3x2.

# Transposing this yields the final result
# of shape 2x3 which is the matrix.
print((X.T + w).T, "
")

# Another solution is to reshape w to be a column
# vector of shape 2X1 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 2x3.
# Numpy treats scalars as arrays of shape();
# these can be broadcast together to shape 2x3.
print(X * 2)
Comment

PREVIOUS NEXT
Code Example
Python :: Python NumPy atleast_3d Function Example 2 
Python :: Python NumPy atleast_2d Function Example when inputs are in high dimension 
Python :: Python NumPy ndarray.T Example 
Python :: Python NumPy ndarray flat function Example 
Python :: jupyter extension 4 
Python :: python terminal color 
Python :: pypi autopep8 
Python :: differences between Pool.apply, Pool.apply_async, Pool.map and Pool.map_async. 
Python :: Python NumPy asfortranarray Function List to an array 
Python :: Python NumPy block Function Syntax 
Python :: python function arguments multiple lines 
Python :: Python NumPy append Function Example Working with axis 
Python :: unsupported operand type python 
Python :: Python __le__ 
Python :: python model feature importance 
Python :: how to nest try/except statements 
Python :: NumPy bitwise_xor Code When inputs are arrays 
Python :: ROS subscribes to image type video frames (Python) through topic Publishing 
Python :: turn dictionary into flat list 
Python :: penggunaan keys di python 
Python :: create loop python 
Python :: How to use a <ComboboxSelected virtual event with tkinter 
Python :: pydantic model from dataclass 
Python :: heatmap colorbar label 
Python :: how to add start menu in python 
Python :: Converting Data Types 
Python :: check if id is present in elasticsearch using python 
Python :: ring Search List Item 
Python :: word cloud mape python 
Python :: Use miraculous with enviroment variable token 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =