NCERTCh 6Free

Introduction Chapter to NumPy

🎓 Class 11📖 Informatics Practices📖 12 notes🧠 15 Q&A⏱️ ~18 min

Introduction Chapter to NumPyStudy Notes

NCERT-aligned · 12 notes · 3 shown free

6.1 INTRODUCTION

Explanation

6.1 INTRODUCTION

NumPy, short for 'Numerical Python', is a fundamental package for data analysis and scientific computing in Python. It introduces a powerful n-dimensional array object, known as ndarray, which allows efficient storage and manipulation of numerical data. Unlike standard Python lists, NumPy arrays store elements contiguously in memory, enabling faster computation and data processing. NumPy also provides a rich set of functions and tools to operate on these arrays, facilitating tasks such as mathematical operations, reshaping, indexing, and statistical analysis. Moreover, NumPy integrates seamlessly with other Python libraries and supports interfacing with programming languages like C and C++, making it versatile for various scientific and engineering applications. This chapter covers the basics of NumPy arrays, including their creation, manipulation, and operations, preparing students to handle data effectively using Python.

  • NumPy stands for Numerical Python and is used for scientific computing.
  • It provides an n-dimensional array object called ndarray.
  • Arrays store elements contiguously in memory for fast processing.
  • NumPy offers numerous functions for array creation and manipulation.
  • It integrates well with other Python packages and languages like C/C++.
  • This chapter introduces arrays, their operations, and file handling.
  • 📌 NumPy: A Python package for numerical and scientific computing.
  • 📌 ndarray: The core n-dimensional array object in NumPy.
  • 📌 Contiguous Memory Allocation: Storing array elements sequentially in memory.

6.2 ARRAY

Explanation

6.2 ARRAY

An array is a data structure used to store multiple values under a single variable name, where all elements are of the same data type and stored contiguously in memory. This contiguous memory allocation means that each element occupies a fixed-size position in memory, allowing fast access and efficient operations. Arrays differ from lists and other data structures by enforcing uniform data types and contiguous storage. Indexing in arrays is zero-based, meaning the first element has index 0, the second index 1, and so forth. For example, an array [10, 9, 99, 71, 90] has elements indexed from 0 to 4. This concept of arrays is fundamental and supported across almost all programming languages. The chapter contrasts contiguous memory allocation, where data is stored sequentially, with non-contiguous allocation, where data blocks may be scattered in memory. Arrays enable efficient data processing and form the basis for NumPy's powerful array structures.

  • An array stores multiple values of the same data type under one variable.
  • Elements are stored contiguously in memory for fast access.
  • Each element is accessed using zero-based indexing.
  • Contiguous memory allocation divides memory into fixed-size slots.
  • Non-contiguous allocation stores data in scattered blocks.
  • Arrays are foundational data structures in programming.
  • 📌 Array: Ordered collection of elements of the same type.
  • 📌 Index: Position of an element in an array, starting at 0.
  • 📌 Contiguous Memory Allocation: Sequential storage of array elements.

6.3 NumPy ARRAY

Explanation

6.3 NumPy ARRAY

NumPy arrays, officially called ndarray, are specialized data structures designed to store numerical data efficiently. Unlike Python lists, which can hold elements of different types and are stored non-contiguously, NumPy arrays require all elements

Practice QuestionsIntroduction Chapter to NumPy

Includes NCERT exercise questions with answers

Q1.1. What is NumPy? How to install it?

Answer:

NumPy is a Python library used for numerical computing. It provides support for arrays, matrices, and many mathematical functions to operate on these data structures efficiently. To install NumPy, you can use the pip package manager by running the command: pip install numpy in your command prompt or terminal.

Explanation:

NumPy stands for Numerical Python and is widely used for scientific computing. Installation via pip is straightforward and ensures you get the latest stable version.

EasyNCERT
Q2.2. What is an array and how is it different from a list? What is the name of the built-in array class in NumPy?

Answer:

An array is a collection of elements of the same data type stored at contiguous memory locations. Unlike a Python list, which can hold elements of different data types and is more flexible but less efficient, a NumPy array (ndarray) is optimized for numerical operations and uses less memory. The built-in array class in NumPy is called 'ndarray' (N-dimensional array).

Explanation:

Arrays provide efficient storage and operations for homogeneous data, whereas lists are more general-purpose but less efficient for numerical computations. NumPy's ndarray class is designed for fast array operations.

EasyNCERT
Q3.3. What do you understand by rank of an ndarray?

Answer:

The rank of an ndarray refers to the number of dimensions or axes it has. For example, a 1-D array has rank 1, a 2-D array has rank 2, and so on. It indicates how many indices are needed to specify an element in the array.

Explanation:

Rank is a fundamental property of arrays that helps in understanding their structure and how to access elements.

EasyNCERT
Q4.4. Create the following NumPy arrays: a) A 1-D array called zeros having 10 elements and all the elements are set to zero. b) A 1-D array called vowels having the elements 'a', 'e', 'i', 'o' and 'u'. c) A 2-D array called ones having 2 rows and 5 columns and all the elements are set to 1 and dtype as int. d) Use nested Python lists to create a 2-D array called myarray1 having 3 rows and 3 columns and store the following data: 2.7, -2, -19 0, 3.4, 99.9 10.6, 0, 13 e) A 2-D array called myarray2 using arange() having 3 rows and 5 columns with start value = 4, step size 4 and dtype as float.

Answer:

