Visit Python4csip.
com for more updates
WORKSHEET – Numpy
1 What will be the output of following code-
import numpy as np
A=[Link]([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=[Link]([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=[Link](6)
print(A)
B=[Link](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= [Link]([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= [Link]([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arr[-2::-2])
1|Page
Visit [Link] for more updates
Ans:
[8 6 4 2 0]
6 What will be the output of following program:
import numpy as np
A=[Link]([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=[Link]([24,46,57,14,68,34,89,92])
B=[Link]([24,78,66,14,68,34,70,92])
c=[Link](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 = [Link](9).reshape(3,3)
print(arr)
print(arr[:, [1,0,2]])
Or
import numpy as np
arr = [Link](9).reshape(3,3)
print(arr)
arr[:, [0,1]]=arr[:,[1,0]]
print(arr)
2|Page
Visit [Link] for more updates
10 WAP to swap first two rows in a 2D numpy array?
Ans:
import numpy as np
arr = [Link](9).reshape(3,3)
print(arr)
print(arr[[1,0,2], :])
OR
import numpy as np
A = [Link](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 = [Link](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 = [Link](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 = [Link](11)
A[(A>=3) & (A<=8)] *= -1
print(A)
3|Page
Visit [Link] for more updates
14 WAP to subtract the mean from each row of a 5*5 array.
Ans:
import numpy as np
X = [Link](25).reshape(5,5)
print(X)
print([Link](axis=1))
Y = X - [Link](axis=1)
print(Y)
15 WAP in a given numpy array to return array of odd rows and even columns
sampleArray = [Link]([[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 = [Link]([[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 [Link] 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 [Link]")
A= [Link](100, 200, 10)
print([Link](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=[Link]([4,2], dtype =int)
print("Printing Array")
print(A)
print("Printing numpy array Attributes")
print("1> Array Shape is: ", [Link])
print("2>. Array dimensions are ", [Link])
print("3>. Length of each element of array in bytes is ", [Link])
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=[Link](9,dtype=int).reshape(3,3)
sum=0
for i in range(3):
5|Page
Visit [Link] 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() [Link]()
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=[Link]([[3,2,4],[3,4,5]])
print([Link])
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=[Link]([2,3,4,5])
b=[Link]([6,7,8,9])
c=a+b
print(c)
Ans:
[8,10,12,14]
6|Page
Visit [Link] 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=[Link]('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 [Link] for more updates
a=[Link](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=[Link](30)
print(a)
34 WAP to compute the covariance of two arrays.
Ans:
import numpy as np
a=[Link]([11,22,33,44,55])
b=[Link]([66,77,88,99,110])
print([Link](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=[Link](0,20)
print(a)
if [Link](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 [Link] for more updates
import numpy as np
First=[Link](5,dtype=int)
Second=[Link](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=[Link]((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=[Link]([[36,4,6],[7,8,9],[16,3,5]])
r,c=[Link]
t=[Link]((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 [Link] 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=[Link]([[14,36],[17,47]])
a2=[Link]([[10,15]])
a3=[Link]((a1,a2),axis=0)
print(a3)
a4=[Link](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=[Link](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]=[Link](5,21)
print(a)
10 | P a g e
Visit [Link] 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 = [Link](10)
a[4] = 1
print(a)
42 WAP in NumPy to Create a 3x3 identity matrix.
Ans:
import numpy as np
a=[Link](3)
print(a)
43 What will be the output of following program?
import numpy as np
x=[Link]([[1,2],[3,4]])
y=[Link]([[12,30]])
z=[Link]((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=[Link]([[1,2],[3,4]])
y=[Link]([[12,30]])
z=[Link]((x,y), axis=None)
print(z)
Ans:
[ 1 2 3 4 12 30]
11 | P a g e