1

I have a array like this

k = np.array([[   1. , -120.8,   39.5],
       [   0. , -120.5,   39.5],
       [   1. , -120.4,   39.5],
       [   1. , -120.3,   39.5]])

I am trying to remove the following row which is also at index 1 position.

b=np.array([   0. , -120.5,   39.5])

I have tried the traditional methods like the following:

k==b #try to get all True values at index 1 but instead got this
array([[False, False, False],
       [ True, False, False],
       [False, False, False],
       [False, False, False]])

Other thing I tried:

k[~(k[:,0]==0.) & (k[:,1]==-120.5) & (k[:,1]==39.5)]

Got the result like this:

array([], shape=(0, 3), dtype=float64)

I am really surprised why the above methods not working. By the way in the first method I am just trying to get the index so that i can use np.delete later. Also for this problem, I am assuming I don't know the index.

1 Answer 1

2

Both k and b are floats, so equality comparisons are subject to floating point inaccuracies. Use np.isclose instead:

k[~np.isclose(k, b).all(axis=1)]
# array([[   1. , -120.8,   39.5],
#        [   1. , -120.4,   39.5],
#        [   1. , -120.3,   39.5]])

Where

np.isclose(k, b).all(axis=1)
# array([False,  True, False, False])

Tells you which row of k matches b.

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.