0% found this document useful (0 votes)
3 views

6.numpy_Coparision_operators

The document provides examples of using comparison operators in NumPy, demonstrating how to compare array elements with various conditions. It includes operations such as less than, greater than, and boolean masking to filter values. Additionally, it shows how to generate arrays and perform calculations based on conditions, such as counting and summing values that meet specific criteria.

Uploaded by

pardhapradeep824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

6.numpy_Coparision_operators

The document provides examples of using comparison operators in NumPy, demonstrating how to compare array elements with various conditions. It includes operations such as less than, greater than, and boolean masking to filter values. Additionally, it shows how to generate arrays and perform calculations based on conditions, such as counting and summing values that meet specific criteria.

Uploaded by

pardhapradeep824
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

07/02/2025, 21:03 numpy_Coparision_operators

In [2]: import numpy as np


x = np.array([1, 2, 3, 4, 5])
print(x < 3) # less than
print(x > 3) # greater than
print(x <= 3) # less than or equal
print(x >= 3) # greater than or equal
print(x != 3) # not equal
print(x == 3) # equal

[ True True False False False]


[False False False True True]
[ True True True False False]
[False False True True True]
[ True True False True True]
[False False True False False]

In [18]: x = np.random.randint(10,size=(3,4))
print(x)
print(np.count_nonzero(x<7))
print(np.sum(x<7))
print(np.sum(x<7,axis=1))
print(np.all(x==7))
print(np.all(x>0))
# is there any nubers greater than 0
print(np.any(x>0))
# boolean operators :
print(np.sum((x>3) & (x<8)))
# similarly | - for or ; ^ - for xor ; ~ - for not

[[7 3 2 5]
[2 5 8 7]
[1 8 3 2]]
8
8
[3 2 3]
False
True
True
4

In [16]: # Boolean array as masks


x = np.random.randint(10,size=(3,4))
print(x)
print(x<5)
# Now to select these values from the array, we can simply index on this Boolean
# this is known as a masking operation:
print(x[x<5])

[[7 7 3 1]
[8 6 3 8]
[8 4 4 7]]
[[False False True True]
[False False True False]
[False True True False]]
[3 1 3 4 4]

In [19]: # np.arange(365) generates an array of integers from 0 to 364, representing each

# Subtracting 172 from each element in the array shifts the range, centering aro

localhost:8892/doc/tree/numpy_Coparision_operators.ipynb? 1/2
07/02/2025, 21:03 numpy_Coparision_operators

# (approximately June 21st).

# The comparison < 90 checks if each day is within 90 days of the shifted center
# s = (np.arange(365) - 172 < 90) #
# print(s)

localhost:8892/doc/tree/numpy_Coparision_operators.ipynb? 2/2

You might also like