I have numpy matrices X = np.matrix([[1, 2], [3, 4], [5, 6]]) and y = np.matrix([1, 1, 0]) and I want to create two new matrices X_pos and X_neg based on y matrix. So hope my output will be the following: X_pos == matrix([[1, 2], [3, 4]]) and X_neg == matrix([[5, 6]]). How can I do this?
-
Any particular reason why they are matrices?cs95– cs952017-09-02 20:26:14 +00:00Commented Sep 2, 2017 at 20:26
-
It's easy for me to work with matrices in my previous and following code.mike– mike2017-09-02 20:28:42 +00:00Commented Sep 2, 2017 at 20:28
Add a comment
|
2 Answers
With np.ma.masked_where routine:
x = np.matrix([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 1, 0])
m = np.ma.masked_where(y > 0, y) # mask for the values greater than 0
x_pos = x[m.mask] # applying masking
x_neg = x[~m.mask] # negation of the initial mask
print(x_pos)
print(x_neg)