Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C28-L1263-2DB5F4D4

Home Dashboard
Chapter 25 · Module 1 — NumPy Fundamentals

Shuffling and Permutations

Priority
  • JobHigh
  • InterviewMedium
  • DevelopmentHigh
4,042
words
23
min read
2
tools

1. Learning Outcomes#

या chapter नंतर तुम्हाला:

  • random shuffling म्हणजे नेमके काय ते समजेल
  • rng.shuffle() array ला in place कसे shuffle करते ते समजेल
  • 1D array मधील values random order मध्ये कशा लावायच्या ते समजेल
  • 2D array मध्ये पूर्ण rows किंवा columns कशा shuffle होतात ते समजेल
  • axis=0 आणि axis=1 यांचा अर्थ स्पष्ट होईल
  • rng.permutation() वापरून नवीन permuted array कसा तयार करायचा ते समजेल
  • integer दिल्यावर permutation() काय तयार करते ते समजेल
  • shuffle() आणि permutation() मधील मुख्य फरक स्पष्ट होईल
  • original array preserve करायचा असेल तर योग्य method निवडता येईल
  • seed वापरून reproducible random order कसा मिळवायचा ते समजेल
  • shuffled result validate करताना कोणते checks करायचे ते समजेल

2. Random Shuffling समजून घेऊया#

समजा आपल्याकडे विद्यार्थ्यांची नावे एका ठराविक क्रमाने आहेत.

A, B, C, D, E

आता training experiment मध्ये प्रत्येक वेळी विद्यार्थ्यांचा क्रम random हवा आहे.

आपल्याला values बदलायच्या नाहीत.

फक्त त्यांचा order बदलायचा आहे.

यालाच साध्या भाषेत shuffling म्हणता येईल.

NumPy मध्ये random shuffling करताना एक महत्त्वाची गोष्ट लक्षात ठेवा:

Key Idea

Shuffling म्हणजे existing values मधून काही values delete किंवा add करणे नाही. त्याच values चा क्रम random पद्धतीने बदलणे होय.

Data Science मध्ये हे अनेक ठिकाणी उपयोगी पडते.

उदाहरणार्थ:

  • observations चा क्रम random करणे
  • experiments मध्ये ordering bias कमी करणे
  • training data चा order बदलणे
  • simulations साठी random ordering तयार करणे
  • records चा sequence random करणे

3. Random Generator चा छोटा recap#

या chapter मध्ये आपण NumPy चा modern random generator वापरणार आहोत.

PYTHON
import numpy as np

rng = np.random.default_rng()

इथे rng हा random numbers आणि random operations करण्यासाठी वापरला जाणारा generator आहे.

या generator कडे shuffle() आणि permutation() सारख्या methods आहेत.

Seed द्यायचा असेल तर:

PYTHON
rng = np.random.default_rng(42)

Seed चा उपयोग आपण पुढे reproducibility मध्ये पाहणार आहोत.

4. rng.shuffle() — Same Array, New Order#

rng.shuffle() existing sequence चा order random पद्धतीने बदलते.

सर्वात महत्त्वाचा मुद्दा असा आहे:

हे operation in place होते.

in place म्हणजे नवीन shuffled array वेगळा return करण्याऐवजी आपण दिलेल्या original array मध्येच बदल केला जातो.

हा flow visually असा आहे:

Visual Diagram80%

Rendering diagram…

इथे flow कुठून सुरू झाला ते पहा.

आपण existing array rng.shuffle() ला दिला.

Method ने त्याच array चा order बदलला.

त्यामुळे separate shuffled result मिळत नाही.

shuffle() चे return value None असते.

Remember

rng.shuffle() वापरल्यानंतर shuffled data original variable मध्येच असतो.

5. 1D Array Shuffle करणे#

सर्वात आधी साधा 1D array घेऊया.

PYTHON
import numpy as np

rng = np.random.default_rng(21)

numbers = np.array([10, 20, 30, 40, 50])

rng.shuffle(numbers)

print(numbers)

इथे numbers मधील values त्या पाचच राहतात.

पण त्यांचा क्रम random होऊ शकतो.

उदाहरणार्थ एका run मध्ये order असा असू शकतो:

