← Back to NumPy Course | Chapter 9: Random Module | Lesson 4 of 6

np.random.choice() का इस्तेमाल

choice एक list या array से random items pick करता है, replacement के साथ या बिना।

In this page:

  1. np.random.choice()
Syntax
python
np.random.choice(a, size, replace=False, p=probabilities)

np.random.choice()

np.random.choice(a, size, replace, p) a से sample करता है। Default से items repeat हो सकते हैं (replace=True); unique picks के लिए replace=False set करें। Optional p argument हर item की probability set करता है।

Note: अगर a एक integer n है, choice arange(n) से sample करता है।

उदाहरण: np.random.choice()

python
import numpy as np

np.random.seed(5)
fruits = np.array(["apple", "banana", "cherry", "date"])
print(np.random.choice(fruits, size=3, replace=False))
print(np.random.choice(fruits, size=5, p=[0.7, 0.1, 0.1, 0.1]))

# Output:
# ['apple' 'banana' 'cherry']
# ['cherry' 'apple' 'date' 'apple' 'apple']
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. replace=False के साथ exist करने वाले से ज़्यादा unique items request करना
  2. Probabilities जो 1 तक sum न हों
  3. default replacement भूल जाना
चैप्टर सारांश
  • choice एक array से sample करता है
  • replace=False unique picks देता है
  • p probabilities set करता है
  • एक integer मतलब arange(n)
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.