首页 > 技术文章 > python之numpy.power()数组元素求n次方

luozeng 2018-03-11 13:47 原文

  • numpy.power(x1, x2)

  • 数组的元素分别求n次方。x2可以是数字,也可以是数组,但是x1和x2的列数要相同。

  • 1 >>> x1 = range(6)
    2 >>> x1
    3 [0, 1, 2, 3, 4, 5]
    4 >>> np.power(x1, 3)
    5 array([  0,   1,   8,  27,  64, 125])
    1 >>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
    2 >>> np.power(x1, x2)
    3 array([  0.,   1.,   8.,  27.,  16.,   5.])
    1 >>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
    2 >>> x2
    3 array([[1, 2, 3, 3, 2, 1],
    4        [1, 2, 3, 3, 2, 1]])
    5 >>> np.power(x1, x2)
    6 array([[ 0,  1,  8, 27, 16,  5],
    7        [ 0,  1,  8, 27, 16,  5]])

     

推荐阅读