[40, 10, 50, 20, 30]

दुसऱ्या generator state किंवा seed मध्ये दुसरा valid order मिळू शकतो.

महत्त्वाचे

Random shuffle ने प्रत्येक वेळी original order बदललाच पाहिजे अशी mathematical guarantee नाही. Random process मुळे कधीतरी original order पुन्हा मिळणेही शक्य आहे.

shuffle() काय return करते#

Beginner ची एक common expectation अशी असते:

PYTHON
shuffled = rng.shuffle(numbers)

आणि नंतर shuffled मध्ये नवीन array असेल असे वाटते.

पण तसे होत नाही.

हे स्पष्ट करण्यासाठी:

PYTHON
import numpy as np

rng = np.random.default_rng(21)

numbers = np.array([10, 20, 30, 40, 50])

result = rng.shuffle(numbers)

print(result)

Expected Output#

TEXT
None

कारण actual shuffled values numbers मध्येच आहेत.

Common Mistake

shuffled = rng.shuffle(arr) लिहून shuffled ला array समजू नका. shuffled ची value None असेल.

6. In-Place Operation चा Mental Model#

समजा आपल्या टेबलवर पाच cards आहेत.

त्यावर:

10, 20, 30, 40, 50

लिहिले आहे.

shuffle() नवीन cards चा दुसरा set तयार करत नाही.

तेच cards उचलून random क्रमाने पुन्हा ठेवते.

म्हणून:

same data container → changed order

हा shuffle() समजण्यासाठी सर्वात महत्त्वाचा mental model आहे.

"जशी करणी तशी भरणी" या म्हणीप्रमाणे, original array थेट shuffle() ला दिला तर बदलही त्याच original array मध्ये दिसणार.

7. Original Array Preserve करायचा असेल तर#

कधी कधी आपल्याला original data सुद्धा ठेवायचा असतो.

अशावेळी original array ची copy() बनवून त्या copy ला shuffle करू शकतो.

PYTHON
import numpy as np

rng = np.random.default_rng(42)

original = np.array([10, 20, 30, 40, 50])

shuffled = original.copy()

rng.shuffle(shuffled)

print(original)
print(shuffled)

इथे:

  • original सुरक्षित आहे
  • shuffled ही वेगळी array आहे
  • rng.shuffle(shuffled) फक्त copy modify करते

हा approach योग्य आहे.

पण NumPy मध्ये यासाठी आणखी direct method आहे:

rng.permutation()

ती आपण थोड्याच वेळात पाहू.

8. 2D Array मध्ये Shuffling#

1D array मध्ये individual values हा shuffle होणारा unit होता.

पण 2D array मध्ये आपल्याला एक नवीन प्रश्न समजून घ्यावा लागतो:

कोणत्या axis वर order बदलायचा?

2D array पाहूया:

PYTHON
matrix = np.array([
    [1, 10],
    [2, 20],
    [3, 30]
])

यामध्ये तीन rows आहेत:

  • [1, 10]
  • [2, 20]
  • [3, 30]

axis=0 वर shuffle केल्यास पूर्ण rows चा क्रम बदलतो.

खालील visual मध्ये original matrix आणि एक possible shuffled result पाहा.

Figure 1: axis=0 वर पूर्ण rows एक unit म्हणून shuffle होतात
axis=0 वर पूर्ण rows एक unit म्हणून shuffle होतातBefore shuffleBefore shuffleFeature A123Feature B102030axis=0axis=0One possible resultOne possible resultFeature A312Feature B301020

इथे learner ने एक महत्त्वाची गोष्ट notice करायची आहे.

[1, 10] row तुटली नाही.

1 एका row मध्ये आणि 10 दुसऱ्या row मध्ये गेले नाही.

पूर्ण row एक unit म्हणून दुसऱ्या position वर गेली.

Data Science मध्ये हे खूप महत्त्वाचे आहे.

एका row मध्ये एकाच observation चे अनेक features असतील तर ते features एकत्र राहणे आवश्यक असते.

9. axis=0 — Rows Shuffle करणे#

Code:

PYTHON
import numpy as np

rng = np.random.default_rng(10)

matrix = np.array([
    [1, 10],
    [2, 20],
    [3, 30]
])

