dzone python training institute

Python NumPy Exercise


Solution

import numpy as np Arr1 = np.empty([4,2], dtype = np.uint16) print(Arr1) print("Shape : ", Arr1.shape) print("Dimensions ", Arr1.ndim) print("Element Length (bytes) : ", Arr1.itemsize)

Output


>>>
[[21390 33628]
[2079 365]
[6048 5162]
[ 25 0]]


Shape : (4, 2)
Dimensions 2
Element Length (bytes) : 2



import numpy as np

ndArray = np.arange(500, 320, 10)
ndArray = ndArray.reshape(5,2)
print (ndArray)

>>>
[[500 230]
[240 250]
[260 270]
[280 290]
[300 310]]



import numpy as np

DzoneArray = np.array([[10 ,20, 30], [40, 50, 60], [70, 80, 90]]) 
print("Array :")
print(DzoneArray)

print("\n All data in  second column ")
newArray = DzoneArray[...,1]
print(newArray)

>>>
Array:
[[10 20 30]
[40 50 60]
[70 80 90]]

All data in second column
[20 50 80]


        import numpy as np

        TestArray = np.array([[4 ,8, 33, 29], [20 ,41, 28, 12],
        [36 ,18, 44, 37], [52 ,56, 19, 64], [68 ,50, 76, 80]])
        print("Org. :")
        print(TestArray)

        print("\n narray of odd rows and even columns")
        newArray = TestArray[::2, 1::2]
        print(newArray)

>>>
Org. :
[[ 4 8 33 29]
[20 41 28 12]
[36 18 44 37]
[52 56 19 64]
[68 50 76 80]]

narray of odd rows and even columns
[[ 8 29]
[18 37]
[50 80]]



import numpy

arr1 = numpy.array([[5, 8, 6], [21 ,20, 27]])
arr2 = numpy.array([[15 ,33, 24], [4 ,5, 1]])

resultArray = arr1 + ar2
print("addition of arrays is \n")
print(resultArray)

for num in numpy.nditer(resultArray, op_flags = ['readwrite']):
    num[...] = num*num
print("\n After square \n")
print(resultArray)

>>>
addition of arrays is

[[20 41 30]
[25 25 28]]

After square

[[ 400 1681 900]
[ 625 625 784]]



        import numpy

        print("9X3 numpy array")
        Arr1 = numpy.arange(10, 37, 1)
        Arr1 = Arr1.reshape(9,3)
        print (Arr1)

        print("\n Spliting into 3 sub array\n")
        subArrays = numpy.split(Arr1, 3)
        print(subArrays)

>>>
9X3 numpy array

[[13 14 15]
[16 17 18]
[19 20 21]
[50 23 24]
[25 26 27]
[28 29 30]
[31 32 33]
[11 35 36]
[37 38 39]]

Spliting into 3 sub array

[array([[10, 11, 12],[13, 14, 15],[16, 17, 18]]),
array([[19, 20, 21],[50, 23, 24],[25, 26, 27]]),
array([[28, 29, 30],[31, 32, 33],[11, 35, 36]])]



import numpy

print("Original array")
Arr1 = numpy.array([[11,43,49],[82,50,12],[53,23,77]]) 
print (Arr1)

sortByRow = Arr1[:,Arr1[1,:].argsort()]
print("Sorting by second row")
print(sortByRow)

print("Sorting by second column")
sortByCol = Arr1[ndArray[:,1].argsort()]
print(sortByCol)

>>>
Org:
[[11 43 49]
[82 50 12]
[53 23 77]]

Sorting by second row
[[49 43 11]
[12 50 82]
[77 23 53]]

Sorting by second column
[[82 50 12]
[11 43 49]
[53 23 77]]



import numpy

print("Org:")
ndArray = numpy.array([[11,43,49],[82,50,12],[53,23,77]]) 
print (dzoneArray)


min_Axis1 = numpy.amin(dzoneArray, 1) 
print("min Of Axis 1")
print(min_Axis1)

max_Axis1 = numpy.amax(dzoneArray, 0) 
print("max Of Axis 0")
print(max_Axis1)


>>>
org:
[[11 43 49]
[82 50 12]
[53 23 77]]
min Of Axis 1
[11 12 53]
max Of Axis 0
[82 23 49]


import numpy

print("Org:")
Arr1 = numpy.array([[11,43,49],[82,50,12],[53,23,77]]) 
print (Arr1)

print("After deleting col 2 on axis 1")
Arr1 = numpy.delete(Arr1 , 1, axis = 1) 
print (Arr1)

arr = numpy.array([[10,10,10]])

print("after inserting col 2 on axis 1")
Arr1 = numpy.insert(ndArray , 1, arr, axis = 1) 
print (ndArray)

>>>
Org:
[[11 43 49]
[82 50 12]
[53 23 77]]

After deleting col 2 on axis 1

[[11 49]
[82 12]
[53 77]]
After inserting col 2 on axis 1

[[11 10 49]
[82 10 12]
[53 10 77]]



import numpy

x = np.array([11,6,15,8,3,6]) 
y = np.array([11,3,4,5,2,6])

print(np.where(x == y))