0% found this document useful (0 votes)
7 views7 pages

NumPy Unsolved Question Solution

NumPy, short for 'Numerical Python', is a package for data analysis and scientific computing that utilizes multidimensional arrays. It can be installed using the command 'pip install NumPy' and offers features such as high-performance N-dimensional arrays, integration with C/C++ and Fortran, and additional capabilities for linear algebra and random number generation. Key attributes of NumPy ndarrays include shape, ndim, and itemsize, which provide information about the array's dimensions and data types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

NumPy Unsolved Question Solution

NumPy, short for 'Numerical Python', is a package for data analysis and scientific computing that utilizes multidimensional arrays. It can be installed using the command 'pip install NumPy' and offers features such as high-performance N-dimensional arrays, integration with C/C++ and Fortran, and additional capabilities for linear algebra and random number generation. Key attributes of NumPy ndarrays include shape, ndim, and itemsize, which provide information about the array's dimensions and data types.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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:

Attribut Definition Example


e
shape This array import numpy as np
attribute returns
a tuple a = [Link]([[1,2,3],[4,5,6]])
consisting of
array
dimensions. It print [Link]
can also be The output is as follows −
used to resize
the array. (2, 3)

Example 2

import numpy as np

a = [Link]([[1,2,3],[4,5,6]])

[Link] = (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 = [Link]([[1,2,3],[4,5,6]])

b = [Link](3,2)

print b
The output is as follows −

[[1, 2]

[3, 4]

[5, 6]]
ndim This array Example 1
attribute returns import numpy as np
the number of a = [Link](24)
print (a)
array print([Link])
dimensions. 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 = [Link](24)
b=[Link](2,4,3)
print(b)
print([Link])
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 Example 1


attribute returns
the length of import numpy as np
each element of
array in bytes. x = [Link]([1,2,3,4,5], dtype = np.int8)

print [Link]
The output is as follows −

Example 2

import numpy as np

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

print [Link]
The output is as follows −

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 All elements of an array are of
different data types for example, same data type for example, an
[1,3.4, ‘hello’, ‘a@’] array of floats may be: [1.2, 5.4,
2.7]
Elements of a list are not stored Array elements are stored in
contiguously in memory. contiguous memory locations.
This makes operations on arrays
faster than lists.
Lists do not support element wise Arrays support element wise
operations, for example, operations. For example, if A1 is
addition, multiplication, etc. an array, it is possible to say
because elements may not be of A1/3 to divide each element of
same type. the array by 3.
Lists can contain objects of NumPy array takes up less space
different datatype that Python in memory as compared to a list
must store the type information because arrays do not require to
for every element along with its store datatype of each element
element value. Thus lists take separately.
more space in memory and are
less efficient.
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:

[Link]([start, ]stop, [step, ]dtype=None)

>>> import numpy as np


>>> [Link](5)
array([0, 1, 2, 3, 4])
>>> [Link](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 = [Link]([[1,2,3],[4,5,6]])

b = [Link](3,2)

print(b)

The output is as follows −

[[1, 2]

[3, 4]

[5, 6]]

Example 2

Example 2
import numpy as np
a = [Link](24)
b=[Link](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 Given two or more existing arrays,
arrays, we can stack vertically we can stack horizontally using
using the vstack() function. the hstack() function.

Example 1 Example 1

import numpy as np import numpy as np

a=[Link]([1,2,3]) a=[Link]([1,2,3])

print(a) print(a)
b=[Link]([6,7,8]) b=[Link]([6,7,8])

print(b) print(b)

print("vstack") print("hstack")

c=[Link]([a,b]) c=[Link]([a,b])

print(c) print(c)

Output: Output:

[1 2 3] [1 2 3]

[6 7 8] [6 7 8]

vstack hstack

[[1 2 3] [1 2 3 6 7 8]

[6 7 8]]

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

Answer :-

import numpy as np

print([Link])

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)

You might also like