search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

NumPy Random Generator | permuted method

schedule Aug 10, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

NumPy Random's permuted(~) method returns a new NumPy array with the values shuffled.

NOTE

To shuffle values in-place, use shuffle(~).

Also, the difference between permutation(~) and permuted(~) is that the former shuffles rows or columns for two-dimensional arrays, but permuted(~) shuffles values independent of the other rows or columns. Consult examples below for clarification.

Parameters

1. x | NumPy array or MutableSequence

The array to shuffle.

2. axislink | int | optional

The axis to shuffle. By default, axis=None, which means that all the values in the array are shuffled.

3. out | NumPy array | optional

If given, then the result is stored in out. By default, a new NumPy array is created and returned.

Return Value

A NumPy array.

Examples

Shuffling a one-dimensional array

To shuffle a one-dimensional array:

import numpy as np
rng = np.random.default_rng(seed=42)
rng.permuted([5,2,6,1])
array([1, 6, 2, 5])

Note that when shuffling one-dimensional arrays, the behaviour is exactly the same as permutation(~).

Shuffling two-dimensional array

Consider the following two-dimensional array:

x = np.arange(12).reshape((3,4))
x
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])

Shuffling all values

By default, axis=None, which means that all the values in the array are shuffled:

rng = np.random.default_rng(seed=42)
rng.permuted(x) # axis=None
array([[ 0, 7, 6, 9],
[11, 3, 5, 2],
[ 4, 10, 1, 8]])

Shuffling values of each column

To shuffle the values in each column, set axis=0:

rng = np.random.default_rng(seed=42)
rng.permuted(x, axis=0)
array([[ 8, 1, 6, 11],
[ 4, 9, 10, 3],
[ 0, 5, 2, 7]])

Note that each row is shuffled independently on how the values in the other columns are sorted. This is the main difference between permuted(~) and permutation(~).

Shuffling values of each row

To shuffle the values in each row, set axis=1:

rng = np.random.default_rng(seed=42)
rng.permuted(x, axis=1)
array([[ 3, 2, 1, 0],
[ 7, 6, 4, 5],
[ 8, 11, 10, 9]])
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!