← Back to NumPy Course | Chapter 12: Advanced Topics | Lesson 4 of 7

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

np.vectorize एक ordinary Python function को wrap करता है ताकि इसे पूरे arrays पर call किया जा सके।

In this page:

  1. np.vectorize()
Syntax
python
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()

python
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. #}
आम गलतियां
  1. यह उम्मीद करना कि np.vectorize fast होगा
  2. empty inputs के साथ otypes skip करना
  3. जहाँ एक 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:

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.