简体   繁体   中英

Performing math.sqrt on numpy structured array column

I have a numpy structured array. The final column needs to contain the result of a simple math equation based on the other values in the row. Problem is I get the following error when trying to calculate the square root portion of the equation:

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

Limitation is that I cannot iterate the array to add the 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)

您只需要使用np.sqrt而不是math.sqrt (后者仅适用于单个值)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM