Is it possible to invert a Numpy boolean array in Python using the tilde operator (~)?

0 votes

Hi all,

So basically my requirement is pretty simple. I just wanted to know I could make use of the tilde operator (~) to invert a Numpy array consisting of boolean data. However, I am aware of the standard way to do it is to use the functions np.invert() and even np.logical_and()

Tilde seems to work but my concern here is that I am unable to find the plausible usage in any Numpy reference material, to be honest.

Note: I have seen that it does not work with scalars at all. Well this is what I mean, consider this example:
Example: bool(~True) -----> This returns True. 

Appreciate all the help!

Jan 11 in Python by Anirudh
• 2,050 points
260 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Good question, glad you brought this up.

I have actually worked on it personally and I can assure you it is possible by all means.

But, to give you more clarity I would like you to actually check out the official Numpy documentation under "numpy.invert". 

This documentation answers your question perfectly. But, it is important that you do note this.

The bitwise_not is an alias for the invert method:

Check out the tiny snippet I have here:

>> np.bitwise_not is np.invert
>> True

And, here is where the Bit-wise NOT operator of the binary representation of the integers are present in the array input. So, how do we go about it from here?

Well the ufunc implements the Python Operator Tilde (~).

Perfect, right? Hope this helped!

answered Jan 11 by Nymeria
• 3,500 points

Related Questions In Python

+4 votes
7 answers
0 votes
1 answer
0 votes
1 answer

Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE

answered Aug 31, 2018 in Python by charlie_brown
• 7,710 points
42 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 9,460 points
22 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
34 views
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
60 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,670 points

reshown Aug 24, 2018 by Priyaj 38 views
0 votes
1 answer

Is it possible in Python requests to print entire HTTP request?

A better idea is to use the ...READ MORE

answered Nov 26, 2018 in Python by Nymeria
• 3,500 points
19 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.