Worksheet - Numpy
Worksheet - Numpy
Ans:
[92 89 34 68]
[57 14 68 34]
Ans:
[11 9 7]
Ans:
[1. 1. 1. 1. 1. 1.]
[[1. 1. 1.]
[1. 1. 1.]]
Ans:
[9 7 5 3 1]
1|Page
Visit Python4csip.com for more updates
Ans:
[8 6 4 2 0]
Ans:
[57 14 68 34 89]
Ans:
(array([0, 3, 4, 5, 7], dtype=int32),)
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
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
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]]
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)
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))
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. (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()
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)
[[14 36]
[17 47]
[10 15]]
[[14 36 17]
[47 10 15]]
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)
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]]
11 | P a g e