1

I have an array with size ( 61000) I want to normalize it based on this rule: Normalize the rows 0, 6, 12, 18, 24, ... (6i for i in range(1000)) based on the formulation which I provide. Dont change the values of the other rows. Here is an example:

def normalize(array):
    minimum = np.expand_dims(np.min(array, axis=1), axis=1)
    maximum = np.expand_dims(np.max(array, axis=1), axis=1)
    return (array - minimum) / (maximum - minimum + 0.00001)

Calling with the following input doesn't work:

A = array([[15, 14,  3],
   [11,  9,  9],
   [16,  6,  1],
   [14,  6,  9],
   [ 1, 12,  2],
   [ 5,  1,  2],
   [13, 11,  2],
   [11,  4,  1],
   [11,  7, 10],
   [10, 11, 16],
   [ 2, 13,  4],
   [12, 14, 14]])

normalize(A)

I expect the following output:

array([[0.99999917, 0.9166659 , 0.    ],
   [11,  9,  9],
   [16,  6,  1],
   [14,  6,  9],
   [ 1, 12,  2],
   [ 5,  1,  2],
   [0.99999909, 0.81818107, 0.        ]],
   [11,  4,  1],
   [11,  7, 10],
   [10, 11, 16],
   [ 2, 13,  4],
   [12, 14, 14]])
8
  • 2
    does array[::6] = normalize(array[::6]) work? if not, could you explain more what the issue you're facing is? Commented Nov 16, 2022 at 23:36
  • thanks for the comment. it changed the 6*i to 0. Commented Nov 16, 2022 at 23:44
  • that seems like an issue with normalize though... tbh I'm a bit confused by the expand dims. could you describe in words what you're trying to do? Commented Nov 16, 2022 at 23:46
  • also... is there a purpose to the a definition in the first line? that looks like it's not used anywhere Commented Nov 16, 2022 at 23:48
  • 2
    You need to cast the original array to float dtype first: array = array.astype('float'), then do array[::6] = normalize(array[::6]). Commented Nov 17, 2022 at 1:30

1 Answer 1

1

You have to set up a second function having the step argument:

def normalize_with_step(array, step):
    
    b = normalize(array[::step])
    a, b = list(array), list(b)
    
    for i in range(0, len(a), step):
        a[i] = b[int(i/step)]
        
    a = np.array(a)
    return a

Let's try it with a step = 6:

a = np.random.randint(17, size = (12, 3))
a = normalize_with_step(a, 6)
a

Output

array([[ 0.83333264,  0.99999917,  0.        ],
       [ 9.        , 14.        ,  6.        ],
       [14.        , 15.        , 12.        ],
       [12.        ,  7.        , 10.        ],
       [ 8.        , 13.        ,  9.        ],
       [12.        ,  0.        ,  3.        ],
       [ 0.53333298,  0.99999933,  0.        ],
       [15.        , 14.        , 12.        ],
       [14.        ,  6.        , 16.        ],
       [ 9.        , 14.        ,  3.        ],
       [ 8.        ,  9.        ,  0.        ],
       [10.        , 13.        ,  0.        ]])
Sign up to request clarification or add additional context in comments.

6 Comments

this should do the same thing as, but would be much slower than, A[::step] = normalize(A[::step])
Writing only A[::step] = normalize(A[::step]) doesn't give the desired output, so it's part of the function as you can see b = normalize(array[::step]) in the first line of my function, the function returns the array with normalized item at the desired step
But that’s an issue with normalize. It doesn’t mean you should turn the whole array into a python list…
Where's the issue? since we get the desired output when we apply normalize_with_step function. If you search a magic solution by writing a single line code instead of a function let me know please
Thanks for your response. I accepted this response, but I think the solution which @bui provided is more faster and easier.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.