Humanities/Arts Exam  >  Humanities/Arts Notes  >  Informatics Practices for Class 11  >  Chapter Notes: Introduction to NumPy

Introduction to NumPy Chapter Notes | Informatics Practices for Class 11 - Humanities/Arts PDF Download

Introduction

  • NumPy stands for 'Numerical Python' and is a package designed for data analysis and scientific computing in Python.
  • It provides a multidimensional array object, which is the core data structure for efficient numerical computations.
  • NumPy includes a variety of functions and tools for working with arrays, enabling fast and efficient data processing.
  • The powerful n-dimensional array in NumPy significantly speeds up data processing due to its optimized implementation.
  • NumPy can be easily interfaced with other Python packages, enhancing its versatility in data science workflows.
  • It provides tools for integrating with other programming languages such as C and C++, allowing for performance-critical operations.

Installing NumPy

  • NumPy can be installed using the command: pip install NumPy in the terminal or command prompt.
  • The installation process downloads and sets up the NumPy library, making it available for use in Python scripts.

Array

  • An array is a data type used to store multiple values under a single identifier (variable name).
  • It contains an ordered collection of data elements, where each element is of the same data type.
  • Elements in an array can be referenced by their index, which indicates their position in the array.
  • Contiguous Memory Allocation: Arrays store elements in contiguous memory locations, meaning the memory space is divided into fixed-size positions, each allocated to a single data element.
  • In contrast, non-contiguous memory allocation divides data into blocks placed in different parts of memory based on availability.
  • Characteristics of Arrays:
    • All elements are of the same data type, though their values may differ.
    • The entire array is stored contiguously in memory, which makes operations on arrays fast.
    • Each element is identified using the array name and its unique index, an integer value based on the element’s position.
  • Zero-Based Indexing: The first element has an index of 0, the second has an index of 1, and so on. For example, in the array [10, 9, 99, 71, 90], the element 10 is at index [0], and 90 is at index [4].
  • Arrays are a fundamental concept supported by almost all programming languages due to their efficiency and utility.

NumPy Array

  • NumPy arrays, officially called ndarray, are used to store lists of numerical data, including vectors and matrices.
  • The NumPy library provides a large set of routines (built-in functions) for creating, manipulating, and transforming arrays.
  • While Python has a built-in array data structure, it is less versatile, efficient, and useful compared to NumPy’s ndarray.
  • In this chapter, the term “array” refers to NumPy arrays unless specified otherwise.

Difference Between List and Array

  • Data Types: Lists can contain elements of different data types (e.g., [1, 3.4, 'hello', 'a@']), while all elements in an array must be of the same data type (e.g., [1.2, 5.4, 2.7] for floats).
  • Memory Storage: List elements are not stored contiguously in memory, whereas array elements are stored in contiguous memory locations, making array operations faster.
  • Element-Wise Operations: Lists do not support element-wise operations like addition or multiplication due to mixed data types, but arrays support such operations (e.g., dividing each element of an array by 3).
  • Memory Efficiency: Lists store type information for each element, making them less memory-efficient. Arrays, not needing to store type information separately, take up less memory.
  • Library Dependency: Lists are part of core Python, while arrays (ndarray) are part of the NumPy library.

Creation of NumPy Arrays from List

  • To create and use NumPy arrays, the NumPy library must be imported, typically as: import numpy as np.
  • The np.array()function converts a given list into a NumPy array. Example:

    array1 = np.array([10, 20, 30])
    array1
    # Output: array([10, 20, 30])

  • Creating a 1-D Array: A 1-D array has a single row of elements. When creating an array from a list with mixed data types (e.g., numbers and strings), all elements are converted to a common type, often strings:

    array2 = np.array([5, -7.4, 'a', 7.2])
    array2
    # Output: array(['5', '-7.4', 'a', '7.2'], dtype='

    Note: U32 indicates a Unicode-32 data type.
  • Common Mistake: Passing individual values to np.array() without square brackets is incorrect (e.g., np.array(1, 2, 3, 4)). The correct way is to pass a list: np.array([1, 2, 3, 4]).
  • Creating a 2-D Array:A 2-D array is created by passing nested lists to np.array(). Example:

    array3 = np.array([[2.4, 3], [4.91, 7], [0, -1]])
    array3
    # Output: array([[ 2.4 , 3. ],
    #  [ 4.91,  7.  ],
    #  [ 0.  , -1.  ]])

