0% found this document useful (0 votes)
2 views11 pages

Worksheet - Numpy

The document is a worksheet focused on Numpy, containing various coding exercises and their expected outputs. It includes tasks such as slicing arrays, reshaping, and performing mathematical operations, along with questions about Numpy's characteristics and functionalities. Each section provides code snippets followed by the answers for verification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Worksheet - Numpy

The document is a worksheet focused on Numpy, containing various coding exercises and their expected outputs. It includes tasks such as slicing arrays, reshaping, and performing mathematical operations, along with questions about Numpy's characteristics and functionalities. Each section provides code snippets followed by the answers for verification.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Visit Python4csip.

com for more updates


WORKSHEET – Numpy
1 What will be the output of following code-
import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
print(A[7:3:-1])
print(A[2:6])

Ans:

[92 89 34 68]
[57 14 68 34]

2 What will be the output of following code-


import numpy as np
A=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
print(A[10:5:-2])

Ans:

[11 9 7]

3 What will be the output of following code-


import numpy as np
A=np.ones(6)
print(A)
B=np.reshape(A,(2,3))
print(B)

Ans:

[1. 1. 1. 1. 1. 1.]
[[1. 1. 1.]
[1. 1. 1.]]

4 What will be the output of following code-


import numpy as np
arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[::-2])

Ans:

[9 7 5 3 1]

5 What will be the output of following code-


import numpy as np
arr= np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[-2::-2])

1|Page
Visit Python4csip.com for more updates
Ans:

[8 6 4 2 0]

6 What will be the output of following program:


import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
print(A[-6:len(A)-1])

Ans:

[57 14 68 34 89]

7 What will be the output of following program:


import numpy as np
A=np.array([24,46,57,14,68,34,89,92])
B=np.array([24,78,66,14,68,34,70,92])
c=np.where(A==B)
print(c)

Ans:
(array([0, 3, 4, 5, 7], dtype=int32),)

8 Point out the Correct Statement:


1. We can not change the size of NumPy array.
2. NumPy array can contain elements of non-homogenous type.
3. Python List occupy less space than a NumPy array.
4. All of the Above.
Ans:

1. We can not change the size of NumPy array.

9 WAP to swap first two columns in a 2D numpy array?

Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[:, [1,0,2]])
Or
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
arr[:, [0,1]]=arr[:,[1,0]]
print(arr)

2|Page
Visit Python4csip.com for more updates

10 WAP to swap first two rows in a 2D numpy array?

Ans:

import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[[1,0,2], :])

OR
import numpy as np
A = np.arange(9).reshape(3,3)
A[[0,1]] = A[[1,0]]
print(A)
11 WAP to reverse the rows in a 2D numpy array?

Ans:
import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[::-1])
12 WAP to reverse the columns in a 2D numpy array?

Ans:

import numpy as np
arr = np.arange(9).reshape(3,3)
print(arr)
print(arr[:, ::-1])
13
WAP in Given a 1D array to negate all elements which are between 3 and 8.
Ans:

import numpy as np
A = np.arange(11)
A[(A>=3) & (A<=8)] *= -1
print(A)

3|Page
Visit Python4csip.com for more updates
14 WAP to subtract the mean from each row of a 5*5 array.
Ans:
import numpy as np
X = np.arange(25).reshape(5,5)
print(X)
print(X.mean(axis=1))
Y = X - X.mean(axis=1)
print(Y)
15 WAP in a given numpy array to return array of odd rows and even columns

sampleArray = numpy.array([[3 ,6, 9, 12], [15 ,18, 21, 24],


[27 ,30, 33, 36], [39 ,42, 45, 48], [51 ,54, 57, 60]])

Expected Output:
Printing Input Array
[[ 3 6 9 12]
[15 18 21 24]
[27 30 33 36]
[39 42 45 48]
[51 54 57 60]]

Printing array of odd rows and even columns


[[ 6 12]
[30 36]
[54 60]]

Ans:

import numpy
sampleArray = numpy.array([[3 ,6, 9, 12], [15 ,18, 21, 24],
[27 ,30, 33, 36], [39 ,42, 45, 48], [51 ,54, 57, 60]])
print("Printing Input Array")
print(sampleArray)

print("\n Printing array of odd rows and even columns")

4|Page
Visit Python4csip.com for more updates
newArray = sampleArray[::2, 1::2]
print(newArray)

16 WAP to Create a 5X2 integer array from a range between 100 to 200 such that
the difference between each element is 10

Ans:

import numpy
print("Creating 5X2 array using numpy.arange")
A= numpy.arange(100, 200, 10)
print(A.reshape(5,2))

17 WAP to Create a 4X2 integer array and Prints its attributes


The element must be a type of unsigned int16. And print the following
Attributes: –
• The shape of an array.
• Array dimensions.
• The Length of each element of the array in bytes.
• Ans:
import numpy as np
A=np.zeros([4,2], dtype =int)
print("Printing Array")
print(A)

print("Printing numpy array Attributes")


print("1> Array Shape is: ", A.shape)
print("2>. Array dimensions are ", A.ndim)
print("3>. Length of each element of array in bytes is ", A.itemsize)

18 WAP to create a 3*3 numpy array with all the elements as per the user choice
and print the sum of all elements of the array.
Ans:
import numpy as np
a=np.zeros(9,dtype=int).reshape(3,3)
sum=0
for i in range(3):

