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, 2019 in Python by Anirudh
• 2,090 points
5,037 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, 2019 by Nymeria
• 3,560 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, 2019 in Python by Neel
• 3,020 points
1,044 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, 2019 in Python by Neel
• 3,020 points
3,415 views
0 votes
1 answer

I have trained a model using machine learning algorithm in python , now i want to create a browser extension of that saved model , Is it possible ??

Hi@Shubham, It may possible. When you saved your ML ...READ MORE

answered Jun 13, 2020 in Python by MD
• 95,460 points
1,126 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, 2019 in Python by Anjali
1,127 views
0 votes
1 answer

Doubt in numpy.vstack

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

answered Jul 30, 2019 in Python by Esha
1,070 views
0 votes
1 answer
0 votes
1 answer

How to save numpy array?

Hi@akhtar, You can use numpy.save() function to save ...READ MORE

answered Jun 22, 2020 in Python by MD
• 95,460 points
1,796 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
1,032 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,560 points
22,786 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP