CS 11 Ans - Numpy Basics
CS 11 Ans - Numpy Basics
1.Write a NumPy program to get the numpy version and show numpy build
configuration.
import numpy as np
print(np.__version__)
print(np.show_config())
import numpy as np
print(np.info(np.add))
import numpy as np
x = np.array([1, 2, 3, 4])
print("Original array:")
print(x)
print("Test if none of the elements of the said array is zero:")
print(np.all(x))
x = np.array([0, 1, 2, 3])
print("Original array:")
print(x)
print("Test if none of the elements of the said array is zero:")
print(np.all(x))
The all() method of numpy.ndarray can be used to check whether all of the
elements of an ndarray object evaluate to True.
The any() method of numpy.ndarray can be used to find whether any of the
elements of an ndarray object evaluate to True.
4. Write a NumPy program to test whether any of the elements of a given
array is non-zero.
import numpy as np
x = np.array([1, 0, 0, 0])
print("Original array:")
print(x)
print("Test whether any of the elements of a given array is
non-zero:")
print(np.any(x))
x = np.array([0, 0, 0, 0])
print("Original array:")
print(x)
print("Test whether any of the elements of a given array is
non-zero:")
print(np.any(x))
Note:
numpy.Inf
numpy.NAN
numpy.NINF
IEEE 754 floating point representation of negative infinity.
import numpy as np
a = np.array([1, 0, np.nan, np.inf])
print("Original array")
print(a)
print("Test a given array element-wise for finiteness :")
print(np.isfinite(a))
import numpy as np
a = np.array([1, 0, np.nan, np.inf])
print("Original array")
print(a)
print("Test element-wise for NaN:")
print(np.isnan(a))
Note
numpy.isscalar(element)[source]
Parameters
elementany
Input argument, can be of any type and shape.
Returns
valbool
True if element is a scalar type, False if it is not.
import numpy as np
a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])
print("Original array")
print(a)
print("Checking for complex number:")
print(np.iscomplex(a))
print("Checking for real number:")
print(np.isreal(a))
print("Checking for scalar type:")
print(np.isscalar(3.1))
print(np.isscalar([3.1]))
Note:
import numpy as np
x = np.array([3, 5])
y = np.array([2, 5])
print("Original numbers:")
print(x)
print(y)
print("Comparison - greater")
print(np.greater(x, y))
print("Comparison - greater_equal")
print(np.greater_equal(x, y))
print("Comparison - less")
print(np.less(x, y))
print("Comparison - less_equal")
print(np.less_equal(x, y))
0.00001
import numpy as np
x = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100])
y = np.array([72, 79, 85, 90, 150, -135, 120, -10, 60, 100.000001])
print("Original numbers:")
print(x)
print(y)
print("Comparison - equal:")
print(np.equal(x, y))
print("Comparison - equal within a tolerance:")
print(np.allclose(x, y))
This function is equivalent to allclose except that masked values are treated as
equal (default) or unequal, depending on the masked_equal argument.
11. Write a NumPy program to create an array with the values 1, 7, 13, 105
and determine the size of the memory occupied by the array.
import numpy as np
X = np.array([1, 7, 13, 105])
print("Original array:")
print(X)
print("Size of the memory occupied by the said array:")
print("%d bytes" % (X.size * X.itemsize))
12. Write a NumPy program to create an array of 10 zeros,10 ones, 10 fives.
import numpy as np
array=np.zeros(10)
print("An array of 10 zeros:")
print(array)
array=np.ones(10)
print("An array of 10 ones:")
print(array)
array=np.ones(10)*5
print("An array of 10 fives:")
print(array)
13. Write a NumPy program to create an array of the integers from 30 to70.
import numpy as np
array=np.arange(30,71)
print("Array of the integers from 30 to70")
print(array)
14. Write a NumPy program to create an array of all the even integers from 30
to 70.
import numpy as np
array=np.arange(30,71,2)
print("Array of all the even integers from 30 to 70")
print(array)