Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

ufuncs

Universal functions (ufuncs)

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Universal functions (ufuncs): NumPy ndarray operations, vectorization, and numerical patterns used across the Python scientific stack.

Teams apply Universal functions (ufuncs) in every serious NumPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Universal functions (ufuncs) in contexts like: Notebooks, feature engineering pipelines, and custom numerical code.

Read the narrative, run `import numpy as np` snippets in the playground (install NumPy with pip if the runner lacks it), tweak shapes and dtypes, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Ufuncs apply a function element-wise on arrays, implemented in compiled code. They are the engine behind NumPy vectorization.

Built-in ufuncs

  • Arithmetic: np.add, np.multiply, np.power
  • Math: np.sin, np.log, np.exp
  • Comparison: np.greater, np.equal
  • Reductions: np.add.reduce sums along an axis

Operators map to ufuncs

import numpy as np
a = np.array([1, 4, 9])
print(np.sqrt(a))
print(np.equal(a, 4))

Output dtype

Ufuncs respect dtype promotion rules. Integer division may need astype(float) first for fractional results.

Important interview questions and answers

  1. Q: Why ufuncs vs Python loop?
    A: Loop in Python; ufunc dispatches to SIMD/C loops on contiguous memory.
  2. Q: np.add.reduce?
    A: Equivalent to sum for 1D—generalizes to higher dimensions with axis.

Self-check

  1. Apply log to a positive float array.
  2. Which ufunc checks element equality?

Tip: Replace Python loops with ufuncs—profile if still slow.

Interview prep

What is ufunc?

Universal function—element-wise compiled operation on arrays.

Why not loop?

Python per-element overhead; ufuncs batch in C/SIMD.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ufunc example?
  • out= parameter?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump