Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print a checkboard pattern of n*n using numpy.
The checkboard pattern is a square grid composed of alternating 0s and 1s, arranged in a way that no two adjacent cells have the same value. It looks like a chessboard, where black and white squares alternate in every row and column.
This pattern is commonly used in chess, checkers, image processing, graphics, and visualization. In this article, we'll learn how to create a checkboard pattern of n×n using NumPy.
Using numpy.indices() Method
The numpy.indices() method returns a grid of indices with the given shape. It creates coordinate matrices from coordinate vectors that we can use to generate patterns.
Syntax
numpy.indices(dimensions, dtype=int)
How It Works
The checkboard pattern is created by:
- Generating coordinate grids using
np.indices() - Summing the row and column indices
- Taking modulo 2 to create alternating 0s and 1s
Example 1: 2×2 Checkboard
Here's how to create a simple 2×2 checkboard pattern ?
import numpy as np
def create_checkboard(n):
# Create coordinate grids
indices = np.indices((n, n))
# Sum row and column indices, then take modulo 2
result = (indices.sum(axis=0)) % 2
return result
# Create 2x2 checkboard
pattern = create_checkboard(2)
print("2×2 Checkboard:")
print(pattern)
2×2 Checkboard: [[0 1] [1 0]]
Example 2: 4×4 Checkboard
Let's create a larger 4×4 checkboard pattern ?
import numpy as np
def create_checkboard(n):
indices = np.indices((n, n))
result = (indices.sum(axis=0)) % 2
return result
# Create 4x4 checkboard
pattern = create_checkboard(4)
print("4×4 Checkboard:")
print(pattern)
4×4 Checkboard: [[0 1 0 1] [1 0 1 0] [0 1 0 1] [1 0 1 0]]
Example 3: 6×6 Checkboard with Visualization
Here's a complete example that creates and displays a 6×6 checkboard ?
import numpy as np
def create_checkboard(n):
"""Create an n×n checkboard pattern using numpy.indices()"""
indices = np.indices((n, n))
checkboard = (indices.sum(axis=0)) % 2
return checkboard
# Create 6x6 checkboard
size = 6
board = create_checkboard(size)
print(f"{size}×{size} Checkboard Pattern:")
print(board)
print(f"\nShape: {board.shape}")
print(f"Data type: {board.dtype}")
6×6 Checkboard Pattern: [[0 1 0 1 0 1] [1 0 1 0 1 0] [0 1 0 1 0 1] [1 0 1 0 1 0] [0 1 0 1 0 1] [1 0 1 0 1 0]] Shape: (6, 6) Data type: int64
Alternative Approach: Using numpy.mgrid
You can also create a checkboard pattern using numpy.mgrid ?
import numpy as np
def create_checkboard_mgrid(n):
"""Alternative method using numpy.mgrid"""
x, y = np.mgrid[0:n, 0:n]
checkboard = (x + y) % 2
return checkboard
# Create 3x3 checkboard using mgrid
pattern = create_checkboard_mgrid(3)
print("3×3 Checkboard using mgrid:")
print(pattern)
3×3 Checkboard using mgrid: [[0 1 0] [1 0 1] [0 1 0]]
Conclusion
Creating checkboard patterns in NumPy is straightforward using np.indices() or np.mgrid. The key is summing coordinate indices and taking modulo 2 to create alternating values. This technique is useful for graphics, image processing, and creating test patterns.
