# Nakafa Framework: LLM URL: https://nakafa.com/en/subject/university/bachelor/ai-ds/ai-programming/array-operation-numpy Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/array-operation-numpy/en.mdx Output docs content for large language models. --- export const metadata = { title: "Array Operations with NumPy", description: "Master NumPy array operations: broadcasting, vectorization, arithmetic, statistics, and shape manipulation with practical examples for data science.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/20/2025", subject: "AI Programming", }; ## Broadcasting in NumPy Broadcasting is NumPy's smart way of handling operations between arrays of different sizes. Think of it like a universal translator that automatically figures out how to make mismatched arrays work together in calculations. You can explore more advanced broadcasting techniques in the [NumPy broadcasting guide](https://numpy.org/doc/stable/user/basics.broadcasting.html) when you're ready for complex scenarios. Vectorization allows operations on entire arrays without writing loops, making calculations incredibly fast by processing elements simultaneously at the hardware level. In the example above, NumPy automatically adds each element at the same position from both arrays. No need to write loops to access each element one by one. ### Broadcasting Rules Broadcasting is a rule system that allows NumPy to perform operations on arrays with different shapes. Like when you want to add the same number to all elements in a list, NumPy can do it automatically. There are three main rules in broadcasting: 1. **Rule 1**: If dimensions differ, add dimensions of size 1 from the left on the array with smaller dimensions 2. **Rule 2**: Stretch dimensions of size 1 to match the corresponding dimension values of the other array 3. **Rule 3**: If shapes are incompatible, an error will occur ### Array with Scalar ### 2D Array with 1D When you work with arrays that have different dimensions, NumPy will try to automatically adjust their shapes. This process is very useful when you want to apply the same operation to each row or column of a matrix. expanded to (1, 3) -> (3, 3) # b is added to each row of a` } ]} /> ### Failed Broadcasting Case ## Array Arithmetic Operations NumPy provides various arithmetic operations that can be applied to arrays. These operations work element-wise, similar to a calculator that can compute many numbers simultaneously. When you perform arithmetic operations with scalars, NumPy will apply that operation to every element in the array. This is very efficient because you don't need to write manual loops. ### Operations Between Arrays It's important to understand the difference between element-wise multiplication (`*`) and matrix multiplication (`@` or `np.dot()`). Element-wise multiplication multiplies elements at the same position, while matrix multiplication follows linear algebra rules. ### Comparison and Logic NumPy also supports comparison operations that produce boolean arrays. These operations are very useful for data filtering or creating complex conditions. 2: {a > 2}") # Output: [False False False True True] print("Equal comparison:") print(f"a == b: {a == b}") # Output: [ True False True False True] # Logical operations print("Logical OR operation:") print(f"(a > 2) | (a == b): {(a > 2) | (a == b)}") # Output: [ True False True True True]` } ]} /> Logical operators in NumPy use special symbols. Use `~` for NOT, `&` for AND, and `|` for OR, not regular Python operators like `not`, `and`, `or`. ## Statistical Functions and Reductions Reduction functions allow you to calculate a single value from an entire array or along a specific axis. Imagine you have a table of exam scores and want to calculate the average for each subject or for each student. NumPy provides various statistical functions that are very useful for data analysis. These functions can be applied to the entire array or only to specific axes. ### Operations with Axes The concept of axes in NumPy is very important. For 2D arrays, `axis=0` means operations are performed along rows (producing values for each column), while `axis=1` means operations are performed along columns (producing values for each row). Understanding axes helps you control how statistical functions work on multidimensional data. For example, if you have monthly sales data for various products, you can calculate total sales per product or per month. ## Array Shape Manipulation Array shape manipulation allows you to change the dimensions and structure of data without changing its contents. Like rearranging books on a shelf, you can arrange them in different rows without adding or reducing the number of books. NumPy stores multidimensional arrays internally as one-dimensional arrays with row-major order (elements at the last index are stored sequentially). Understanding this is important for reshape and flatten operations. ### Flatten and Ravel Both `flatten()` and `ravel()` functions convert multidimensional arrays to 1D arrays, but in different ways. Flatten creates a new copy of the data, while ravel tries to create a more memory-efficient view. ### Reshape and Resize Reshape operation allows you to change the array shape without changing its data, as long as the total number of elements remains the same. While resize modifies the array directly (in-place). ### Transpose Transpose is an operation that flips array axes, very useful in linear algebra operations. NumPy provides two ways to perform transpose: using the `transpose()` method or the shorter `.T` attribute. ## Data Standardization with Z-Transform Z-Transform is a standardization technique that transforms data to have a mean of 0 and standard deviation of 1. This technique is very useful in machine learning to ensure all features have the same scale. The Z-Transform formula is , where: - is the feature matrix of size - is the number of observations (rows) - is the number of features (columns) - is the mean vector for each column - is the standard deviation vector for each column This standardization process ensures that each feature contributes equally in machine learning algorithms, regardless of their original data scale. For example, if you have height data in centimeters and weight data in kilograms, standardization will make both have the same influence in the model. For complete documentation and more information about NumPy array operations, you can visit the [official NumPy documentation](https://numpy.org/doc/stable/user/basics.html) which provides comprehensive guides and practical examples.