Chapter 6: Introduction to NumPy
6.1 Introduction
NumPy stands for "Numerical Python" and is a powerful library used for data analysis and scientific computing in Python.
It provides a special data structure called an ndarray (N-dimensional array) that allows for efficient data manipulation and computation.
NumPy integrates easily with other Python libraries and programming languages like C and C++.
6.2 Array
An array is a data structure used to store multiple values under a single identifier, where each value is of the same data type and accessed by an index.
Characteristics of an Array:
Elements must be of the same data type.
Stored contiguously in memory, making operations faster.
Uses zero-based indexing, like lists in Python.
6.3 NumPy Array
NumPy Array: Known as ndarray, it is a versatile data structure used to store numerical data, vectors, and matrices.
Difference Between Lists and Arrays:
Arrays only hold data of a single type, while lists can hold mixed types.
Arrays are more memory-efficient and support faster, element-wise operations.
Lists are part of core Python; arrays are from the NumPy library.
Creating a NumPy Array from a List
Use the np.array() function to convert a list into a NumPy array.
Example:
python
Copy code
import numpy as np
array1 = np.array([10, 20, 30])
Types of Arrays
1-D Array: A single row of elements.
2-D Array: A matrix with rows and columns.
Attributes of NumPy Arrays
ndim: Number of dimensions.
shape: Size of each dimension (rows and columns).
size: Total number of elements.
dtype: Data type of elements.
itemsize: Memory size in bytes of each element.
6.4 Indexing and Slicing
Indexing: Used to access elements in an array. In 2-D arrays, indexing uses [row, column].
Slicing: Extracts a portion of the array using array[start:end] syntax.
6.5 Operations on Arrays
Arithmetic Operations: Element-wise addition, subtraction, multiplication, division, and more.
Example:
python
Copy code
array1 = np.array([3, 6])
array2 = np.array([10, 20])
result = array1 + array2Transpose: Converts rows to columns and vice versa.
Sorting: Sorts elements either by rows or columns.
6.6 Concatenating Arrays
Concatenation: Combines multiple arrays along a specified axis using np.concatenate().
6.7 Reshaping Arrays
Reshaping: Changes the shape of an array without changing its data. The number of elements must remain the same.
6.8 Splitting Arrays
Splitting: Divides an array into multiple subarrays with the np.split() function.
6.9 Statistical Operations on Arrays
Common Operations:
max(): Finds the maximum element.
min(): Finds the minimum element.
sum(): Calculates the sum of elements.
mean(): Finds the average.
std(): Calculates the standard deviation.
6.10 Loading Arrays from Files
Loading Data: Use np.loadtxt() and np.genfromtxt() to load data from text files, commonly used for handling CSV files.
6.11 Saving NumPy Arrays in Files
Saving Data: Use np.savetxt() to store array data into a text file for later use.