rng.shuffle(matrix, axis=0)

print(matrix)

axis=0 म्हणजे rows च्या direction मध्ये order बदलणे.

Shape मात्र बदलत नाही.

PYTHON
import numpy as np

rng = np.random.default_rng(10)

matrix = np.array([
    [1, 10],
    [2, 20],
    [3, 30]
])

before_shape = matrix.shape

rng.shuffle(matrix, axis=0)

print(before_shape == matrix.shape)

Expected Output#

TEXT
True

Shuffling फक्त order बदलते.

Dimensions किंवा shape बदलत नाही.

10. axis=1 — Columns Shuffle करणे#

आता त्याच matrix वर axis=1 वापरूया.

axis=1 म्हणजे columns चा order बदलणे.

खालील visual मध्ये दोन columns एकत्र कसे reorder होतात ते पहा.

Figure 2: axis=1 वर columns पूर्ण column units म्हणून reorder होतात
axis=1 वर columns पूर्ण column units म्हणून reorder होतातBefore shuffleBefore shuffleColumn A123Column B102030axis=1axis=1One possible resultOne possible resultFormer B102030Former A123

इथे पहिल्या column मधील values independently shuffle केलेल्या नाहीत.

Columns चा order बदलला आहे.

म्हणजे संपूर्ण column position बदलते.

PYTHON
rng.shuffle(matrix, axis=1)
Common Mistake

axis=1 म्हणजे प्रत्येक row मधील values independently वेगवेगळ्या random order मध्ये shuffle होतील असे समजू नका. Axis वरील sub-arrays एकाच permutation नुसार reorder होतात.

जर प्रत्येक row ला independently shuffle करायचे असेल तर प्रत्येक row वर स्वतंत्र random operation करावी लागते.

11. axis चा सोपा Summary#

Array axis काय reorder होते?
1D 0 individual elements
2D 0 rows
2D 1 columns

axis=0 हा default आहे.

म्हणून:

PYTHON
rng.shuffle(matrix)

आणि:

PYTHON
rng.shuffle(matrix, axis=0)

2D array साठी समान axis निवडतात.

12. rng.permutation() — New Permuted Result#

आता rng.permutation() समजून घेऊया.

shuffle() चा मुख्य स्वभाव होता:

original array modify करणे

permutation() चा मुख्य स्वभाव आहे:

randomly reordered result तयार करून return करणे

Existing array दिल्यास original array preserve राहतो.

हा flow पाहा:

Visual Diagram80%

Rendering diagram…

इथे original array वर थेट mutation होत नाही.

आपल्याला नवीन result मिळतो.

13. Existing Array चे Permutation#

PYTHON
import numpy as np

rng = np.random.default_rng(42)

original = np.array([11, 22, 33, 44])

permuted = rng.permutation(original)

print(np.array_equal(original, np.array([11, 22, 33, 44])))
print(np.array_equal(np.sort(permuted), np.sort(original)))

Expected Output#

TEXT
True
True

पहिले True सांगते:

original array बदललेला नाही.

दुसरे True सांगते:

permuted result मध्ये त्याच values आहेत.

फक्त order बदलू शकतो.

यामुळे permutation() original data preserve करायचा असेल तेव्हा खूप convenient ठरते.

14. Integer Sequence चे Permutation#

rng.permutation() ला array देणे compulsory नाही.

Integer n दिल्यास NumPy:

0 ते n - 1

ही integer sequence तयार करून तिचा random permutation return करते.

उदाहरण:

PYTHON
import numpy as np

rng = np.random.default_rng(42)

order = rng.permutation(6)

print(order)

Possible result मध्ये values नेहमी:

0, 1, 2, 3, 4, 5

याच असतील.

फक्त order random असेल.

हे validate करूया:

PYTHON
import numpy as np

rng = np.random.default_rng(42)

order = rng.permutation(6)

print(np.array_equal(np.sort(order), np.arange(6)))

Expected Output#

TEXT
True

हा pattern random indices तयार करताना उपयोगी पडतो.

15. permutation() आणि axis#

rng.permutation() सुद्धा multidimensional array वर axis घेऊ शकते.

उदाहरण:

