Data Science & ML

NumPy Fundamentals

Arrays, ndarray, indexing, slicing, broadcasting, vectorized operations, linear algebra

22 interview questionsยท
Junior
1

What is an ndarray in NumPy?

Answer

An ndarray (N-dimensional array) is NumPy's fundamental data structure. It is a homogeneous multidimensional array, meaning all elements must be of the same type. This homogeneity enables very fast vectorized operations because data is stored contiguously in memory, unlike Python lists which store references to scattered objects.

2

How to create a NumPy array containing the values [1, 2, 3, 4, 5]?

Answer

The np.array() function is the standard method for creating an ndarray from a Python sequence like a list or tuple. It converts the sequence into an optimized NumPy array. Other functions like np.arange() generate sequences but with different syntax (start, stop, step), and np.zeros()/np.ones() create arrays filled with specific values.

3

Which function to use to create an array of 10 evenly spaced elements between 0 and 1?

Answer

np.linspace(0, 1, 10) creates exactly 10 evenly spaced values between 0 and 1, inclusive of both endpoints. It is ideal when the desired number of points is known. np.arange() uses a fixed step and may not include the endpoint. np.linspace() is preferred for intervals with a precise number of points, particularly for plotting or numerical integration calculations.

4

Which attribute provides the dimensions (shape) of a NumPy array?

5

How to create a 3x3 matrix filled with zeros?

+19 interview questions

Master Data Science & ML for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free