Executing math.sqrt on numpy structured array column

I have a multilevel structured array. The final column should contain the result of a simple math equation based on other values ​​in the row. The problem is that I am getting the following error when I try to calculate the square root of the equation:

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

The limitation is that I cannot iterate over the array to add values ​​one at a time.

Here's an example to show the error:

import numpy as np
import math

data = np.random.randint(-100, 100, (1, 6, 4))

data[:,3] = math.sqrt((0-data[:,0])**2 + (0-data[:,1])**2 + (0-data[:,2])**2)

      

+3


source to share


1 answer


You just need to use np.sqrt

instead math.sqrt

(the latter only works on single values).



+5


source







All Articles