PYTHON
permuted_rows = rng.permutation(matrix, axis=0)

यामध्ये:

  • rows randomly reorder होतात
  • नवीन array मिळते
  • original matrix preserve राहते

Columns चा permutation हवा असल्यास:

PYTHON
permuted_columns = rng.permutation(matrix, axis=1)

त्यामुळे axis चा mental model दोन्ही methods साठी उपयोगी पडतो.

16. shuffle() vs permutation()#

दोन्ही methods random ordering साठी आहेत.

पण त्यांचा data handling वेगळा आहे.

Feature rng.shuffle() rng.permutation()
Random order तयार करते Yes Yes
Original array modify करते Yes No
In-place operation Yes No
New permuted result return करते No Yes
Return value None New permuted array
Original preserve करण्यासाठी convenient No Yes
Integer n वर permutation तयार करू शकते No Yes

यातून practical rule तयार होतो:

Key Idea

Original data बदलणे acceptable असेल तर shuffle() वापरा. Original सुरक्षित ठेवायचा असेल तर permutation() हा अधिक direct पर्याय आहे.

17. Original Preserve करण्याचे दोन मार्ग#

Original array preserve करण्यासाठी दोन valid patterns आहेत.

Pattern 1 — copy() + shuffle()#

PYTHON
shuffled = original.copy()
rng.shuffle(shuffled)

यामध्ये आपण explicit copy तयार करतो.

Pattern 2 — permutation()#

PYTHON
shuffled = rng.permutation(original)

हा अधिक concise आहे.

दोन्ही approaches चे intention स्पष्ट असले पाहिजे.

जर requirement फक्त random reordered copy तयार करणे असेल तर permutation() साधारणपणे अधिक natural दिसते.

18. Seed आणि Reproducible Shuffling#

Random operation शिकताना seed हा अत्यंत महत्त्वाचा concept आहे.

Random data science workflow मध्ये अनेकदा आपल्याला आज मिळालेला random order पुन्हा reproduce करायचा असतो.

उदाहरणार्थ:

  • debugging
  • experiment repeat करणे
  • team member ला समान random setup देणे
  • test repeat करणे

त्यासाठी fixed seed वापरता येतो.

PYTHON
rng = np.random.default_rng(1234)

एक महत्त्वाची अट आहे.

Same seed पुरेसा नाही.

Reproducibility साठी सामान्यतः खालील गोष्टी समान असायला हव्यात:

  • same seed
  • same starting data
  • same random operations
  • same operation order

दोन fresh generators ला समान seed दिल्यास समान starting state मिळते.

Visual Diagram80%

Rendering diagram…

Code पाहूया:

PYTHON
import numpy as np

a = np.arange(8)
b = np.arange(8)

rng1 = np.random.default_rng(1234)
rng2 = np.random.default_rng(1234)

rng1.shuffle(a)
rng2.shuffle(b)

print(np.array_equal(a, b))

Expected Output#

TEXT
True

दोन generators समान seed पासून सुरू झाले.

दोघांना समान data मिळाला.

दोघांवर समान random operation झाले.

म्हणून result reproduce झाला.

19. Same Generator वर Repeated Calls#

हा beginner trap आहे.

PYTHON
rng = np.random.default_rng(1234)

हा generator तयार केल्यानंतर प्रत्येक random operation त्याची internal state पुढे नेते.

म्हणून:

PYTHON
rng.shuffle(a)
rng.shuffle(b)

या दोन calls साठी "seed तोच आहे" म्हणून identical result अपेक्षित ठेवणे योग्य नाही.

Generator ची state पहिल्या call नंतर बदललेली असते.

Remember

Fixed seed म्हणजे प्रत्येक random call ला same result मिळेल असा अर्थ नाही. Fixed seed म्हणजे random operation ची sequence reproducible बनते.

20. Shuffled Result Validate करणे#

Random order पाहून manually "हे योग्य दिसते" असे म्हणणे reliable validation नाही.

Random operation validate करताना properties तपासणे अधिक योग्य असते.

1. Values हरवल्या किंवा वाढल्या नाहीत#

1D array साठी sorted values compare करता येतात.

PYTHON
import numpy as np

