# NumPy 3d translation matrix

Here's how to translate 3d points in Python using a translation matrix.

To translate a series of points in three dimensions in Cartesian space (x, y, z) you first need to "homogenize" the points by adding a value to their projective dimension—which we'll set to one to maintain the point's original coordinates, and then multiply our point cloud using NumPy's `np.matmul` method by a transformation matrix constructed from a (4, 4) identity matrix with three translation parameters in its bottom row (tx, ty, tz).

## Steps

Here's a breakdown of the steps.

• Import the NumPy Python library
• Define a point cloud with Cartesian coordinates (x, y, z)
• Convert the points to homogeneous coordinates (x, y, z, w)
• Define our translation parameters (tx, ty, tz)
• Construct the translation matrix
• Multiply the homogenized point cloud by the transformation matrix with NumPy's `np.matmul`

## Code

``````# translate.py
import numpy as np

# Define a set of Cartesian (x, y, z) points
point_cloud = [
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 1, 1],
[1, 2, 3],
]

# Convert to homogeneous coordinates
point_cloud_homogeneous = []
for point in point_cloud:
point_homogeneous = point.copy()
point_homogeneous.append(1)
point_cloud_homogeneous.append(point_homogeneous)

# Define the translation
tx = 2
ty = 10
tz = 100

# Construct the translation matrix
translation_matrix = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[tx, ty, tz, 1],
]

# Apply the transformation to our point cloud
translated_points = np.matmul(
point_cloud_homogeneous,
translation_matrix)

# Convert to cartesian coordinates
translated_points_xyz = []
for point in translated_points:
point = np.array(point[:-1])
translated_points_xyz.append(point)

# Map original to translated point coordinates
# (x0, y0, z0) → (x1, y1, z1)
for i in range(len(point_cloud)):
point = point_cloud[i]
translated_point = translated_points_xyz[i]
print(f'{point} → {list(translated_point)}')
``````

# [Solved] Object of type ndarray is not JSON serializable

If you try to serialize a NumPy array to JSON in Python, you'll get the error below.

``````TypeError: Object of type ndarray is not JSON serializable
``````

Luckily, NumPy has a built-in method to convert one- or multi-dimensional arrays to lists, which are in turn JSON serializable.

``````import numpy as np
import json

arr = np.array([[100,200],[300,400]])

# Convert the array to list
arr_as_list = arr.tolist()

# Serialize as JSON
json.dumps(arr_as_list)
# '[[100, 200], [300, 400]]'
``````

# Return a NumPy ndarray from AWS Lambda

Here's the error I was getting when trying to return a NumPy `ndarray` in the response body of an AWS Lambda function.

``````Object of type ndarray is not JSON serializable
``````

## Reproduce the error

``````import numpy as np
import json

# A NumPy array
arr = np.array([[1,2,3],[4,5,6]])
.astype(np.float64)

# Serialize the array
json.dumps(arr)
# TypeError: Object of type ndarray is not JSON serializable
``````

## Solution

NumPy arrays provide a built-in method to convert them to lists called `.tolist()`.

``````import numpy as np
import json

# A NumPy array
arr = np.array([[1,2,3],[4,5,6.78]])
.astype(np.float64)

# Convert the NumPy array to a list
arr_as_list = arr.tolist()

# Serialize the list
json.dumps(arr_as_list)
``````

Want to see older publications? Visit the archive.

Listen to Getting Simple .