5|Page
Visit Python4csip.com for more updates
for j in range(3):
num=int(input('enter element of array'))
a[i][j]=num
sum=sum+a[i][j]
print(a)
print('summ of All elements of array is:',sum)
19 Which of the following is a data type of elements of NumPy array created by
the linespace() method?
1. float64 2. int32 3. bool 4.int16
Ans:
1
20 Which of the following is used to create one dimensional array from string?
1. formstring() 2. Fromstr() 3. fromstring() 4.fromstr()
Ans:
3
21 What is the use of reshape() method?

1. To create 2D array from 1D array


2. To create 1D array from 2D array
3. Both 1 and 2
4. None of the above
Ans:
1
22 What is the output of the following program?
import numpy as np
A=np.array([[3,2,4],[3,4,5]])
print(A.T.shape)

1. (2,3)
2. (3,3)
3. (3,2)
4. None
Ans:

(3,2)
23 What will be the output of the program?
import numpy as np
a=np.array([2,3,4,5])
b=np.array([6,7,8,9])
c=a+b
print(c)

Ans:

[8,10,12,14]
6|Page
Visit Python4csip.com for more updates
24 Which of the following is not the method of linear regression?
1. Best Fit Line Method
2. polyfit() method
3. plot() method
4. None

Ans:
3
25 What does positive covariance means?
1. Similar
2. Same
3. Strongly similar
4. None

Ans:
3
26 Slicing is specified by using _____________ operator.
Ans:
colon
27 Transpose of a 2D array can be done by using ______________.

Ans:
.T or transpose()

28 NumPy array supports ________________ operations which is not supported in


Python List.

Ans:
vectorized
29 An array consist of _____________ and _________________.

Ans:
Element and index
30 NumPy is an _________________ module of Python.

Ans:
open-source
31 Write a program to convert a string into an array.

Ans:
import numpy as np
A=np.fromstring('1 2 3 4', dtype=int, sep=' ')
print(A)

32 Write a program in numpy to count the number of even and odd element in 1D
array.

Ans:
import numpy as np

7|Page
Visit Python4csip.com for more updates
a=np.arange(1,20)
counteven=0
countodd=0
print(a)
for i in range(len(a)):
if(a[i]%2==0):
counteven=counteven+1
else:
countodd=countodd+1
print('No of even elements are::::',counteven)
print('No of Odd elements are::::',countodd)
33 Write a Program to store 30 zeros in an array.
Ans:

import numpy as np
a=np.zeros(30)
print(a)
34 WAP to compute the covariance of two arrays.

Ans:
import numpy as np
a=np.array([11,22,33,44,55])
b=np.array([66,77,88,99,110])
print(np.cov(a,b))
35 Write a program to check whether none of the elements of a given array is
zero.

Ans:
import numpy as np
a=np.arange(0,20)
print(a)
if np.all(a):
print(‘Array does’nt contain Zeros’)
else:
print(‘Array contains Zeros’)

36 Write a program in NumPy to create two array and store 5 ones in first and 5
tens in second array and store the sum of both the array in third array.

Ans:

8|Page
Visit Python4csip.com for more updates
import numpy as np
First=np.ones(5,dtype=int)
Second=np.full(5,10,dtype=int)
Third=First+Second
print(First)
print(Second)
print(Third)
37 Write a program in NumPy to create 9*9 array in which the elements in the
alternate row(i.e. odd rows) will be equal to 1s and others as 0s.

Ans:

import numpy as np
A=np.full((9,9),1,dtype=int)
for i in range(len(A)):
if i%2==0:
A[i]=0
print(A)
38 Write a NumPy program to create 3*3 array in which elements entered by the
user:

Input: Output:

36 4 6 36 4 0

7 8 9 0 8 0
16 3 5 16 0 0

Ans:
import numpy as np
a1=np.array([[36,4,6],[7,8,9],[16,3,5]])
r,c=a1.shape
t=np.empty((3,3),dtype=int)
for i in range(r):
for j in range(c):
if a1[i][j]%4==0:
t[i][j]=a1[i][j]

9|Page
Visit Python4csip.com for more updates
else:
t[i][j]=0
print(a1)
print('Array after transsformation')
print(t)

39 What will be the output of following program:


import numpy as np
a1=np.array([[14,36],[17,47]])
a2=np.array([[10,15]])
a3=np.concatenate((a1,a2),axis=0)
print(a3)
a4=a3.reshape(2,3)
print()
print(a4)
Ans:

[[14 36]
[17 47]
[10 15]]

[[14 36 17]
[47 10 15]]

40 Write a NumPy program to create a 3*3 identity matrix i.e. , diagonal of


elements are 1 and the rest are 0. Replace all 0s with any random number from
5 to 20.

Ans:
import numpy as np
import random
a=np.zeros(9,dtype=int).reshape(3,3)
for i in range(3):
for j in range(3):
if i==j:
a[i][j]=1
else:
a[i][j]=random.randint(5,21)
print(a)

10 | P a g e
Visit Python4csip.com for more updates
41 WAP in NumPy to Create a null vector of size 10 but the fifth value which is 1.

Ans:
import numpy as np
a = np.zeros(10)
a[4] = 1
print(a)

42 WAP in NumPy to Create a 3x3 identity matrix.

Ans:
import numpy as np
a=np.eye(3)
print(a)
43 What will be the output of following program?
import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y.T), axis=1)
print(z)

Ans:
[[ 1 2 12]
[ 3 4 30]]

44 What will be the output of following program?


import numpy as np
x=np.array([[1,2],[3,4]])
y=np.array([[12,30]])
z=np.concatenate((x,y), axis=None)
print(z)
Ans:
[ 1 2 3 4 12 30]

11 | P a g e

You might also like