Python Tutorial: NumPy Unsolved Question Solution(Preeti Arora)

Friday, 24 December 2021

NumPy Unsolved Question Solution(Preeti Arora)

Q. What is NumPy? How to install it?

Answer:-
NumPy stands for ‘Numerical Python’. It is a package for data analysis and scientific computing with Python. NumPy uses a multidimensional array object, and has functions and tools for working with these arrays.

Installing NumPy, NumPy can be installed by typing following command:-
pip install NumPy

Q. Write some features of Numpy?

Answer:

1. High-performance N-dimensional array object

This is the most important feature of the NumPy library. It is the homogeneous array object. We perform all the operations on the array elements. The arrays in NumPy can be one dimensional or multidimensional.

a. One dimensional array

The one-dimensional array is an array consisting of a single row or column. The elements of the array are of homogeneous nature.

b. Multidimensional array

In this case, we have various rows and columns. We consider each column as a dimension. The structure is similar to an excel sheet. The elements are homogenous.

2. It contains tools for integrating code from C/C++ and Fortran

3. It contains a multidimensional container for generic data.

4. Additional linear algebra, Fourier transform, and random number capabilities

5. It consists of broadcasting functions

6. It had data type definition capability to work with varied databases

Q3. Write some attributes of NumPy ndarrays.

Solution:

Attribute

Definition

Example

shape

This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

 

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

print a.shape

The output is as follows −

(2, 3)

Example 2

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

a.shape = (3,2)

print a

The output is as follows −

[[1, 2]

 [3, 4]

 [5, 6]]

Example 3

NumPy also provides a reshape function to resize an array.

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

b = a.reshape(3,2)

print b

The output is as follows −

[[1, 2]

 [3, 4]

 [5, 6]]

ndim

This array attribute returns the number of array dimensions.

 

Example 1

import numpy as np 
a = np.arange(24) 
print (a)
print(a.ndim)

The output is as follows −

[0 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 17 18 19 20 21 22 23]
1
Example 2
import numpy as np 
a = np.arange(24) 
b=a.reshape(2,4,3)
print(b)
print(b.ndim)

The output is as follows −

[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]
 
 [[12 13 14]
  [15 16 17]
  [18 19 20]
  [21 22 23]]]
3

 

itemsize

This array attribute returns the length of each element of array in bytes.

Example 1

import numpy as np

x = np.array([1,2,3,4,5], dtype = np.int8)

print x.itemsize

The output is as follows −

1

Example 2

import numpy as np

x = np.array([1,2,3,4,5], dtype = np.float32)

print x.itemsize

The output is as follows −

4

 

 

Q4. What is NumPy? How to install it?

Answer:-

NumPy stands for ‘Numerical Python’. It is a package for data analysis and scientific computing with Python. NumPy uses a multidimensional array object, and has functions and tools for working with these arrays.

Installing NumPy, NumPy can be installed by typing following command:-
pip install NumPy

Q5. Differentiate between lists in Python and arrays.

Difference between Numpy array and list

Lists

Array

List can have elements of different data types for example, [1,3.4, ‘hello’, ‘a@’]

All elements of an array are of same data type for example, an array of floats may be: [1.2, 5.4, 2.7]

Elements of a list are not stored contiguously in memory.

Array elements are stored in contiguous memory locations. This makes operations on arrays faster than lists.

Lists do not support element wise operations, for example, addition, multiplication, etc. because elements may not be of same type.

Arrays support element wise operations. For example, if A1 is an array, it is possible to say A1/3 to divide each element of the array by 3.

Lists can contain objects of different datatype that Python must store the type information for every element along with its element value. Thus lists take more space in memory and are less efficient.

NumPy array takes up less space in memory as compared to a list because arrays do not require to store datatype of each element separately.

List is a part of core Python.

Array (ndarray) is a part of NumPy library.

Q6. Give the purpose of using arange().


Answer :-

We can create an array with numbers in a given range and sequence using the arange() function. This function is analogous to the range() function of Python.

Syntax:

numpy.arange([start, ]stop, [step, ]dtype=None)

>>> import numpy as np
>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(5.0)
array([ 0., 1., 2., 3., 4.])

Q. What is reshape() function?


Answer :-
We can modify the shape of an array using the reshape() function. Reshaping an array cannot be used to change the total number of elements in the array. 

Example 1

import numpy as np

a = np.array([[1,2,3],[4,5,6]])

b = a.reshape(3,2)

print(b)

The output is as follows −

[[1, 2]

 [3, 4]

 [5, 6]]

Example 2

Example 2
import numpy as np 
a = np.arange(24) 
b=a.reshape(2,4,3)
print(b)

The output is as follows −

[[[ 0  1  2]
  [ 3  4  5]
  [ 6  7  8]
  [ 9 10 11]]
 
 [[12 13 14]
  [15 16 17]
  [18 19 20]

  [21 22 23]]]

Q9. Differentiate between hstack() and vstack() functions.

Vertical Stack   Hstack()

Horisental Stack   Vstack()

Given two or more existing arrays, we can stack vertically using the vstack() function.

Example 1

import numpy as np

a=np.array([1,2,3])

print(a)

b=np.array([6,7,8])

print(b)

print("vstack")

c=np.vstack([a,b])

print(c)

Output:

[1 2 3]

[6 7 8]

vstack

[[1 2 3]

 [6 7 8]]

 

Given two or more existing arrays, we can stack horizontally  using the hstack() function.

Example 1

import numpy as np

a=np.array([1,2,3])

print(a)

b=np.array([6,7,8])

print(b)

print("hstack")

c=np.hstack([a,b])

print(c)

Output:

[1 2 3]

[6 7 8]

hstack

[1 2 3 6 7 8]

 

 

Q13. Import NumPy as 'np' and print the version number.

Answer :-

import numpy as np

print(np.version.version)

Q14. Consider a 2-D array having shape (3,5,4). What is the size of the array?

Answer :- 60 (3x5x4)

Q15. Identify the shape of the following ndarrays:

Identify the shape of the following ndarrays:

(i)                  

10

20

30

50

80

 

(ii)                

100

200

300

400

(iii)              

10

30

60

70

90

40

20

60

70

10

70

30

60

80

20

Answer : (i) (5,)

(ii) (5,1)

(iii) (3,5)


No comments:

Post a Comment