I want to describe the relevant part written in the comment of the following program without using for.
Please tell me how to write something else that doesn't seem to be slow!
import numpy as np def test (value): return value + 1, value + 2, value + 3, value + 4 A = np.random.rand (300, 900) B = np.zeros ((300,900,4)) # I want you to teach me another way to write the following without using a for statement! for i in range (A.shape ): for j in range (A.shape ): B [i] [j] = test (A [i] [j])
Answer # 1
If you use numpy, it's easier to sum the arrays
The n iterations of a random array a with shape (x, y) are A
The (x, y) iteration of array b from 1 to n is B
The result adds it.
import numpy as np n = 4 # Maximum number to add to value in test x = 2 # Originally 300 y = 3 # Originally 900 a = np.random.rand (y, x) A = np.tile (a, (n, 1, 1)). T b = np.array ([i + 1 for i in range (n)]) B = np.tile (b, (x, y, 1)) print (A + B) # [[[1.75150523 2.75150523 3.75150523 4.75150523] # [1.21935891 2.21935891 3.21935891 4.21935891] # [1.16490574 2.16490574 3.16490574 4.16490574]] # [[1.73834419 2.73834419 3.73834419 4.73834419] # [1.58224169 2.58224169 3.58224169 4.58224169] # [1.48666611 2.48666611 3.48666611 4.48666611]]]
Answer # 2
If you want to use a function called test:
import numpy as np def test (value): return value + 1, value + 2, value + 3, value + 4 A = np.random.rand (300, 900) B = np.zeros ((300,900,4)) B [:,:, 0], B [:,:, 1], B [:, :, 2], B [:, :, 3] = test (A)
- please explain the function using the python dictionary
- python - process without using the same formula twice
- python - please tell me the reason for using * in the print function other than the calculation formula
- please tell me how to write a program to end when the result is executed 5 times [python]
- python 3x - i want to get the nth array with an argument using python3 argparse
- parameter estimation using python's weighted least squares method (wls)
- parallel processing using python multiprocessingpool and multiprocessingqueue does not work well
- about external libraries when using multiple versions of python
- python - i want to separate by a specific word using the split function
- python - image recognition using cnn keras multiple inputs
- python 3x - best estimator: i get an error with no syntax, so please tell me what to do
- please tell me about the role of python tag = "mychr"
- about batch change of file name using python
- python - read and write values from another file
- please tell me how to see only the result without compiling with vs code of windows
- i don't understand the exercises using python trigonometric functions
- python - (tensorflow) please teach me how to connect labels and indexes
- (python) input () if time elapses without input, skip input () and move to the next process what can i do?
- python - about write loop to csv
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- i want to call a child component method from a parent in vuejs
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- python 3x - typeerror: 'method' object is not subscriptable