Hello. I'm trying to create a function that generates a subarray of an existing array using number-fi. For example, after receiving parameters such as shape of the existing array, center, and fill of the existing array, like the code below,
import numpy as np
def sub_array(array, shape, center, fill=None):
# ... your code here
I want to make it like the picture below.
So I squeezed the code as below, but if there is no fill, it will work, but if there is a fill, I don't know which code to enter or if it's better to flip the code from the beginning. It's my first time coding, so please understand that the code is messy!
def subarray(array, shape, center, fill=None):
"""Calculate sub_array of numpy array."""
if shape[0] % 2 == 0:
raw = ((shape[0] // 2) - 1, (shape[0] // 2))
if shape[0] % 2 == 1:
raw = ((shape[0] // 2), (shape[0] // 2))
if shape[1] % 2 == 0:
col = ((shape[1] // 2) - 1, (shape[1] // 2))
if shape[1] % 2 == 1:
col = ((shape[1] // 2), (shape[1] // 2))
new_array = array[center[0] - raw[0]: (center[0] + raw[1] + 1), center[1] - col[0]: (center[1] + col[1] + 1)]
#np.append(new_array, np.full(()))
return new_array
I think we should think about what to do if there is no value in fill
in the situation where we have to fill in the empty space, but I don't think that's the case for now.
If I were you, I'd like to solve this problem
I think I'm going to think about it's
The code for solving number 1 should correspond to the above conditional statement, right?
To summarize the code you wrote,
If the length is even, divide the length in half and subtract 1, and if it is odd, divide it in half and discard the decimal point.
That's what I'd say.
But if you think about it carefully, you don't have to divide it into conditional sentences like that, subtract 1 from the length, divide it in half, and write
to discard the decimal point, so you can process it at once without branching it into conditional sentences.
For example, if the length is 5, 5 // 2 = 2
was previously 6 // 2 - 1 = 2
was previously 6 // 2 - 2
You can see that (5 - 1) // 2 = 2
and (6 - 1) // 2 = 2
.
Secondly, you are thinking about filling in the empty space of the newly created array.
Conversely, you can declare an array of filled values and write some of the values in this array by taking them from a given array.
If you write in the form of np.ones(shape, int) * fill
, you can get an array filled with the desired values.
You just have to cut the rest well and cover it up Haha
I don't know because I haven't used numpy, but I found some plausible ones in the official document and attached them.
import numpy as np
z = np.arange(1, 31).reshape(5, 6)
"""
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30]])
"""
z[1:5, 1:4] #[row split, column split]
"""
array([[ 8, 9, 10],
[14, 15, 16],
[20, 21, 22],
[26, 27, 28]])
"""
np.block([
[ z[1:5, 2:6], np.ones((4, 1)) ],
[ np.ones((1, 4)), np.ones((1, 1)) ]])
"""
array([[ 9., 10., 11., 12., 1.],
[15., 16., 17., 18., 1.],
[21., 22., 23., 24., 1.],
[27., 28., 29., 30., 1.],
[ 1., 1., 1., 1., 1.]])
"""
I'm lazy to explain, so pass!
567 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
599 GDB gets version error when attempting to debug with the Presense SDK (IDE)
886 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
607 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.