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 Syntax 
Python :: Python NumPy atleast_2d Function Example 
Python :: Python NumPy transpose Function Example in one line of code 
Python :: pathlib home 
Python :: seasonal plot python time series 
Python :: geopandas nc file 
Python :: manipulate sns legend 
Python :: tqdm start bar at 
Python :: Python NumPy asfarray Function Syntax 
Python :: Python NumPy concatenate Function Example when axis equal to none 
Python :: django on-delete options 
Python :: catch all event on socketio python 
Python :: python increase a value every n rows 
Python :: python __truediv__ 
Python :: how to fetch limited rows in pandas dataframe using sqlalchemy 
Python :: simpy 
Python :: NumPy bitwise_or Code When inputs are Boolean 
Python :: django view - apiview decorator (retrieve, update or delete - GET, PUT, DELETE) 
Python :: # find all text files in directory or any type of files in directory 
Python :: penggunaan len di python 
Python :: python code to java code converter 
Python :: displaying print output in a textbox 
Python :: map reduce and filter functions in python 
Python :: make python present number in sciencetifc 
Python :: python 3.9.7 
Python :: cuenta atras segundero python 
Python :: EDA dataframe get missing and zero values 
Python :: ring Create Lists 
Python :: for loop the string from reverse order and skipping last element in string python 
Python :: install open3d jetson nano aarch64 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =