Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Binary Operators Explained – Element-wise Arithmetic in Python Arrays

NumPy Binary Operators

Binary operators in NumPy allow you to perform mathematical operations between arrays or between an array and a scalar.

Instead of using loops, NumPy performs operations element by element, making it extremely fast and efficient.

These are called Binary Operators.


What are Binary Operators in NumPy?

Binary operators are operators that work on two operands, such as:

  • Array + Array
  • Array - Array
  • Array * Array
  • Array / Array

Each operation is applied element-wise.


Why Use Binary Operators?

  • Faster than Python loops
  • Simple syntax
  • Supports large datasets
  • Used in data science & ML
  • Easy mathematical computation

1. Addition (+ Operator)

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b)

Output

[5 7 9]

2. Subtraction (- Operator)

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])

print(a - b)

Output

[ 9 18 27]

3. Multiplication (* Operator)

a = np.array([2, 3, 4])
b = np.array([5, 6, 7])

print(a * b)

Output

[10 18 28]

4. Division (/ Operator)

a = np.array([10, 20, 30])
b = np.array([2, 5, 10])

print(a / b)

Output

[5. 4. 3.]

5. Floor Division (// Operator)

a = np.array([10, 20, 30])
b = np.array([3, 4, 7])

print(a // b)

Output

[3 5 4]

6. Modulus (%) Operator

a = np.array([10, 20, 30])
b = np.array([3, 4, 6])

print(a % b)

Output

[1 0 0]

7. Power (** Operator)

a = np.array([2, 3, 4])

print(a ** 2)

Output

[ 4  9 16]

Binary Operations with Scalar

You can also perform operations with a single number.


Example

a = np.array([1, 2, 3])

print(a + 10)
print(a * 5)

Output

[11 12 13]
[ 5 10 15]

How Binary Operations Work

NumPy applies operations element by element:

a = [1, 2, 3]
b = [4, 5, 6]

a + b = [1+4, 2+5, 3+6]
= [5, 7, 9]

Binary Operators in 2D Arrays

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

print(a + b)

Output

[[ 6  8]
[10 12]]

Real-World Example: Sales Data

sales = np.array([100, 200, 300])
tax = np.array([10, 20, 30])

total = sales + tax

print(total)

Output

[110 220 330]

Real-World Example: Image Processing

image = np.array([10, 20, 30])
brightness = image + 50

print(brightness)

Binary Operator Table

OperatorMeaningExample
+    Addition    a + b
-    Subtraction    a - b
*    Multiplication    a * b
/    Division    a / b
//    Floor Division    a // b
%    Modulus    a % b
**    Power    a ** 2

Advantages of NumPy Binary Operators

  • Fast execution
  • No loops required
  • Clean and readable code
  • Works with large datasets
  • Essential for data science

Summary

NumPy binary operators allow element-wise mathematical operations between arrays or scalars. They include addition, subtraction, multiplication, division, modulus, and exponentiation.

These operations are core features of NumPy and are widely used in data processing and AI applications built with Python.


Conclusion

Mastering binary operators helps you perform fast and efficient numerical computations in NumPy. Whether you're working in data science, machine learning, or analytics, these operations are essential tools for array manipulation.




Post a Comment

0 Comments