質問

I have a numpy array.

[['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']]

How do I shuffle the rows ?

I tried numpy.random.shuffle() but it was returning None probably due to absence of commas

役に立ちましたか?

解決

numpy.random.shuffle is designed to work in-place meaning that it should return None and instead modify your array.

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]]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top