简体   繁体   English

将多维 numpy 数组中的列提高到 2 次方

[英]Raise a column in a multi-dimensional numpy array to 2nd power

I have no idea how to raise a column to the 2nd power, I've done some googling and nothing came up.我不知道如何将一个列提高到 2 次方,我已经做了一些谷歌搜索,但没有任何结果。 I've tried: X[:,-1:0] = X[:,-1:0] ** 2 , when X is a 47*3 numpy matrix, and I want to raise the last column to the 2nd power and it didn't work.我试过: X[:,-1:0] = X[:,-1:0] ** 2 ,当X是 47*3 numpy 矩阵时,我想将最后一列提高到 2 次方它没有用。

Unless I've misunderstood your problem, a simple for loop should suffice除非我误解了您的问题,否则一个简单的for循环就足够了

from numpy import array
a = array([[1, 3, 5], [2, 3, 6], [4, 3, 9]])
for r in a:
    r[-1] = r[-1]**2
print(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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