rng = np.random.default_rng(7)

original = np.array([40, 10, 30, 20, 20])

shuffled = rng.permutation(original)

print(np.array_equal(np.sort(original), np.sort(shuffled)))

Expected Output#

TEXT
True

Duplicate value 20 सुद्धा preserve झाली आहे.

2. Shape बदललेली नाही#

PYTHON
original_shape = matrix.shape

rng.shuffle(matrix, axis=0)

print(matrix.shape == original_shape)

Expected Output#

TEXT
True

3. Original preserve करायचा असेल तर तो unchanged आहे#

PYTHON
import numpy as np

rng = np.random.default_rng(8)

original = np.array([1, 2, 3, 4])
backup = original.copy()

permuted = rng.permutation(original)

print(np.array_equal(original, backup))

Expected Output#

TEXT
True

4. 2D Row Shuffle मध्ये पूर्ण rows preserve झाल्या आहेत#

Rows मध्ये duplicate records असू शकतात.

त्यामुळे rows चा sorted list compare करणे useful आहे.

PYTHON
import numpy as np

rng = np.random.default_rng(5)

matrix = np.array([
    [101, 70],
    [102, 85],
    [103, 60],
    [102, 85]
])

original = matrix.copy()

rng.shuffle(matrix, axis=0)

same_rows = sorted(map(tuple, original)) == sorted(map(tuple, matrix))

print(same_rows)

Expected Output#

TEXT
True

इथे प्रत्येक row tuple मध्ये convert करून complete rows compare केल्या.

त्यामुळे column values चुकीच्या पद्धतीने mix झाल्या आहेत का हेही लक्षात येते.

21. Practical Data Example#

समजा dataset मध्ये प्रत्येक row एका student चे record आहे.

Student ID Score
101 78
102 91
103 65
104 84

जर आपण rows shuffle केल्या तर valid result असा असू शकतो:

Student ID Score
103 65
101 78
104 84
102 91

इथे Student ID आणि त्याचा Score pair तुटलेला नाही.

हेच row-wise shuffling मध्ये सर्वात महत्त्वाचे आहे.

जर columns independently random केल्या तर record चे meaning खराब होऊ शकते.

म्हणून axis समजून operation करणे आवश्यक आहे.

22. Common Mistakes#

Mistake 1 — shuffle() चा return value वापरणे#

चुकीची expectation:

PYTHON
result = rng.shuffle(arr)

आणि result shuffled array आहे असे समजणे.

योग्य mental model:

PYTHON
rng.shuffle(arr)

आता arr स्वतः shuffled आहे.

Mistake 2 — Original data अनवधानाने बदलणे#

PYTHON
rng.shuffle(original)

हे original modify करते.

Original सुरक्षित हवा असल्यास:

PYTHON
shuffled = rng.permutation(original)

किंवा:

PYTHON
shuffled = original.copy()
rng.shuffle(shuffled)

Mistake 3 — 2D array मध्ये axis दुर्लक्षित करणे#

Default axis=0 rows reorder करते.

Columns reorder करायच्या असतील तर explicitly:

PYTHON
rng.shuffle(matrix, axis=1)

वापरा.

Mistake 4 — Random order न बदलल्यास operation failed समजणे#

Random permutation मध्ये original order सुद्धा mathematically possible permutation आहे.

म्हणून random order visually वेगळा दिसलाच पाहिजे अशी test condition ठेवू नका.

त्याऐवजी:

  • elements preserved आहेत का
  • shape preserved आहे का
  • original preserve करण्याची requirement पूर्ण झाली का

हे तपासा.

Mistake 5 — Fixed seed म्हणजे प्रत्येक call same समजणे#

Generator state प्रत्येक random call नंतर पुढे जाते.

Reproducibility म्हणजे पूर्ण sequence repeat करणे, प्रत्येक call independent identical असणे नाही.

23. Method निवडण्यासाठी छोटा Decision Flow#

Visual Diagram80%

Rendering diagram…

हा flow practical coding मध्ये पटकन method निवडायला मदत करतो.

24. Hands-On Practice#

Practice 1#

Requirement

[10, 20, 30, 40, 50, 60] ही array घ्या. Original array बदलता कामा नये आणि random reordered version तयार करा.

