In article we are going to explored 15 Numpy interview questions and answers.

#### 1.What is NumPy?

Python provides a package for scientific computing, the package name is NumPy.It is a Python package that provides support for:

- A multidimensional array object.
- Masked arrays and matrices.
- Functions for fast operations on arrays.
- Coverage for operations in mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms basic linear algebra, basic statistical operations, random simulation, and much more.

#### 2. What are the features of NumPy Library?

- NumPy arrays have a fixed size at the time of creation, if we want any change at runtime then ndarray creates a new array and deletes the old one.
- The elements in a NumPy array must be of the same data type and memory size. But if we have objects then it allows different size elements.
- ndarray is designed with Vectorization, which means the absence of explicit looping and indexing etc at the time of operations. This helps in faster code execution, easy code management, fewer bugs.
- In NumPy all operations behave with implicit element-by-element fashion, i.e., they broadcast. So operations on one element can broadcast to other elements of the array which makes the operations faster.
- NumPy fully supports OOPS, ndarray is a class. This class contains methods and attributes. This provides the flexibility to the NumPy library to support several operations on n-Dimensional arrays.

#### 3. How to Install Numpy library?

Below are the steps to Install NumPy library on a windows PC.

- Download & Install the Python executable binaries on your Windows system from Python.org.
- Run the Python executable installer to complete the installation.
- Install pip on your machine.
- Then use the below pip commands to install NumPy Library on your PC.

Open the command prompt and use the command “pip install numpy” - This will install the Numpy library on your machine.

#### 4. How to import numpy library in your code?

When writing your code you will need to import the NumPy library in your program as the first line of code. The syntax to import any library is very simple and it can be done by using the import keyword as given below.

**import numpy as np**

Now since you have imported the Numpy Library as np then you can use np as Numpy object to access NumPy features.

#### 5. What is NumPy array? ndarray?

- The core component of the NumPy library is the ndarray. ndarray object provides the implementation of n-dimensional arrays of homogeneous data types. ndarray is designed and implemented to accomplish high performance.
- ndarray has several important differences when it’s compared with the standard Python sequences like lists and dictionaries etc.
- NumPy arrays are optimized for advanced mathematical and scientific operations with large data sets. These optimizations in ndarray make it superior to Python’s built-in sequences.

#### 6. What are the advantages of Numpy Arrays over Python arrays and lists?

- NumPy arrays are an alternative for lists and arrays in Python

Arrays in Numpy are equivalent to lists in python.NumPy arrays can perform some functions that could not be performed when

applied to python arrays due to their heterogeneous nature. - NumPy arrays maintain minimal memory usage as they are treated as objects. Python deletes and creates these objects continually, as per the requirements. Hence, the memory allocation is less as compared to Python lists. NumPy has features to avoid memory wastage in the data buffer.
- NumPy array has support for multi-dimensional arrays. We can also create multi-dimensional arrays in NumPy.These arrays have multiple rows and columns.
- NumPy includes easy-to-use functions for mathematical computations on the array data set. Numpy has many functions and features that make it ideal for Maths operations. There are functions for Linear Algebra, bitwise operations, Fourier transform, arithmetic operations, string operations, etc.

#### 7. Can Numpy be used with other Libraries?

Yes, The answer is Yes. Numpy can be used with many other Python libraries which makes it flexible and reuseable.

- NumPy can be used with Pandas library. Pandas is one of the most important libraries in python for data analysis. When we use both the libraries together it is a very helpful resource for scientific computations.
- NumPy can be used with the Matplotlib library to plot different graphs.
- NumPy can be used with SciPy also. Scipy is an open-source library in Python. It is the most important scientific library in python.
- NumPy can be used with the Tkinter library also. Tkinter is a standard library for GUI. We use Tkinter for the GUI representation of the NumPy data. We can easily convert the array objects into image objects.

#### 8.How do we create 1D Array in NumPy ?

Numpy provides array function that we can use to create a 1-D array in NumPy.Here is an example

