Doc
Doc

Reputation: 385

Scipy: Partition array into 3 subarrays

I am trying to figure out whether there's a numpy/scipy function to efficiently partition an array into subarrays using a certain rule.

My problem is the following: I have a nxn matrix, lets call it W. And I have a vector h. I now want to partition the column vectors of W into 3 arrays:

Right now I am doing it like this, which is working but I think it is not very efficient:

    nonzero_indices = (sp.isclose(sp.dot(h_k.T, W),0, 10e-12) == False)
    self.W_null = W[:,~nonzero_indices]

    W_nonzero = W[:,nonzero_indices]
    pos_indices = (sp.dot(h_k.T, W_nonzero) > 0)
    W_pos = W_nonzero[:,pos_indices]
    W_neg = W_nonzero[:,~pos_indices]

Is there a better way? Thanks for your help and if there something not clear please let me know. Cheers

Upvotes: 0

Views: 81

Answers (1)

Lee
Lee

Reputation: 31060

w=np.random.random((10,10))-0.5 # example array

.

wneg = w[w<0]

wzero = w[w==0]

wpos = w[w>0]

Upvotes: 2

Related Questions