Attributes of NumPy Arrays

  • ndarray.ndim: Returns the number of dimensions (axes) of the array. For example, a 1-D array has ndim = 1, and a 2-D array has ndim = 2.
  • ndarray.shape: Returns a tuple representing the array’s dimensions (e.g., (3, 2) for an array with 3 rows and 2 columns).
  • ndarray.size: Returns the total number of elements in the array, equal to the product of the shape’s elements.
  • ndarray.dtype: Specifies the data type of the array’s elements (e.g., int32, float64, U32). All elements in an array share the same data type.
  • ndarray.itemsize: Specifies the size in bytes of each element. For example:
  • int32 or float32: Each element occupies 32 bits (4 bytes).
  • int64 or float64: Each element occupies 64 bits (8 bytes).
  • For strings (e.g., U32), the itemsize can be larger (e.g., 128 bytes).

Other Ways of Creating NumPy Arrays

  • Specifying Data Type: The dtype argument in np.array()allows specifying the data type, converting elements accordingly. 
    Example:

    array4 = np.array([[1, 2], [3, 4]], dtype=float)
    array4
    # Output: array([[1., 2.],
    # [3., 4.]])

  • zeros(): Creates an array with all elements initialized to 0, with a default data type of float. Example:

    array5 = np.zeros((3, 4))
    array5
    # Output: array([[0., 0., 0., 0.],
    # [0., 0., 0., 0.],
    #   [0., 0., 0., 0.]])

  • ones(): Creates an array with all elements initialized to 1, with a default data type of float. Example:

    array6 = np.ones((3, 2))
    array6
    # Output: array([[1., 1.],
    # [1., 1.],
    # [1., 1.]])

  • arange():Creates an array with numbers in a specified range and step size, similar to Python’s range(). Examples:

    array7 = np.arange(6)
    array7
    # Output: array([0, 1, 2, 3, 4, 5])
    array8 = np.arange(-2, 24, 4)
    array8
    # Output: array([-2, 2, 6, 10, 14, 18, 22])

Indexing and Slicing

  • NumPy arrays can be indexed, sliced, and iterated over, similar to Python lists.

Indexing

  • 1-D Array Indexing: Elements are accessed using a single index, starting from 0 (zero-based indexing).
  • 2-D Array indexing: Elements are accessed using two indices, [i, j], where i is the row number and j is the column number, both starting from 0.
  • Example: For a 2-D array marksrepresenting student marks (4 rows, 3 columns):
  • marks[0, 2] accesses the element in the 1st row, 3rd column (e.g., 56).
  • marks[3, 1] accesses the element in the 4th row, 2nd column (e.g., 72).
  • Accessing an invalid index (e.g., marks[0, 4]) raises an “IndexError”.

Slicing

  • Slicing extracts a portion of an array by specifying start and end indices using the syntax [start:end].
  • The end index is excluded in the slice.
  • Example:

    array8 = np.array([-2, 2, 6, 10, 14, 18, 22])
    array8[3:5]
    # Output: array([10, 14])

  • Reversing an Array: Using a step of -1 in slicing reverses the array:

    array8[::-1]
    # Output: array([22, 18, 14, 10, 6, 2, -2])

Reshaping Arrays

  • The reshape() method changes the shape of an array without altering its data, provided the total number of elements remains the same.
  • Example: Reshaping a 1-D array with 12 elements into different 2-D shapes:

    array3 = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
    array3.reshape(3, 4)
    # Output: array([[10, 11, 12, 13],
    # [14, 15, 16, 17],
    # [18, 19, 20, 21]])

    array3.reshape(2, 6)
    # Output: array([[10, 11, 12, 13, 14, 15],
    # [16, 17, 18, 19, 20, 21]])

