[PYTHON] [Numpy] Shuffle ndarray

What's this How to shuffle Numpy ndarray. The indexing of ndarray is for reference only, so you need to make a copy and then assign it.

code

shuffle_ndarray.py


import numpy
import random

#First, create a matrix appropriately
a = [
		[1, 2, 3],
		[5, 6, 7],
		[9, 10, 11],
		[12, 13, 14]
	]
nda = numpy.array(a)

#Shuffle (twice the number of lines)
for i in range(nda.shape[0] * 2) :
	index_a = random.randint(0, nda.shape[0]-1)
	index_b = random.randint(0, nda.shape[0]-1)

	#For reference, both will be the same element
	#nda[index_a], nda[index_b] = nda[index_b], nda[index_a]
	
	#Make a copy and then substitute each
	nda[index_a], nda[index_b] = numpy.array(nda[index_b]), numpy.array(nda[index_a])




Recommended Posts

[Numpy] Shuffle ndarray
Numpy type hint memorandum (NDArray)
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Numpy [Basic]
numpy part 1
numpy tips
About numpy
NumPy axis
Use Numpy
numpy part 2
python: Use your own class for numpy ndarray