IKH

chapter 25

Numpy Quiz Questions

Example

Python
import numpy as np
a = np.array([])
print(a.shape)

Output

PowerShell
(0,)

Example

Python
import numpy as np
a = np.arange(10,20,-1)
b = a.reshape(5,2)
print(b.flatten())

Output

PowerShell
---------------------------------------------------------------------------
ValueError                               Traceback (most recent call last)
<ipython-input-533-d495ed6809f0> in <module>
      1 import numpy as np
      2 a = np.arange(10,20,-1)
----> 3 b = a.reshape(5,2)
      4 print(b.flatten())
      
ValueError: cannot reshape array of size 0 into shape (5,2)

Example

Python
import numpy as np
a = np.arange(1,6)
a = a[::-2]
print(a)

Output

PowerShell
[5 3 1]

Example

Python
a = np.array([3,3])
b = np.array([3,3.5])
c = np.array([3,'3'])
d = np.array([3,True])
print(f"a dtype =>{a.dtype}")
print(f"b dtype =>{b.dtype}")
print(f"c dtype =>{c.dtype}")
print(f"d dtype =>{d.dtype}")

Output

PowerShell
a  dtype =>int32
b  dtype =>float64
c  dtype =><U11
d  dtype  >int32 

Example

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

Output

PowerShell
[3 2 3]

Example

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

Output

PowerShell
[3 3 5]

Example

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

Output

PowerShell
[1]

Example

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

Output

PowerShell
[]

Example

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

Output

PowerShell
[2 3 3]

Example

Python
a = np.arange(20).reshape(5,4)
print(f" value of a[3][3] ==> {a[3][3]}")
print(f" value of a[-2][-1] ==> {a[-2][-1]}")
print(f" value of a[3][-1] ==> {a[3][-1]}")
print(f" value of a[3,3] ==> {a[3,3]}")

Output

PowerShell
value of a[3][3] ==> 15
value of a[-2][-1] ==> 15
value of a[3][-1] ==> 15
value of a[3,3] ==> 15

11 question se

27 question se tak

np.trunc(a)

  • Remove the digits after decimal point.

Example

Python
print(np.trunc(1.23456))
print(np.trunc(1.99999))

Output

PowerShell
1.0
1.0

np.fix(a)

  • Round to nearest integer towards zero

Example

Python
print(np.fix(1.234546))
print(np.fix(1.99999999))

Output

PowerShell
1.0
1.0

np.around(a)

  • It will perform round operation.
  • If the next digit is >=5 then remove that digit by incrementing previous digit.
  • If the next digit is <5 then remove that digit and we are not required to do anything with the previous digit.

Example

Python
print(np.around(1.23456))
print(np.around(1.99999))

Output

PowerShell
1.0
2.0

Example

Python
help(np.cumsum)

Output

PowerShell
Help on function cumsum in module numpy: 

cumsum(a, axis=None, dtype=None, out=None)
    Return the cumulative sum of the elements along a given axis.

Example

Python
a = np.array([10,20,30,40])
print(np.cumsum(a))

Output

PowerShell
[ 10 30 60 100]

28 tak ho gya