在Python开头,将一个具有最大元素的Numpy阵列洗牌

发布于 2025-02-12 23:23:14 字数 553 浏览 1 评论 0原文

我正在尝试将数组r1洗牌,以便最大的元素始终位于r2 [0,0]。连接电流和可能的预期输出。

import numpy as np
r1 = np.array([[150.        , 132.5001244 , 115.00024881],
       [ 97.50037321,  80.00049761,  62.50062201],
       [ 45.00074642,  27.50087082,  10.00099522]])
r2=np.random.shuffle(r1)
print("r2 =",r2)

当前输出是

r2 = None

可能的预期输出

array([[150.,62.50062201,115.00024881],  
       [27.50087082,80.00049761,132.5001244]
       [10.00099522,97.50037321,45.00074642]])

I am trying to shuffle the array r1 such that the largest element is always at r2[0,0]. The current and one possible expected output is attached.

import numpy as np
r1 = np.array([[150.        , 132.5001244 , 115.00024881],
       [ 97.50037321,  80.00049761,  62.50062201],
       [ 45.00074642,  27.50087082,  10.00099522]])
r2=np.random.shuffle(r1)
print("r2 =",r2)

The current output is

r2 = None

One possible expected output is

array([[150.,62.50062201,115.00024881],  
       [27.50087082,80.00049761,132.5001244]
       [10.00099522,97.50037321,45.00074642]])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2025-02-19 23:23:14

为了清楚起见,您应该继续进行以下操作:

import numpy as np

r1 = np.array([[150.        , 132.5001244 , 115.00024881],
               [ 97.50037321,  80.00049761,  62.50062201],
               [ 45.00074642,  27.50087082,  10.00099522]])

# make a copy and shuffle the copy
r2 = r1.copy()
np.random.shuffle(r2.ravel())

# get the index of the max
idx = np.unravel_index(r2.argmax(), r2.shape)

# swap first and max
r2[idx], r2[(0, 0)] = r2[(0, 0)], r2[idx]

print(r2)

如果

最初排序阵列,则可以使用阵列的平坦视图:

r1 = np.array([[150.        , 132.5001244 , 115.00024881],
               [ 97.50037321,  80.00049761,  62.50062201],
               [ 45.00074642,  27.50087082,  10.00099522]])

r2 = r1.copy()

# r2.ravel() returns a view of the original array
# so we can shuffle only the items starting from 1
np.random.shuffle(r2.ravel()[1:])

可能的输出:

[[150.          80.00049761  62.50062201]
 [ 97.50037321 132.5001244  115.00024881]
 [ 45.00074642  27.50087082  10.00099522]]

For clarity, here is how you should proceed:

import numpy as np

r1 = np.array([[150.        , 132.5001244 , 115.00024881],
               [ 97.50037321,  80.00049761,  62.50062201],
               [ 45.00074642,  27.50087082,  10.00099522]])

# make a copy and shuffle the copy
r2 = r1.copy()
np.random.shuffle(r2.ravel())

# get the index of the max
idx = np.unravel_index(r2.argmax(), r2.shape)

# swap first and max
r2[idx], r2[(0, 0)] = r2[(0, 0)], r2[idx]

print(r2)

Alternative

If the the array is initially sorted, we can use a flat view of the array:

r1 = np.array([[150.        , 132.5001244 , 115.00024881],
               [ 97.50037321,  80.00049761,  62.50062201],
               [ 45.00074642,  27.50087082,  10.00099522]])

r2 = r1.copy()

# r2.ravel() returns a view of the original array
# so we can shuffle only the items starting from 1
np.random.shuffle(r2.ravel()[1:])

possible output:

[[150.          80.00049761  62.50062201]
 [ 97.50037321 132.5001244  115.00024881]
 [ 45.00074642  27.50087082  10.00099522]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文