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,070 points
990 views

1 answer to this question.

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,520 points

Related Questions In Python

0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26 in Python by Neel
• 3,010 points
58 views
0 votes
1 answer

Is it possible to print all the modules imported in a python script?

Since I am using Python 3.6, I ...READ MORE

answered Jul 3 in Python by Neel
• 3,010 points
37 views
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24 in Python by Anjali
57 views
+1 vote
1 answer

is it possible to compare a values to a backslash in Python?

The backslash "\" character is used to ...READ MORE

answered Jun 28 in Python by Neel
• 3,010 points
42 views
0 votes
1 answer

Doubt in numpy.vstack

The vstack function in numpy will stack ...READ MORE

answered Jul 30 in Python by Esha
49 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
56 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
125 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,520 points
253 views