How to drop rows from numpy array based on values from another numpy array?
Date : March 29 2020, 07:55 AM
hope this fix your issue Just do a simple indexing as follows and then reassign the new result: In [54]: X_train = X_train[labels2 != -1]
In [55]: X_train
Out[55]:
array([[ 1, 2, 3],
[ 7, 8, 9],
[10, 11, 12]])
|
How do I change id values in a numpy array to a string numpy array with a dictionary
Date : March 29 2020, 07:55 AM
seems to work fine Using the solution from this post: x = np.array([[1,1,3], [2,2,2]])
d = {1: 'a', 2:'b', 3:'c'}
np.vectorize(d.get)(x)
>> array([['a', 'a', 'c'],
['b', 'b', 'b']], dtype=object)
|
Is there a 'numpy' way to extend a 1D Numpy Array to 2D with the output values dependent on the original values?
Tag : python , By : Sigtryggur
Date : March 29 2020, 07:55 AM
like below fixes the issue I have a list of numbers, call this "list", of length l, max N. The desired output is a numpy array shaped [l, N], where each row has list[row_index] 1s followed by 0s for the rest. , This will work: l = [3, 2, 4, 2, 1, 0]
N = 5
np.where(np.arange(N) >= np.array(l)[np.newaxis].T, 0, 1)
array([[1, 1, 1, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
|
Find indices of numpy array based on values in another numpy array
Tag : python , By : user182203
Date : March 29 2020, 07:55 AM
To fix this issue Taking into account the proposed options on the comments, and adding an extra option with numpy's in1d option: >>> import numpy as np
>>> summed_rows = np.random.randint(low=1, high=14, size=9999)
>>> common_sums = np.array([7,10,13])
>>> ind_1 = (summed_rows==common_sums[:,None]).any(0).nonzero()[0] # Option of @Brenlla
>>> ind_2 = np.where(summed_rows == common_sums[:, None])[1] # Option of @Ravi Sharma
>>> ind_3 = np.arange(summed_rows.shape[0])[np.in1d(summed_rows, common_sums)]
>>> ind_4 = np.where(np.in1d(summed_rows, common_sums))[0]
>>> ind_5 = np.where(np.isin(summed_rows, common_sums))[0] # Option of @jdehesa
>>> np.array_equal(np.sort(ind_1), np.sort(ind_2))
True
>>> np.array_equal(np.sort(ind_1), np.sort(ind_3))
True
>>> np.array_equal(np.sort(ind_1), np.sort(ind_4))
True
>>> np.array_equal(np.sort(ind_1), np.sort(ind_5))
True
python -m timeit -s 'import numpy as np; np.random.seed(0); a = np.random.randint(low=1, high=14, size=9999); b = np.array([7,10,13])' 'ind_1 = (a==b[:,None]).any(0).nonzero()[0]'
10000 loops, best of 3: 52.7 usec per loop
python -m timeit -s 'import numpy as np; np.random.seed(0); a = np.random.randint(low=1, high=14, size=9999); b = np.array([7,10,13])' 'ind_2 = np.where(a == b[:, None])[1]'
10000 loops, best of 3: 191 usec per loop
python -m timeit -s 'import numpy as np; np.random.seed(0); a = np.random.randint(low=1, high=14, size=9999); b = np.array([7,10,13])' 'ind_3 = np.arange(a.shape[0])[np.in1d(a, b)]'
10000 loops, best of 3: 103 usec per loop
python -m timeit -s 'import numpy as np; np.random.seed(0); a = np.random.randint(low=1, high=14, size=9999); b = np.array([7,10,13])' 'ind_4 = np.where(np.in1d(a, b))[0]'
10000 loops, best of 3: 63 usec per loo
python -m timeit -s 'import numpy as np; np.random.seed(0); a = np.random.randint(low=1, high=14, size=9999); b = np.array([7,10,13])' 'ind_5 = np.where(np.isin(a, b))[0]'
10000 loops, best of 3: 67.1 usec per loop
|
Merging one NumPy array into new NumPy array with equal amount of values
Date : March 29 2020, 07:55 AM
With these it helps From the comments I take it you need the indices of all the zeros and a random 100'000 ones. # make example
>>> A = np.repeat((0,1), (10**5, 10**7))
>>> np.random.shuffle(A)
# convert to bool
>>> m = A.astype(bool)
# put an additional 100'000 zeros ...
>>> B = np.repeat((False, True), (10**5, 10**7 - 10**5))
>>> np.random.shuffle(B)
# ... at positions that used to be one
>>> m[m] = B
# and get the indices of zeros
>>> idx, = np.where(~m)
# check
>>> idx
array([ 1, 22, 180, ..., 10099911, 10099950, 10099969])
>>> len(idx)
200000
>>> A[idx]
array([0, 1, 1, ..., 1, 1, 0])
>>> A[idx].sum()
100000
|