Introduction
• Overview of NumPy in Data Analytics
• Importance of NumPy for numerical
computations
Creating an Array
• Using np.array() to create an ndarray
• Examples with 1D, 2D, and 3D arrays
• Example:
• import numpy as np
• arr = np.array([1, 2, 3, 4, 5])
• print(arr)
Intrinsic Creation of an Array
• np.zeros(), np.ones(), np.eye(), np.arange(),
np.linspace()
• Random arrays using np.random module
• Example:
• zeros_arr = np.zeros((2,2))
• ones_arr = np.ones((3,3))
• random_arr = np.random.rand(3,3)
• print(zeros_arr, ones_arr, random_arr)
Data Types in NumPy
• Common data types: int32, float64, bool,
complex
• Checking and converting data types (dtype,
astype())
• Example:
• arr = np.array([1.5, 2.7, 3.1],
dtype=np.float32)
• print(arr.dtype)
Basic Operations
• Element-wise arithmetic operations
• Broadcasting rules
• Universal functions (ufuncs)
• Example:
• arr1 = np.array([1, 2, 3])
• arr2 = np.array([4, 5, 6])
• result = arr1 + arr2
• print(result)
Aggregate Functions
• sum(), mean(), min(), max(), std()
• Axis-based aggregations
• Example:
• arr = np.array([[1, 2, 3], [4, 5, 6]])
• print(np.sum(arr, axis=0))
Indexing and Slicing
• Accessing array elements
• Slicing and step size
• Boolean indexing and fancy indexing
• Example:
• arr = np.array([10, 20, 30, 40, 50])
• print(arr[1:4])
Iterating Over Arrays
• Using loops with NumPy arrays
• np.nditer() for efficient iteration
• Example:
• arr = np.array([1, 2, 3, 4])
• for x in np.nditer(arr):
• print(x)
Conditions and Boolean Arrays
• Applying conditions (> , < , ==)
• np.where() for conditional operations
• Example:
• arr = np.array([1, 2, 3, 4, 5])
• print(arr[arr > 2])
Array Manipulation
• Joining arrays (np.concatenate(), np.hstack(),
np.vstack())
• Splitting arrays (np.split(), np.hsplit(),
np.vsplit())
• Changing shape (reshape(), ravel(), flatten())
• Sorting arrays (np.sort(), argsort())
• Example:
• arr = np.array([[3, 2, 1], [6, 5, 4]])
• print(np.sort(arr))
Structured Arrays
• Defining structured arrays with multiple data
types
• Accessing fields in structured arrays
• Example:
• dtype = [('name', 'U10'), ('age', 'i4')]
• data = np.array([('Alice', 25), ('Bob', 30)],
dtype=dtype)
• print(data['name'])
Reading and Writing Array Data
• Saving arrays (np.save(), np.savetxt())
• Loading arrays (np.load(), np.loadtxt())
• Example:
• np.save('array.npy', arr)
• loaded_arr = np.load('array.npy')
• print(loaded_arr)
NumPy in Real-World Applications
• Data preprocessing for machine learning
• Statistical analysis and modeling
• Integration with Pandas and SciPy
• Example:
• import pandas as pd
• df = pd.DataFrame(arr, columns=['Values'])
• print(df)