## Python Basics - Numpy Arrays

(Part-1) - Question and Answers

CBSE Class 11 - Informatics Practices

**Q1: What is NumPy?**

Answer:

1. NumPy stands for Numerical Python.

2. It is the core library for scientific computing in Python.

3. A NumPy array (also called ndarray) is a homogeneous multidimensional array of data objects.

4. A NumPy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.

5. Numpy module provides a set of methods and tools for working with these arrays.

6. In Numpy dimensions are called axes. The number of axes is rank.

**Q2: What is the command line option to install NumPy module?**

Answer: Run the following command at the command prompt.

*pip install numpy*

**Q3: What is one dimensional (1D) array?**

Answer: A one-dimensional array or 1D array is a named group of a contiguous set of elements having the same data type. 1D arrays are also called vectors.

**Q4: What is a multi-dimensional array?**

Answer: A multidimensional array is an array of arrays. Multi-dimensional arrays are also known as matrices. For example, a two-dimensional array (2D array) has two axes i.e. rows and columns.

**Q5: Write the differences between NumPy array and a list.**

NumPy Array | List |
---|---|

Works on homogenous data types i.e. all data types of the same kind. | Works on heterogenous data types i.e. mixed data types. |

Does not support adding or removing elements. | Supports adding and removing elements. |

Smaller memory consumption | High memory consumption |

Has better runtime | Runtime not fast |

Supports vectorized operations i.e. np + 2 will add 2 to each data member. | Does not support vectorized operations e.g. list1 + 2 will throw an error. |

**Q6: What are the different ways to create NumPy arrays?**

Answer: NumPy arrays can be created by different methods such as:

1. Using Lists or Tuples

2. Using numpy.array( ) function

3. Using fromiter( ) function to create ndarray from non-numeric sequence.

4. Using arange( ) function to create ndarray of evenly spaced values within a specified numeric range.

5. Using special functions like empty( ), zeroes( ), ones( ), full( ) etc.

**Q7: What are axes in Numpy arrays? How it is related to the rank of an array?**

Answer: Numpy refers to dimensions as axes. In ndarrays, axes are always numbered 0 onwards. In NumPy, the number of dimensions is called the rank of an array. One can use ndarray.ndim property to get the number of dimensions.

e.g. a 1-dimensional ndarray will have rank = 1

*import numpy as np*

*'create 1D array'*

*np1 = np.array([1,2,3,4])*

*print('Rank is:', np1.ndim)*

**Q8: What is meant by the shape of ndarray?**

Answer: The shape of a ndarray tells about the number of elements present in each axis (dimension) of it. For a matrix with n rows and m columns, shape will be (n,m).

e.g. as shown in the figure, the shape of a 1-Dimension array is (4,). Here 4 refers to the number of items in the first axis.

The shape of 2-D array is (2,3) i.e. it is a matrix of 2 rows and 3 columns each.

**Q9: How can we find the size of a ndarray?**

Answer: ndarray.size attribute returns the total number of elements of the array. This is equal to the product of the elements of shape.

e.g. A 2-dimensional array of 2 rows and 3 columns i.e. shape is (2,3), its size is 6 (=2 × 3)

**Q10: What does ndarray.itemsize indicate?**

Answer: It returns the length of each element of the array in bytes

**Q11: Name the attribute used to determine the data type of the elements of a ndarray?**

Answer: ndarray.dType

**Q12: What is the shape of the following ndarrays (see figure)?**

Answer:

(a) (4, )

(b) (1, 3)

(c) (2, 4)

**Q13: What are the kinds of data types (dType) supported by NumPy arrays?**

Answer: NumPy supports a greater variety of data types:

**Integer Related**: int_, int32 (32-bit signed), int64 (64-bit signed integer), int8 (Byte from -127 to 128), unint8 (unsigned 8-bit), unint16, unint32 (unsigned 32-bit) etc.

**Float Type**: float_, float64, float32(Half precision float: sign bit, 5 bits exponent, 10 bits mantissa), float 64

**Complex Numbers**: complex_, complex64, complex128

**Boolean Type**: bool

**Other**: string_, unicode_

**Q14: Create a one-dimensional array having**

**(a) 5 integers**

**(b) 4 floating numbers**

**(c) 3 boolean values**

**(d) 5 8-bit integers**

Answer:

import numpy as np np1 = np.array([1,2,3,4]) print(np1) print('np1 dType:', np1.dtype) np2 = np.array([2.1, 3.5, 5.6, 2.78]) print(np2) print('np2 dType:', np2.dtype) np3 = np.array([True, False, True]) print(np3) print('np3 dType:', np3.dtype) np4 = np.array([3, 5, 7], np.int8) print(np4) print('np4 dType:', np4.dtype)

**☞See also:**

Informatics Syllabus (New) - 2019-20

Unit Test Paper I - Python -2019-20

Python Programming Basics (MCQs) - YouTube Video

Python Snippets -1 (Arithmetic Operators)

Python Basics (Q & A)Python Programming Basics (MCQs) - YouTube Video

Python Snippets -1 (Arithmetic Operators)

Python Basics (Q & A) Part-2

Python Basics - Variables (Q & A)

Python Basics - Literals (Q & A)

Python Basics - Augmented Assignment Operators

Python Basics - String Manipulation (Part-1)

Python Basics - String Manipulation (Part-2)

Python Basics - List Manipulation (Part-1) Q & A

Python Basics - List Manipulation (Part-2) Q & A

It is very helpful thanks for the questions.

ReplyDelete