0

I have all_data as a numpy array with the size of (2,601), NUM_SAMPLES = 601 and NUM_CLUSTERS = 3. Is there any vector form to build f (a (601,9) numpy array) than what using nested for-loops as follows?

f = np.empty((0,9), float)
for n in range(NUM_SAMPLES):
    f_n = np.array([[]])
    for m in range(NUM_CLUSTERS):
        f_n = np.hstack( (f_n , z_i(alldata[:,n], m).T))
    f = np.concatenate((f, f_n) , axis=0)

NOTE : when recalling function z_i(alldata[:,n], m), it returns a (3,1) numpy array.

f is supposed to be 'F' in the following formula: formula of f

5
  • Your problem is the function z_i. Commented Nov 30, 2021 at 20:19
  • why? What is wrong? Could you please tell me more? @QuangHoang Commented Nov 30, 2021 at 20:26
  • what Quang want to say is, that you could create a function that can input all data and return a 3xn array or given an array and a m creates a 3m xn or something like that Commented Nov 30, 2021 at 20:52
  • z_i has to be called once for each combination of n and m.? And it's a relatively slow python function? That's your bottleneck! Commented Nov 30, 2021 at 21:39
  • @hpaulj I see. Thanks. Commented Dec 1, 2021 at 10:52

1 Answer 1

1

Because you have some function z_i in the middle of your loops, you're more or less stuck with loops. You don't need to do a bunch of really inefficient concats like you're doing, but your array size is so small it probably doesn't matter.

f = np.vstack((np.hstack((z_i(alldata[:,n], m).T for m in range(NUM_CLUSTERS)))
               for n in range(NUM_SAMPLES)))

If you really want this to run faster you have to look into z_i and change how that's working.

Sign up to request clarification or add additional context in comments.

Comments

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.