NumPy Lecture Notes PDF

Document Details

AuthoritativeZebra315

Uploaded by AuthoritativeZebra315

Twisha Patel, Ishita Akolkar

Tags

NumPy Numerical Python Arrays Mathematics

Summary

These lecture notes cover the NumPy library for numerical computing in Python. The material discusses array manipulation, creating arrays from various data types (lists, tuples, etc.), mathematical functions, and more. The notes provide code samples and detailed explanations.

Full Transcript

NumPy. TWISHA PATEL ISHITA AKOLKAR LECTURE - 6/7 NUMERICAL PYTHON NumPy Notes. Numerical Python Extensively used for array operations A large collection of mathematical functions to operate To install numpy on local dev...

NumPy. TWISHA PATEL ISHITA AKOLKAR LECTURE - 6/7 NUMERICAL PYTHON NumPy Notes. Numerical Python Extensively used for array operations A large collection of mathematical functions to operate To install numpy on local device: pip install numpy or conda install numpy To use numpy: import numpy as np import numpy as np print("Welcome to the world of numeric python.") Welcome to the world of numeric python. Why NumPy? 1. NumPy Arrays are faster than lists. How is that achieved? – Locality of Reference: Stored at one continuous place – Certain parts of the library are written in C/C++ to achieve higher speeds. 2. Rich Function Library 3. Interoperability: Other useful libraries such as Pandas, scikit-learn, and SciPy are built on top of NumPy. 4. Use Cases: – Data Analysis – Machine Learning Array vs List NumPy arrays are more efficient than Python lists for numerical operations. They have a fixed size, support element-wise operations, and are implemented in C. 1. NumPy Array: ndarray: N-Dimensional Array defined in numpy Collection of items of same type Zero- based syntax 1.1 Instantiation of ndarray: Basic instantiation: a= np.array(list | tuple | set | dict) arr = np.array([1, 2, 3]) print(arr) [1 2 3] arr2 = np.array([[1,2], [3,4]]) print(arr2) [[1 2] [3 4]] Other ways: 1. arange(start,end,step): Array starting from Start till end (not included) with specified steps. range_arr = np.arange(0, 10, 2) print(range_arr) [0 2 4 6 8] 1. zeros(n): Array filled with zeros of n shape zero_arr = np.zeros((2, 3)) print(zero_arr) [[0. 0. 0.] [0. 0. 0.]] 1. ones(n): Array filled with ones of n shape one_arr = np.ones((2, 3)) print(one_arr) [[1. 1. 1.] [1. 1. 1.]] 1. eye(n): 2-D NumPy array with ones on the diagonal and zeros elsewhere [n rows and columns] identity = np.eye(4) print(identity) [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] 1. linspace(start, stop, n): Evenly spaced n numbers from from start to stop (not included) linspace_arr = np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1. ] print(linspace_arr) [0. 0.25 0.5 0.75 1. ] Keep in Mind! --> In arange, end isn't included but in linspace stop is. # Difference in arange and linspace: range_arr = np.arange(0, 10, 2) print("Using arange:", range_arr) linspace_arr = np.linspace(0, 10, 5) print("Using linspace:", linspace_arr) Using arange: [0 2 4 6 8] Using linspace: [ 0. 2.5 5. 7.5 10. ] Notice how while the end limit is 10 in both the cases, arange array doesn't include 10, but linspace array does. 1.2 Attributes of an ndarray: 1.2.1 Dimensions of the array Number of dimensions (axes) of the array. arr.ndim print("Array:", arr) print("Dimension: ",arr.ndim) print("Array:\n", arr2) print("Dimension: ",arr2.ndim) Array: [1 2 3] Dimension: 1 Array: [[1 2] [3 4]] Dimension: 2 1.2.2 Shape: Number of elements in each dimension. arr.shape print("1D array:", arr) print("Shape:", arr.shape) 1D array: [1 2 3] Shape: (3,) print("2D array:\n", identity) print("Shape:", identity.shape) 2D array: [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] Shape: (4, 4) Reshaping an array: arr.reshape(new shape): Change the current shapr arr.flatten(): Reduce the current dimension to a 1 ogarr= np.arange(0,10) reshaped_arr = ogarr.reshape(5, 2) print("Original Array:", ogarr) print("Reshaped Array:\n", reshaped_arr) print("Flattened Array:", reshaped_arr.flatten()) Original Array: [0 1 2 3 4 5 6 7 8 9] Reshaped Array: [[0 1] [2 3] [4 5] [6 7] [8 9]] Flattened Array: [0 1 2 3 4 5 6 7 8 9] 1.2.3 Size Returns the total number of elements in the array. arr.size print("Array: ",arr,"\nSize:",arr.size) print("Array:\n",arr2,"\nSize:",arr2.size) Array: [1 2 3] Size: 3 Array: [[1 2] [3 4]] Size: 4 1.2.4 Data Type Returns the data type of the elements in the array arr.dtype Types: – Integer Data Types: int32, int64 – Floating-Point Data Types: float32, float64 – Boolean Data Type: bool – Complex Data Types: complex64, complex128 – String Data Type: str_ (e.g.,

Use Quizgecko on...
Browser
Browser