# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/indexing-slicing-numpy Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/indexing-slicing-numpy/en.mdx Output docs content for large language models. --- export const metadata = { title: "Indexing and Slicing with NumPy", description: "Learn NumPy indexing and slicing techniques: 1D, 2D arrays, boolean masks, advanced indexing. Complete guide with tested code examples for AI programming.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/20/2025", subject: "AI Programming", }; ## Basic Indexing and Slicing Concepts Indexing and slicing are your tools for extracting specific data from NumPy arrays. Think of indexing as pointing to exact locations, while slicing grabs entire sections in one go. Unlike Python lists that create copies, NumPy slicing creates views that share memory with the original array. This memory-sharing behavior makes NumPy incredibly efficient but requires careful attention to avoid unintended modifications. The [NumPy indexing documentation](https://numpy.org/doc/stable/user/basics.indexing.html) provides extensive examples of advanced indexing patterns when you need more sophisticated data selection. ## 1D Array Operations One-dimensional arrays use the same syntax as Python lists. Slicing returns a view of the data, not a copy, so changes to the view will affect the original array. An important concept in NumPy slicing is the difference between view and copy. Views share memory with the original array, while copies are independent duplicates. ## Multidimensional Array Operations Multidimensional arrays use integer tuples for indexing. Each dimension is separated by commas within square brackets. Assignment and slicing can be combined for complex operations. Advanced slicing allows extraction of subarrays with complex patterns: ### 2D Operations Summary | Operation | Description | Example Result | |-----------|-------------|----------------| | `a[2,1]` | Element at row 2, column 1 | Single value: `8.0` | | `a[1,:]` | Entire row 1 | 1D Array: `[4. 5. 6.]` | | `a[:,2]` | Entire column 2 | 1D Array: `[3. 6. 9. 12.]` | | `a[1:3, 0:2]` | Subarray rows 1-2, columns 0-1 | 2D Array: `[[4. 5.], [7. 8.]]` | | `a[::2, :]` | Every second row | 2D Array with rows 0, 2 | | `a[:, ::2]` | Every second column | 2D Array with columns 0, 2 | ## Advanced Indexing Advanced indexing allows the use of integer or boolean arrays to access elements with complex patterns. Indexing with integer arrays creates new copies rather than views. Copies have the same shape as the index array. Boolean masks are powerful techniques for filtering data based on specific conditions. Indexing with boolean arrays takes elements where True values are found. 1) & (a < 4) print("Complex mask (a > 1) & (a < 4):", complex_mask) # Output: Complex mask (a > 1) & (a < 4): [False False True True False False] print("a[complex_mask]:", a[complex_mask]) # Output: a[complex_mask]: [2. 3.]` } ]} /> Advanced indexing can be combined with regular slicing for very flexible operations: ## Advanced Techniques NumPy provides various slicing techniques that enable efficient and flexible data manipulation. Step parameters allow you to take elements at specific intervals, such as taking every second or third element. Ellipsis (`...`) is shorthand for full slice on unspecified dimensions. Very useful for high-dimensional arrays. ## Boolean Indexing Boolean indexing uses boolean arrays to filter elements based on specific conditions. True indexes take elements in the target array, while False ignores them. 2 print("a > 2:", greater_than_2) # Output: a > 2: [False False False True True True] print("a[a > 2]:", a[a > 2]) # Output: a[a > 2]: [3. 4. 5.] # Boolean mask with compound conditions even_and_greater_than_1 = (a % 2 == 0) & (a > 1) print("(a % 2 == 0) & (a > 1):", even_and_greater_than_1) # Output: (a % 2 == 0) & (a > 1): [False False True False True False] print("a[even_and_greater_than_1]:", a[even_and_greater_than_1]) # Output: a[even_and_greater_than_1]: [2. 4.]` } ]} /> Boolean indexing on 2D arrays can be applied for filtering elements based on conditions: 5 print("Mask a > 5:") print(mask) # Output: # [[False False False False] # [False False True True] # [ True True True True]] print("a[mask]:", a[mask]) # Output: a[mask]: [ 6 7 8 9 10 11] # Boolean indexing with assignment a[a < 5] = 0 print("After a[a < 5] = 0:") print(a) # Output: # [[ 0 0 0 0] # [ 0 0 6 7] # [ 8 9 10 11]]` } ]} /> ## Fancy Indexing Fancy indexing uses integer arrays to access elements with specific order or patterns. This technique is very useful for data sampling and reorganization. Fancy indexing on 2D arrays enables selection of rows and columns with complex patterns: ## Practical Applications Indexing and slicing have many practical applications in data analysis and machine learning. Filtering techniques are very useful for data preprocessing in machine learning and statistical analysis: = 20) & (temperatures <= 28) normal_temps = temperatures[normal_temp_mask] print("Normal temperatures:", normal_temps) # Output: Normal temperatures: [22.5 25.1 27.3 24.8] # Filter extreme temperatures extreme_temp_mask = (temperatures < 20) | (temperatures > 30) extreme_temps = temperatures[extreme_temp_mask] print("Extreme temperatures:", extreme_temps) # Output: Extreme temperatures: [19.8 30.2 18.5 31.1] # Replace extreme values with average mean_temp = temperatures[normal_temp_mask].mean() temperatures_cleaned = temperatures.copy() temperatures_cleaned[extreme_temp_mask] = mean_temp print("Data after cleaning:", temperatures_cleaned) # Output: Data after cleaning: [22.5 25.1 24.925 24.925 24.925 27.3 24.925 24.8 ]` } ]} /> Sampling and data reorganization are important techniques for machine learning and large dataset analysis: