np.vectorize() का इस्तेमाल
np.vectorize एक ordinary Python function को wrap करता है ताकि इसे पूरे arrays पर call किया जा सके।
In this page:
Syntax
vectorized = np.vectorize(function_name)
result = vectorized(arr)
np.vectorize()
np.vectorize broadcasting के साथ हर element पर एक scalar function apply करता है। यह एक convenience है, speed-up नहीं, क्योंकि यह अभी भी Python में under the hood loop करता है। Real speed के लिए, ufuncs या np.where इस्तेमाल करें।
Note:
output dtype control करने के लिए otypes specify करें।
उदाहरण: np.vectorize()
import numpy as np
def grade(score):
return "A" if score >= 90 else "B" if score >= 80 else "C"
vgrade = np.vectorize(grade)
print(vgrade(np.array([95, 85, 70])))
# Output:
# ['A' 'B' 'C']
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. #}
आम गलतियां
- यह उम्मीद करना कि np.vectorize fast होगा
- empty inputs के साथ otypes skip करना
- जहाँ एक ufunc exist करता है वहाँ इसे इस्तेमाल करना
चैप्टर सारांश
- vectorize scalar functions में array support add करता है
- यह convenient है, fast नहीं
- ufuncs या where prefer करें
- otypes output type set करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: