Shuffle two lists in the same order in python

tags: python  shuffle  Same order  list

   Usually when doing machine learning problems, you need to prepare training data, usually the sample data and labels are stored in 2 lists, such as train_x = [ x 1 , x 2 , . . . , x N ] ,train_y = [ y 1 , y 2 , . . . , y N ] . Sometimes it is necessary to shuffle the data before processing (for example, batch gradient descent algorithm, the data needs to be shuffled). At this time, you need to shuffle the two lists in the same order, so how to achieve it in python? This can be achieved by setting the same random seed and then shuffle. code show as below:

import random

randnum = random.randint(0,100)
random.seed(randnum)
random.shuffle(train_x)
random.seed(randnum)
random.shuffle(train_y)

Intelligent Recommendation

Python - two lists get the same element

Article catalog Python - two lists get the same element Python - two lists get the same element Method 1: Turn into a tuple - then A & B Method 2 (While cycle method): Method 3: (FO cycle implemen...

Python judges the same elements of the two lists

Mainly use list derivatives Give an example Example 2 Example Example...

Put two sets of data together in python shuffle in a fixed order

Sometimes two sets of data, such as features and labels, need to be randomly shuffled together, but you want to record this shuffled order, so what should you do? Here is a good method: Output: If you...

Two LISTs have the same elements and the number of the same elements in Python

Article catalog Reference URL Judging whether two LISTs have repeated elements Method 1, use traversal Method 2 Judgment different values ​​in two lists Elements in LIST1 in List2 Elements in LIST2 Re...

More Recommendation

Scramble multiple lists in the same order

Scramble (shuffle) multiple lists using the same sequence Can be used for network training to disrupt the training data tags without changing the correspondence Method one: np.random.shuffle (no retur...

The difference between using shuffle and permutation to randomly shuffle lists in python

Results of the:   Function: permutation returns a random list of the permutation range or returns a new array in a disordered order without changing the original array, If the input is a multi-di...

[Leetcode] [Python] Merge Two Sorted Lists / Two Order Linches

Topic Merge two row Solution ideas It has begun to be used as two LISTs to modify their values, but it should be modified by the pointer to the list. and there is also a notes that Dummy Node, see Sum...

Two ways to combine two order linked lists (Python)

Leetcode link Iteration time complexity o (n + m) Recursive time complexity o (n + m)...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top