shaare.it

TypeError: only size-1 arrays can be converted to Python scalars (alternate)

7 Dec 2025

1 min read

TypeError: only size-1 arrays can be converted to Python scalars

$ python -c "import numpy as np; np.float64(np.array([1,2]))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars

Why this happens

A conversion to a Python scalar (float, int) is requested but the source is an array with multiple elements.

Fix

Index into the array to get a single element, or reduce it with a summary operation.

Wrong code

import numpy as np
arr = np.array([1,2])
val = float(arr)

Fixed code

import numpy as np
arr = np.array([1,2])
val = float(arr[0])
# or
val = arr.mean().item()