0
0
Data Analysis Pythondata~5 mins

Array arithmetic (element-wise) in Data Analysis Python

Choose your learning style9 modes available
Introduction

We use element-wise array arithmetic to do math on each number in a list or table separately. This helps us quickly compare or change many numbers at once.

When you want to add two lists of numbers, like daily sales from two stores.
When you need to multiply each temperature reading by a conversion factor.
When you want to subtract expenses from income for each month.
When you want to divide each student's score by the total possible points.
When you want to apply a discount to every price in a list.
Syntax
Data Analysis Python
import numpy as np

array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Addition
result_add = array1 + array2

# Subtraction
result_sub = array1 - array2

# Multiplication
result_mul = array1 * array2

# Division
result_div = array1 / array2

Both arrays must have the same shape (same number of elements) for element-wise operations.

NumPy arrays allow fast element-wise math without writing loops.

Examples
Adding two empty arrays results in an empty array.
Data Analysis Python
import numpy as np

# Empty arrays
array1 = np.array([])
array2 = np.array([])
result = array1 + array2
print(result)
Subtracting single element arrays returns an array with one element.
Data Analysis Python
import numpy as np

# Single element arrays
array1 = np.array([10])
array2 = np.array([5])
result = array1 - array2
print(result)
Multiplying arrays with zeros results in zeros where multiplied by zero.
Data Analysis Python
import numpy as np

# Arrays with zero
array1 = np.array([0, 2, 4])
array2 = np.array([1, 0, 2])
result = array1 * array2
print(result)
Division returns float results for each element.
Data Analysis Python
import numpy as np

# Division with floats
array1 = np.array([10, 20, 30])
array2 = np.array([2, 5, 10])
result = array1 / array2
print(result)
Sample Program

This program shows how to add, subtract, and divide two arrays element-wise. It prints the sales from two stores, then the total sales, difference, and ratio for each day.

Data Analysis Python
import numpy as np

# Create two arrays representing daily sales from two stores
store1_sales = np.array([100, 150, 200, 250])
store2_sales = np.array([80, 120, 160, 200])

print("Store 1 sales:", store1_sales)
print("Store 2 sales:", store2_sales)

# Calculate total sales each day by adding element-wise
total_sales = store1_sales + store2_sales
print("Total sales each day:", total_sales)

# Calculate difference in sales each day
sales_difference = store1_sales - store2_sales
print("Sales difference each day:", sales_difference)

# Calculate sales ratio (store1 / store2)
sales_ratio = store1_sales / store2_sales
print("Sales ratio each day (store1/store2):", sales_ratio)
OutputSuccess
Important Notes

Element-wise operations run fast because they use optimized NumPy code.

Time complexity is O(n), where n is the number of elements.

Space complexity is O(n) because a new array is created for the result.

Common mistake: Trying to operate on arrays of different sizes without broadcasting causes errors.

Use element-wise operations when you want to do math on each pair of elements separately, not on the whole arrays as single numbers.

Summary

Element-wise array arithmetic lets you do math on each number in two arrays separately.

Arrays must be the same size for these operations to work directly.

NumPy makes these operations simple and fast without loops.