在没有逗号的情况下改组numpy数组

时间:2014-05-08 12:23:20

标签: python python-2.7 python-3.x numpy scipy

我有一个numpy数组。

[['5.1' '3.5' '1.4' '0.2' 'Iris-setosa']
 ['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
 ['5.6' '3.0' '4.5' '1.5' 'Iris-versicolor']
 ['5.8' '2.7' '4.1' '1.0' 'Iris-versicolor']
 ['6.2' '3.4' '5.4' '2.3' 'Iris-virginica']
 ['5.9' '3.0' '5.1' '1.8' 'Iris-virginica']]

如何对行进行随机播放?

我尝试了numpy.random.shuffle(),但由于缺少逗号而返回None

1 个答案:

答案 0 :(得分:2)

numpy.random.shuffle旨在就地,这意味着它应该返回None,而是修改您的数组。

import numpy as np

x = np.arange(9).reshape((3,3))
print(x)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]

np.random.shuffle(x)
print(x)
# [[3 4 5]
#  [0 1 2]
#  [6 7 8]]