Python 多维数组中列的平方和的平方根

Python 多维数组中列的平方和的平方根,python,numpy,Python,Numpy,我将多维列表与numpy一起使用 我有一张单子 l = [[0 2 8] [0 2 7] [0 2 5] [2 4 5] [ 8 4 7]] 我需要求列的平方和的平方根 0 2 8 0 2 7 0 2 5 2 4 5 8 4 7 输出为 l = [sqrt((square(0) + square(0) + square(0) + square(2) + square(8)) sqrt((square(2) + square(2) + square(2) + square(4) + squa

我将多维列表与numpy一起使用

我有一张单子

l = [[0 2 8] [0 2 7] [0 2 5] [2 4 5] [ 8 4 7]]
我需要求列的平方和的平方根

0 2 8
0 2 7
0 2 5
2 4 5
8 4 7
输出为

l = [sqrt((square(0) + square(0) + square(0) + square(2) + square(8))  sqrt((square(2) + square(2) + square(2) + square(4) + square(4)) sqrt((square(8) + square(7) + square(5)) + square(5) + square(7))]

您要做的是使用map/reduce

从理论上讲,可以使用嵌套for循环来完成,但可以用更实用的方式来完成

for l in matrix:
    sum all elements**2 in 
    return the squar root of the sum
一行:

map(lambda x: sqrt(lambda r, z: r + z**2, x), matrix)
但为了更清楚,您可以将其改写为:

def SumOfSquare(lst):
    return reduce(lambda r, x: r + x**2, lst)

def ListOfRoot(lst):
    return map(lambda x: SumOfSquare(x), lst)

s = ListOfRoot(matrix) 
误读了这个问题,它没有numpy。

使用这个标准函数

>>> import numpy as np
>>> np.sum(np.array(l)**2,axis=0)**.5
array([ 10.67707825,   3.46410162,  11.74734012])
import numpy as np
a = np.array([[0, 2, 8], [0, 2, 7], [0, 2, 5], [2, 4, 5], [ 8, 4, 7]])
np.linalg.norm(a,axis=0)
给出:


数组([8.24621125,6.63324958,14.56021978])

我认为内部sqrt应该是一个square@AshwiniChaudhary:对不起,写问题时弄错了。我已经更新了问题。
import numpy as np
a = np.array([[0, 2, 8], [0, 2, 7], [0, 2, 5], [2, 4, 5], [ 8, 4, 7]])
np.linalg.norm(a,axis=0)