NumPy — Array Slicing
Key attributes
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(f"Shape: {arr.shape}") # (2, 3)
print(f"Size: {arr.size}") # 6
print(f"Ndim: {arr.ndim}") # 2
print(f"Dtype: {arr.dtype}") # int64
print(f"Itemsize: {arr.itemsize}") # 8 bytes
reshape
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
flatten
arr_2d = np.array([[1, 2], [3, 4]])
flat = arr_2d.flatten()
print(flat) # [1, 2, 3, 4]
T (transpose)
arr = np.array([[1, 2], [3, 4]])
print(arr.T)
dtype
arr = np.array([1, 2, 3], dtype=np.float32)
print(arr.dtype) # float32
Mini Practice
- Check shape and size
- Reshape arrays
- Transpose matrices
- Work with dtypes
Up Next
Continue with Indexing - Accessing elements.
Related Topics
Frequently Asked Questions about Array Slicing
What is Array Slicing in NumPy?
Array Slicing is a fundamental concept in NumPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Array Slicing?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Array Slicing.
Why is Array Slicing important in NumPy?
Array Slicing is essential for NumPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.