Splitting Arrays

  • The numpy.split() function divides an array into multiple sub-arrays along a specified axis.
  • It can split at specific indices or into equal parts.
  • Parameters:
    • indices: A list of indices where the array is split.
    • N: An integer specifying the number of equal parts to split the array into.
    • axis: The axis along which to split (default is 0, i.e., row-wise).
  • Example:

    array4 = np.array([[10, -7, 0, 20],
    [-5, 1, 200, 40],
    [30, 1, -1, 4],
    [1, 2, 0, 4],
    [0, 1, 0, 2]])
    first, second, third = np.split(array4, [1, 3])
    first
    # Output: array([[10, -7, 0, 20]])
    second
    # Output: array([[-5, 1, 200, 40],
    # [30, 1, -1, 4]])
    third
    # Output: array([[1, 2, 0, 4],
    # [0, 1, 0, 2]])

Statistical Operations on Arrays

  • NumPy provides functions for statistical operations on arrays, including maximum, minimum, sum, mean, and standard deviation.
  • max(): Finds the maximum element in an array.
    • For a 1-D array: Returns the overall maximum.
    • For a 2-D array: Returns the overall maximum, or column-wise/row-wise maximum with axis=1 or axis=0.
    • Example:

      arrayA = np.array([1, 0, 2, -3, 6, 8, 4, 7])
      arrayA.max()
      # Output: 8

      arrayB = np.array([[3, 6], [4, 2]])
      arrayB.max()
      # Output: 6
      arrayB.max(axis=1)
      # Output: array([6, 4])
      arrayB.max(axis=0)
      # Output: array([4, 6])

  • min(): Finds the minimum element in an array, similar to max().

    arrayA.min()
    # Output: -3
    arrayB.min()
    # Output: 2
    arrayB.min(axis=0)
    # Output: array([3, 2])

  • sum(): Computes the sum of all elements in an array.
    • Can sum along a specific axis using axis.
    • Example:

      arrayA.sum()
      # Output: 25
      arrayB.sum()
      # Output: 15
      arrayB.sum(axis=1)
      # Output: array([9, 6])

  • mean(): Computes the average of elements in an array.
    • Can compute row-wise or column-wise means using axis.
    • Example:

      arrayA.mean()
      # Output: 3.125
      arrayB.mean()
      # Output: 3.75
      arrayB.mean(axis=0)
      # Output: array([3.5, 4.])

  • std(): Computes the standard deviation of elements in an array.

    arrayA.std()
    # Output: 3.550968177835448

Loading Arrays from Files

  • NumPy provides functions like loadtxt() and genfromtxt() to load data from text files into arrays.
  • loadtxt():
    • Loads data from a text file into a NumPy array.
    • Parameters:
      • skiprows: Skips specified rows (e.g., skiprows=1 skips the header row).
      • delimiter: Specifies the character separating values (e.g., comma, space; default is space).
      • dtype: Specifies the data type of the array (default is float).
      • unpack: If True, transposes the array to extract columns as separate arrays; if False, extracts rows as separate arrays.
    • Example:

      stud1, stud2, stud3, stud4, stud5 = np.loadtxt('C:/NCERT/data.txt', skiprows=1, delimiter=', ', dtype=int)
      stud1
      # Output: array([1, 36, 18, 57])

      rollno, mks1, mks2, mks3 = np.loadtxt('C:/NCERT/data.txt', skiprows=1, delimiter=', ', unpack=True, dtype=int)
      rollno
      # Output: array([1, 2, 3, 4, 5])

  • genfromtxt():
    • Similar to loadtxt() but can handle missing values and non-numeric data.
    • Missing values or non-numeric data in numeric columns are converted to nan (or a specified value using filling_values).
    • Example:

      dataarray = np.genfromtxt('C:/NCERT/dataMissing.txt', skip_header=1, delimiter=', ')
      dataarray
      # Output: array([[1., 36., 18., 57.],
      # [2., nan, 23., 45.],
      # [3., 43., 51., nan],
      # [4., 41., 40., 60.],
      # [5., 13., 18., 27.]])

      dataarray = np.genfromtxt('C:/NCERT/dataMissing.txt', skip_header=1, delimiter=', ', filling_values=-999, dtype=int)
      dataarray
      # Output: array([[1, 36, 18, 57],
      # [2, -999, 23, 45],
      # [3, 43, 51, -999],
      # [4, 41, 40, 60],
      # [5, 13, 18, 27]])

Saving NumPy Arrays in Files on Disk

  • The savetxt() function saves a NumPy array to a text file.
  • Parameters:
    • delimiter: Specifies the character separating values in the file.
    • fmt: Specifies the format for saving data (e.g., %i for integers; default is float).

Summary

  • An array is a data type that holds objects of the same data type (numeric, textual, etc.), stored contiguously in memory.
  • Each element is referenced by the array name and its index.
  • NumPy is a Python library for scientific computing, storing data in powerful n-dimensional arrays.
  • The numpy.array() function returns an ndarray object.
  • Arithmetic operations can be performed on arrays of the same shape.
  • NumPy arrays are fixed in size; their memory allocation cannot be changed once defined.
  • numpy.split() divides an array into multiple sub-arrays along a specified axis.
  • numpy.concatenate() combines multiple arrays into a single array.
  • numpy.loadtxt() and numpy.genfromtxt() load data from files, while savetxt() saves arrays to files.
  • NumPy stands for 'Numerical Python' and is a package designed for data analysis and scientific computing in Python.
The document Introduction to NumPy Chapter Notes | Informatics Practices for Class 11 - Humanities/Arts is a part of the Humanities/Arts Course Informatics Practices for Class 11.
All you need of Humanities/Arts at this link: Humanities/Arts
16 docs

FAQs on Introduction to NumPy Chapter Notes - Informatics Practices for Class 11 - Humanities/Arts

1. What is NumPy and why is it important in Python programming?
Ans. NumPy, short for Numerical Python, is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy is important because it serves as the foundation for many other scientific computing libraries in Python, such as SciPy and Pandas, and it significantly enhances performance in numerical computations due to its optimized, vectorized operations.
2. How do I create a NumPy array?
Ans. You can create a NumPy array using the `numpy.array()` function. For example, to create a 1D array, you can use `np.array([1, 2, 3, 4])`, and for a 2D array, you can use `np.array([[1, 2, 3], [4, 5, 6]])`. You can also create arrays filled with zeros, ones, or random numbers using functions like `np.zeros()`, `np.ones()`, and `np.random.rand()`.
3. What are the different types of indexing available in NumPy?
Ans. NumPy provides several types of indexing: basic indexing, advanced indexing, and boolean indexing. Basic indexing uses integers to index arrays, similar to Python lists. Advanced indexing allows for indexing with arrays of integers, while boolean indexing enables selection of elements based on conditions. For example, you can use `array[array > 5]` to select all elements greater than 5 from an array.
4. How can I sort a NumPy array?
Ans. You can sort a NumPy array using the `numpy.sort()` function or the `sort()` method of the array object. For example, `np.sort(array)` returns a sorted copy of the array, while `array.sort()` sorts the array in place. You can also sort along a specific axis by specifying the `axis` parameter.
5. What is the purpose of `numpy.genfromtxt()`?
Ans. The `numpy.genfromtxt()` function is used to read data from text files, handling missing values and different data types more effectively than `numpy.loadtxt()`. It allows you to specify how to handle missing values and can automatically convert data types based on the content of the file. This function is particularly useful for loading structured data for analysis or processing in scientific computing tasks.
Related Searches

Introduction to NumPy Chapter Notes | Informatics Practices for Class 11 - Humanities/Arts

,

Sample Paper

,

past year papers

,

video lectures

,

ppt

,

Objective type Questions

,

Viva Questions

,

Extra Questions

,

MCQs

,

Summary

,

Previous Year Questions with Solutions

,

mock tests for examination

,

shortcuts and tricks

,

Semester Notes

,

practice quizzes

,

Free

,

Important questions

,

Introduction to NumPy Chapter Notes | Informatics Practices for Class 11 - Humanities/Arts

,

pdf

,

Exam

,

Introduction to NumPy Chapter Notes | Informatics Practices for Class 11 - Humanities/Arts

,

study material

;