0
0
NumpyHow-ToBeginner ยท 3 min read

How to Do Element Wise Operation in NumPy: Simple Guide

In NumPy, element wise operations are done by applying arithmetic operators directly on arrays using +, -, *, /, etc. These operations apply the calculation to each element individually, producing a new array of the same shape.
๐Ÿ“

Syntax

Element wise operations in NumPy use simple arithmetic operators applied directly to arrays.

  • array1 + array2: Adds each element of array1 to the corresponding element of array2.
  • array1 - array2: Subtracts elements of array2 from array1 element-wise.
  • array1 * array2: Multiplies elements element-wise.
  • array1 / array2: Divides elements element-wise.
  • Operations also work with scalars, applying the operation to each element.
python
import numpy as np

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

# Element wise addition
result_add = array1 + array2

# Element wise multiplication
result_mul = array1 * array2

# Element wise subtraction
result_sub = array1 - array2

# Element wise division
result_div = array1 / array2

# Element wise addition with scalar
result_scalar = array1 + 10
๐Ÿ’ป

Example

This example shows how to add, multiply, subtract, and divide two NumPy arrays element-wise, and how to add a scalar to an array.

python
import numpy as np

array1 = np.array([10, 20, 30])
array2 = np.array([1, 2, 3])

add = array1 + array2
mul = array1 * array2
sub = array1 - array2
div = array1 / array2
scalar_add = array1 + 5

print("Addition:", add)
print("Multiplication:", mul)
print("Subtraction:", sub)
print("Division:", div)
print("Scalar Addition:", scalar_add)
Output
Addition: [11 22 33] Multiplication: [10 40 90] Subtraction: [ 9 18 27] Division: [10. 10. 10.] Scalar Addition: [15 25 35]
โš ๏ธ

Common Pitfalls

Common mistakes include:

  • Trying to operate on arrays of different shapes without broadcasting rules applying, which causes errors.
  • Using Python lists instead of NumPy arrays, which do not support element wise operations directly.
  • Confusing matrix multiplication (@ or np.dot()) with element wise multiplication (*).
python
import numpy as np

# Wrong: lists do not support element wise operations directly
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# This will concatenate lists instead of element wise addition
wrong_result = list1 + list2

# Right: convert lists to arrays first
array1 = np.array(list1)
array2 = np.array(list2)
right_result = array1 + array2

print("Wrong result (list concatenation):", wrong_result)
print("Right result (element wise addition):", right_result)
Output
Wrong result (list concatenation): [1, 2, 3, 4, 5, 6] Right result (element wise addition): [5 7 9]
๐Ÿ“Š

Quick Reference

OperationSymbolDescription
Addition+Adds elements of two arrays element-wise
Subtraction-Subtracts elements element-wise
Multiplication*Multiplies elements element-wise
Division/Divides elements element-wise
Power**Raises elements to the power element-wise
Modulo%Computes remainder element-wise
โœ…

Key Takeaways

Use standard arithmetic operators (+, -, *, /) directly on NumPy arrays for element wise operations.
Element wise operations require arrays to have compatible shapes or use broadcasting.
Convert Python lists to NumPy arrays before performing element wise operations.
Element wise multiplication (*) is different from matrix multiplication (@ or np.dot).
Scalar operations apply the operation to each element of the array.