python - balance positives and negatives in numpy -
i have matrix last column has floats in it. around 70% of numbers positive, while 30% negative. i'd remove rows positive number result matrix has approxiamtely same number of positive , negative numbers in last column. i'd remove positives rows randomly.
what this:
import numpy np x = np.arange(30).reshape(10, 3) x[[0,1,2,],[2,2,2]] = x[[0,1,2],[2,2,2]] * -1 = np.where(x[:,2] > 0)[0] n_pos = np.sum(x[:,2] > 0) n_neg = np.sum(x[:,2] < 0) n_to_remove = n_pos - n_neg np.random.shuffle(a) new_x = np.delete(x, a[:n_to_remove], axis = 0)
result:
>>> x array([[ 0, 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]]) >>> new_x array([[ 0, 1, -2], [ 3, 4, -5], [ 6, 7, -8], [15, 16, 17], [18, 19, 20], [27, 28, 29]])
i think easier arrays matrices, let me know if need solution matrices.
Comments
Post a Comment