Logic FunctionsΒΆ

NumPy’s logic functions test truth values, check array contents for special values, inspect types, perform element-wise logical operations, and compare arrays. These are essential for data validation (checking for NaN or infinite values), conditional processing (selecting elements that meet criteria), and debugging (verifying that computed arrays match expected results). Understanding these functions helps you write robust data pipelines that handle edge cases gracefully.

import numpy as np
np.__version__

Truth Value TestingΒΆ

np.all() returns True if all elements evaluate to True (non-zero), while np.any() returns True if at least one element is True. These are useful for validation checks – for example, verifying that all predictions are within a valid range, or checking whether any values in a dataset are missing. Both support the axis parameter for testing along specific dimensions.

Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.

x = np.array([1,2,3])
#

x = np.array([1,0,3])
#

Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero.

x = np.array([1,0,0])
#

x = np.array([0,0,0])
#

Array ContentsΒΆ

np.isfinite(), np.isinf(), and np.isnan() test each element for special floating-point values. In real-world datasets, NaN (not a number) values appear when data is missing or computations fail, and inf values can result from division by zero or overflow. Detecting these early in your pipeline prevents them from silently corrupting downstream calculations like means, sums, and model training.

Q3. Predict the result of the following code.

x = np.array([1, 0, np.nan, np.inf])
#print np.isfinite(x)

Q4. Predict the result of the following code.

x = np.array([1, 0, np.nan, np.inf])
#print np.isinf(x)

Q5. Predict the result of the following code.

x = np.array([1, 0, np.nan, np.inf])
#print np.isnan(x)

Array Type TestingΒΆ

np.iscomplex(), np.isreal(), and np.isscalar() inspect the type of array elements or objects. These are useful for writing generic functions that need to handle different input types – for example, a function that behaves differently for real vs. complex inputs, or one that accepts both scalar and array arguments.

Q6. Predict the result of the following code.

x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])
#print np.iscomplex(x)

Q7. Predict the result of the following code.

x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])
#print np.isreal(x)

Q8. Predict the result of the following code.

#print np.isscalar(3)
#print np.isscalar([3])
#print np.isscalar(True)

Logical OperationsΒΆ

np.logical_and(), np.logical_or(), np.logical_xor(), and np.logical_not() perform element-wise logical operations on arrays. Unlike Python’s and/or/not keywords (which only work on scalars), these NumPy functions operate on entire arrays and return boolean arrays. They are essential for combining multiple conditions when filtering data – for example, selecting samples where feature A is above a threshold AND feature B is below another threshold.

Q9. Predict the result of the following code.

#print np.logical_and([True, False], [False, False])
#print np.logical_or([True, False, True], [True, False, False])
#print np.logical_xor([True, False, True], [True, False, False])
#print np.logical_not([True, False, 0, 1])

ComparisonΒΆ

np.allclose() checks if two arrays are element-wise equal within a tolerance, which is essential for floating-point comparisons where exact equality rarely holds. np.array_equal() checks for exact equality. np.greater(), np.less(), np.equal(), and np.isclose() provide element-wise comparison with different semantics. These functions are indispensable for writing unit tests, validating numerical algorithms, and comparing model outputs against expected baselines.

Q10. Predict the result of the following code.

#print np.allclose([3], [2.999999])
#print np.array_equal([3], [2.999999])

Q11. Write numpy comparison functions such that they return the results as you see.

x = np.array([4, 5])
y = np.array([2, 5])
#
#
#
#

Q12. Predict the result of the following code.

#print np.equal([1, 2], [1, 2.000001])
#print np.isclose([1, 2], [1, 2.000001])