a) zeros = np.zeros(10) b) vowels = np.array(['a', 'e', 'i', 'o', 'u']) c) ones = np.ones((2,5), dtype=int) d) myarray1 = np.array([[2.7, -2, -19], [0, 3.4, 99.9], [10.6, 0, 13]]) e) myarray2 = np.arange(4, 4 + 3*5*4, 4, dtype=float).reshape(3,5) Explanation: - np.zeros(10) creates a 1-D array of 10 zeros. - np.array with list of vowels creates a 1-D array of characters. - np.ones with shape (2,5) and dtype int creates a 2-D array of ones. - Nested lists are passed to np.array to create myarray1. - np.arange creates a sequence starting at 4, stepping by 4, total 15 elements, reshaped to 3x5.

Explanation:

Each part uses appropriate NumPy functions to create arrays as specified. Reshape is used to get the desired dimensions.

MediumNCERT
Q5.5. Using the arrays created in Question 4 above, write NumPy commands for the following: a) Find the dimensions, shape, size, data type of the items and itemsize of arrays zeros, vowels, ones, myarray1 and myarray2. b) Reshape the array ones to have all the 10 elements in a single row. c) Display the 2nd and 3rd element of the array vowels. d) Display all elements in the 2nd and 3rd row of the array myarray1. e) Display the elements in the 1st and 2nd column of the array myarray1. f) Display the elements in the 1st column of the 2nd and 3rd row of the array myarray1. g) Reverse the array of vowels.

Answer:

a) zeros.ndim, zeros.shape, zeros.size, zeros.dtype, zeros.itemsize vowels.ndim, vowels.shape, vowels.size, vowels.dtype, vowels.itemsize ones.ndim, ones.shape, ones.size, ones.dtype, ones.itemsize myarray1.ndim, myarray1.shape, myarray1.size, myarray1.dtype, myarray1.itemsize myarray2.ndim, myarray2.shape, myarray2.size, myarray2.dtype, myarray2.itemsize b) ones.reshape(1, 10) c) vowels[1:3] d) myarray1[1:3, :] e) myarray1[:, 0:2] f) myarray1[1:3, 0] g) vowels[::-1] Explanation: - ndim gives number of dimensions. - shape gives the shape tuple. - size gives total number of elements. - dtype gives data type of elements. - itemsize gives size in bytes of each element. - Slicing is used to access specified elements or rows/columns. - Reversing array uses slicing with step -1.

Explanation:

Each command uses NumPy attributes and slicing to retrieve or manipulate the arrays as required.

MediumNCERT
Q6.6. Using the arrays created in Question 4 above, write NumPy commands for the following: a) Divide all elements of array ones by 3. b) Add the arrays myarray1 and myarray2. c) Subtract myarray1 from myarray2 and store the result in a new array. d) Multiply myarray1 and myarray2 elementwise. e) Do the matrix multiplication of myarray1 and myarray2 and store the result in a new array myarray3. f) Divide myarray1 by myarray2. g) Find the cube of all elements of myarray1 and divide the resulting array by 2. h) Find the square root of all elements of myarray2 and divide the resulting array by 2. The result should be rounded to two places of decimals.

Answer:

a) ones / 3 b) myarray1 + myarray2 c) myarray2 - myarray1 d) myarray1 * myarray2 e) myarray3 = np.matmul(myarray1, myarray2) f) myarray1 / myarray2 g) (myarray1 ** 3) / 2 h) np.round(np.sqrt(myarray2) / 2, 2) Explanation: - Arithmetic operations on arrays are elementwise by default. - Matrix multiplication uses np.matmul or the @ operator. - np.sqrt computes square root elementwise. - np.round rounds the elements to specified decimal places.

Explanation:

These commands demonstrate elementwise arithmetic, matrix multiplication, and use of mathematical functions in NumPy.

MediumNCERT
Q7.7. Using the arrays created in Question 4 above, write NumPy commands for the following: a) Find the transpose of ones and myarray2. b) Sort the array vowels in reverse. c) Sort the array myarray1 such that it brings the lowest value of the column in the first row and so on.

Answer:

a) ones.T myarray2.T b) np.sort(vowels)[::-1] c) np.sort(myarray1, axis=0) Explanation: - .T attribute returns the transpose of an array. - np.sort sorts array elements; reversing with [::-1] gives descending order. - Sorting myarray1 along axis=0 sorts each column independently, bringing lowest values to first row.

Explanation:

Transpose changes rows to columns and vice versa. Sorting can be done ascending or descending by reversing the sorted array.

MediumNCERT
Q8.8. Using the arrays created in Question 4 above, write NumPy commands for the following: a) Use NumPy.split() to split the array myarray2 into 5 arrays columnwise. Store your resulting arrays in myarray2A, myarray2B, myarray2C, myarray2D and myarray2E. Print the arrays myarray2A, myarray2B, myarray2C, myarray2D and myarray2E. b) Split the array zeros at array index 2, 5, 7, 8 and store the resulting arrays in zerosA, zerosB, zerosC and zerosD and print them. c) Concatenate the arrays myarray2A, myarray2B and myarray2C into an array having 3 rows and 3 columns.

Answer:

a) myarray2A, myarray2B, myarray2C, myarray2D, myarray2E = np.split(myarray2, 5, axis=1) print(myarray2A, myarray2B, myarray2C, myarray2D, myarray2E) b) zerosA, zerosB, zerosC, zerosD = np.split(zeros, [2, 5, 7, 8]) print(zerosA, zerosB, zerosC, zerosD) c) np.concatenate((myarray2A, myarray2B, myarray2C), axis=1) Explanation: - np.split splits arrays at specified indices along given axis. - axis=1 splits columnwise. - np.concatenate joins arrays along specified axis.

Explanation:

Splitting and concatenation are useful for dividing and combining arrays as needed.

MediumNCERT