Can you use 3 seperate 1D numpy arrays to manipulate a 3D array using vectorization?

Rob Lasch

I am trying multiply a specific location of an array by certain value, where the location is determined by the index and the value of the num array. The certain value comes from the same index position of the multiplier array. We only want to apply this multiplier if the needs_multiplier is value at that index position is true. I think the code will do a better job explaining this. I am trying to vectorize this and avoid the for loop.

import numpy as np

data = np.array([[[ 2.,  2.,  2.,  2.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.]],

                 [[ 1.,  1.,  1.,  1.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.]],

                 [[ 3.,  3.,  3.,  3.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.]],

                 [[ 5.,  5.,  5.,  5.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.],
                  [ 0.,  0.,  0.,  0.]]])

needs_multiplier = np.array([True, True, False, True])
num = np.array([1, 2, 2, 3])
multipler = np.array([0.5, 0.6, 0.2, 0.3])


for i, cfn in enumerate(num):
    if needs_multiplier[i]:
        data[i, 1, cfn] = multipler[i] * data[i, 0, cfn]
        data[i, 2, cfn] = data[i, 0, cfn]-data[i, 1, cfn]

print(data) # this is the result I am looking for

[[[2.  2.  2.  2. ]
  [0.  1.  0.  0. ]
  [0.  1.  0.  0. ]
  [0.  0.  0.  0. ]]

 [[1.  1.  1.  1. ]
  [0.  0.  0.6 0. ]
  [0.  0.  0.4 0. ]
  [0.  0.  0.  0. ]]

 [[3.  3.  3.  3. ]
  [0.  0.  0.  0. ]
  [0.  0.  0.  0. ]
  [0.  0.  0.  0. ]]

 [[5.  5.  5.  5. ]
  [0.  0.  0.  1.5]
  [0.  0.  0.  3.5]
  [0.  0.  0.  0. ]]]
user7138814

num can be used as index array after selecting "active" values with num[needs_multiplier]

Then vectorizing the expressions is pretty straight forward:

b = needs_multiplier
num_b = num[needs_multiplier]

data[b, 1, num_b] = multipler[b] * data[b, 0, num_b]
data[b, 2, num_b] = data[b, 0, num_b] - data[b, 1, num_b]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a way to manipulate a 3D array with a 1D using vectorization?

Create a 3D numpy array with 3 2D numpy arrays or 3 1D numpy array

multiply 3D array with 1D array in numpy

NumPy: Concatenating 1D array to 3D array

Reshape 1d array to 3d array numpy

Slice a 3d numpy array using a 1d lookup between indices

How can I concatenate a list of 2d numpy arrays into a 3d numpy array?

expand and copy 1D numpy array to 3D

Vectorization of summation of multipication of a 1D vector and a 3D array

How can i shift a 2d array by an 1d array in numpy? The same for 3d by 2d

Creating a numpy array of 3D coordinates from three 1D arrays, first index changing fastest

Changing subarrays in 3D arrays using values of a 2D array using numpy

How can I scale a set of 2D arrays (3D array) by a 2D array in a vectorized way using NumPy?

combine 3d arrays into a 4d array in numpy

Numpy reshape array of arrays to 1D

Reshape 2D numpy array into 3 1D arrays with x,y indices

numpy 3D array vectorized access with arrays of indices

List of 3D Numpy Arrays to Array Conversion

Filter 2d arrays containing a 1d array inside a 3d array

How to insert numpy 1D array to numpy 3D array?

Efficient conversion of a 3D numpy array to a 1D numpy array

How to compose a 3d array from 3 1d arrays in MATLAB?

Split a 3D numpy array into smaller 3D arrays

How to pad a numpy 3D array (or torch tensor) with values from surrounding 3D arrays

Plot a 3D point using matplotlib and arrays with numpy in Python

Efficient sum of Gaussians in 3D with NumPy using large arrays

Numpy: assemble three 1D arrays into 3D (but not exactly a simple coordinate grid)

Can numpy create n arrays by summing n elements in 1d array to each element of another 1d array?

Can you use .index for 2D arrays (without using numpy)?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive