0% found this document useful (0 votes)
26 views8 pages

CS 11 Ans - Numpy Basics

This document provides examples of NumPy programs to perform common array operations and tests. It includes programs to get NumPy version and configuration info, test properties of arrays like whether elements are zero, check element types, perform element-wise comparisons, and create arrays with specified values. The programs demonstrate basic NumPy functions like np.info(), np.all(), np.any(), np.isfinite(), np.isinf(), np.isnan(), np.greater(), np.equal(), np.arange(), and how to get the size of memory used by an array.

Uploaded by

asha.py81
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)
26 views8 pages

CS 11 Ans - Numpy Basics

This document provides examples of NumPy programs to perform common array operations and tests. It includes programs to get NumPy version and configuration info, test properties of arrays like whether elements are zero, check element types, perform element-wise comparisons, and create arrays with specified values. The programs demonstrate basic NumPy functions like np.info(), np.all(), np.any(), np.isfinite(), np.isinf(), np.isnan(), np.greater(), np.equal(), np.arange(), and how to get the size of memory used by an array.

Uploaded by

asha.py81
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/ 8

NumPy Basic: Exercises, Practice, Solution

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())

2. Write a NumPy program to get help on the add function.

import numpy as np
print(np.info(np.add))

3. Write a NumPy program to test whether none of the elements of a given


array is zero.

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))

Note: numpy.any()returns True if at least one element in a NumPy array


evaluates to True while numpy.all() returns True only if all elements in a NumPy
array evaluate to True.

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))

5. Write a NumPy program to test a given array element-wise for finiteness


(not infinity or not a Number).

Note:

numpy.Inf

IEEE 754 floating point representation of (positive) infinity.

numpy.NAN

IEEE 754 floating point representation of Not a Number (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))

6. Write a NumPy program to test element-wise for positive or negative


infinity.
import numpy as np
a = np.array([1, 0, np.nan, np.inf])
print("Original array")
print(a)
print("Test element-wise for positive or negative infinity:")
print(np.isinf(a))

7. Write a NumPy program to test element-wise for NaN of a given array.

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))

8. Write a NumPy program to test element-wise for complex number, real


number of a given array. Also test whether a given number is a scalar type or
not.

Note

numpy.isscalar(element)[source]

Returns True if the type of element is a scalar type.

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:

Scalar is a group of data types such as integer, float, Boolean,


string etc. It doesn't include data types such as List or Tuple.

numpy.isscalar() : This is a logical function that returns true if


the type of input num is scalar.
9. Write a NumPy program to create an element-wise comparison (greater,
greater_equal, less and less_equal) of two given arrays.

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))

10. Write a NumPy program to create an element-wise comparison (equal,


equal within a tolerance) of two given arrays.

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))

ma.allclose(a, b, masked_equal=True, rtol=1e-05, atol=1e-08)


Returns True if two arrays are element-wise equal within a tolerance.

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)

You might also like