```
import numpy as np
arr=[1,2,3]
nparray = np.array(arr)
print( "1-D Array is : ",nparray)
```

#### 9.How do we create 2D Array in NumPy?

Numpy provides array function that we can use to create a 1-D, 2-D or N-D array in NumPy.Here is an example:

```
import numpy as np
arr=[[1,2,3],[4,5,6]]
nparray = np.array(arr)
print("\n 2-D Array is : ",nparray)
```

#### 10.How do we create 3-D Array or N-D Array ?

Numpy provides array function that we can use to create a 1-D, 2-D or N-D array in NumPy. Here is an example:

```
import numpy as np
arr=[[[1,2,3],[4,5,6],[7,8,9]]]
nparray = np.array(arr)
print("\n 3-D Array is :",nparray)
```

#### 11.How to find the data type of Numpy array values?

If we want to find the type of the array elements we can use samplearray.dtype which will return us the type of the array elements.

```
import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears")])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])
print("Data Type of of 2-D Array elements",samplearray.dtype)
print("Data Type of of 1-D Array elements",samplearray1D.dtype)
```

Output

```
Data Type of of 2-D Array elements <U9
Data Type of of 1-D Array elements int32
```

#### 12.How can we find the Size of a 1-D NumPy array?

To find the size of the array we can use samplearray.size which will return us the size of the array by providing the number of array elemnets:

```
import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears")])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])
print("Size of 2-D Array",samplearray.size)
print("Size of 1-D Array",samplearray1D.size)
```

Output

```
Size of 2-D Array 4
Size of 1-D Array 10
```

#### 13.How can we find the Size of a 2-D NumPy array? How to find each axis size?

- When trying to determine the size of the array we can specify the axis if we are interested in a particular axis for a 2-D or N-D array. For 1-D array, the axis is always 0 as it is a linear array.
- To use the size() function of NumPy we pass 0 if we want the number of rows and we pass 1 if we are interested in a number of columns.
- If we do not pass any argument for axis then size() functions return the total number of elements in the array.

Let us understand this with the example below:

```
import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears"),
("Orange","Grapes", "Kiwi","Pineapple" )])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])
print("Size of 2-D Array",np.size(samplearray))
print("Number of Rows (X-Axis)",np.size(samplearray,0))
print("Number of Column (Y-Axis)",np.size(samplearray,1))
print("Size of 1-D Array",np.size(samplearray1D))
print("Size of 1-D Array(X-Axis)",np.size(samplearray1D,0))
```

Output

```
Size of 2-D Array 8
Number of Rows (X-Axis) 2
Number of Column (Y-Axis) 4
Size of 1-D Array 10
Size of 1-D Array(X-Axis) 10
```

#### 14. How can we find the shape of a NumPy array?

To find the shape of the array we can use samplearray.shape which will return us the shape of the array in terms of number or rows and number of columns this array holds.

```
import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears"),
("Orange","Grapes", "Kiwi","Pineapple" )])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])
print("Shape of 2-D Array",samplearray.shape)
print("Shape of 1-D Array",samplearray1D.shape)
```

Output

```
Shape of 2-D Array (2, 4)
Shape of 1-D Array (10,)
```

#### 15. How to find the Dimensions of NumPy array?

We can find the dimensions of the array by using ndim attribute. This will tell us if the array is 1-D,2-D..or N-Dimensional. To use this use below syntax:

```
import numpy as np
samplearray = np.array([("Apple","Banana", "Cheery","Pears"),
("Orange","Grapes", "Kiwi","Pineapple" )])
samplearray1D = np.array([1,2,3,4,5,6,7,8,9,10])
print("samplearray Array is of Dimensions: ",samplearray.ndim)
print("samplearray1D Array is of Dimensions: ",samplearray1D.ndim)
```

Output

```
samplearray Array is of Dimensions: 2
samplearray1D Array is of Dimensions: 1
```