Your Task

rng.permutation() वापरा आणि original array unchanged आहे तसेच result मध्ये सर्व values आहेत हे verify करा.

HintShow Hint

Original array ची backup copy घेऊन np.array_equal() वापरू शकता.

SolutionShow Solution
PYTHON
import numpy as np

rng = np.random.default_rng(100)

original = np.array([10, 20, 30, 40, 50, 60])
backup = original.copy()

permuted = rng.permutation(original)

print(np.array_equal(original, backup))
print(np.array_equal(np.sort(original), np.sort(permuted)))

Expected Output#

TEXT
True
True

Explanation

पहिले True original array untouched असल्याचे सांगते.

दुसरे True permuted result मध्ये सर्व original values preserve झाल्याचे सांगते.


Practice 2#

Requirement

Student records असलेली 2D array पूर्ण rows म्हणून random order मध्ये shuffle करा.

Your Task

axis=0 वापरा आणि shuffle नंतरही complete rows चा collection समान आहे हे verify करा.

HintShow Hint

प्रत्येक row ला tuple मध्ये convert करून sorted rows compare करू शकता.

SolutionShow Solution
PYTHON
import numpy as np

rng = np.random.default_rng(50)

records = np.array([
    [101, 72],
    [102, 88],
    [103, 64],
    [104, 91]
])

before = records.copy()

rng.shuffle(records, axis=0)

valid = sorted(map(tuple, before)) == sorted(map(tuple, records))

print(valid)

Expected Output#

TEXT
True

Explanation

पूर्ण records preserve झाले आहेत.

फक्त rows ची positions random झाल्या आहेत.

25. Interview Preparation#

What is the main difference between Generator.shuffle() and Generator.permutation()?Show Answer

Generator.shuffle() modifies the supplied sequence in place and returns None, while Generator.permutation() returns a randomly permuted result and preserves the original array when an array is supplied.

Key Points:

  • shuffle() performs in-place mutation.
  • permutation() returns a new permuted result.
  • Method choice depends on whether the original data must be preserved.

Why interviewer asks this:

It checks whether you understand mutation, return behavior, and how to avoid unintentionally modifying data.


What does axis=0 mean when shuffling a two-dimensional NumPy array?Show Answer

With a two-dimensional array, axis=0 randomizes the order of rows while preserving the values within each row as one sub-array.

Key Points:

  • Complete rows are reordered.
  • Row contents remain associated.
  • The array shape remains unchanged.

Why interviewer asks this:

It checks whether you understand multidimensional random operations and the practical importance of preserving row-level records.


How can you make a NumPy shuffle reproducible?Show Answer

Create the random generator with a fixed seed and repeat the same random operations on the same starting data in the same order.

Key Points:

  • Use np.random.default_rng(seed).
  • The same seed initializes the same random state.
  • Generator state advances after each random operation.
  • Reproducibility applies to the random operation sequence.

Why interviewer asks this:

Reproducibility is important in testing, debugging, data science experiments, and collaborative analytical workflows.

26. Quick Recap#

या chapter मधून मुख्यतः हे लक्षात ठेवा:

  • shuffling मध्ये values बदलत नाहीत; त्यांचा order बदलतो
  • rng.shuffle() original array ला in place modify करते
  • rng.shuffle() चे return value None असते
  • 1D array मध्ये elements reorder होतात
  • 2D array मध्ये axis=0 rows reorder करते
  • axis=1 columns reorder करते
  • rng.permutation(array) नवीन permuted result देते
  • rng.permutation(n) हे 0 ते n-1 values चे permutation तयार करते
  • original preserve करायचा असेल तर permutation() किंवा copy() + shuffle() वापरता येते
  • same seed, same starting data आणि same random-call sequence reproducible result देतात
  • random result validate करताना exact order पेक्षा data preservation, shape आणि mutation behavior तपासणे अधिक reliable आहे
Summary

shuffle() म्हणजे "हेच data container random order मध्ये बदल". permutation() म्हणजे "मला random order मधील नवीन result दे". हा फरक लक्षात राहिला तर दोन्ही methods योग्य ठिकाणी वापरणे खूप सोपे होते.

Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.