np.random.choice() का इस्तेमाल
choice एक list या array से random items pick करता है, replacement के साथ या बिना।
In this page:
Syntax
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()
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. #}
आम गलतियां
- replace=False के साथ exist करने वाले से ज़्यादा unique items request करना
- Probabilities जो 1 तक sum न हों
- 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: