|
| 1 | +""" Author : Juzer Shakir || Created on 10 Nov 2017 || Last modified on 19 Dec 2017 10:10am |
| 2 | +Topic : How to use numpy module in python (Introduction 1) |
| 3 | +
|
| 4 | +What Numpy can do? |
| 5 | +#1. Find the dimension of the array |
| 6 | +#2. Find the byte size of each element |
| 7 | +#3. Find the data type of the elements |
| 8 | +#4. Find the size of an array (finding total number of elements) |
| 9 | +#5. Find the shape of an array (giving you no. of columns and rows) |
| 10 | +#6. You can reshape the array |
| 11 | +#7. You can slice the array |
| 12 | +#8. Finding min, max and sum of the elements in the array""" |
| 13 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 14 | + |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +#----1 |
| 18 | +a = np.array([(1, 2, 3), (4, 5, 6)]) |
| 19 | +print(a) |
| 20 | +#this will give us the dimension of the numpy variable |
| 21 | +dim = a.ndim |
| 22 | +print("\nThe dimension of this array is --> {}".format(dim)) |
| 23 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 24 | + |
| 25 | +#----2 |
| 26 | +#this will give the byte size of the array |
| 27 | +size = a.itemsize |
| 28 | +print("\nThe size is --> {} bytes".format(size)) |
| 29 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 30 | + |
| 31 | +#----3 |
| 32 | +#this will give the data type the array |
| 33 | +data_type = a.dtype |
| 34 | +print("\nThe data type of this array is --> {}".format(data_type)) |
| 35 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 36 | + |
| 37 | +#----4 |
| 38 | +#this will give the size of the array(no.of elements) |
| 39 | +size = a.size |
| 40 | +print("\nThe number of elements in this array are --> {}".format(size)) |
| 41 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 42 | + |
| 43 | +#-----5 |
| 44 | +#this will give the shape of the array (no. of rows and no. of columns) |
| 45 | +shape = a.shape |
| 46 | +print("\nThere are {} number of rows and columns respectively.".format(shape)) |
| 47 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 48 | + |
| 49 | +#-----6 |
| 50 | +#this will reshape the shape of the array to different rows and columns (arguments - (r,c)) |
| 51 | +reshape = a.reshape(3, 2) |
| 52 | +print("\nThe new shape of the array is \n {}".format(reshape)) |
| 53 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 54 | + |
| 55 | +#-----7 |
| 56 | +#this will output a specific number in the elemnts of an array(arguments (r,c)) |
| 57 | +n = a[0,2] |
| 58 | +print("\nThe number at the first row and third column is {}".format(n)) |
| 59 | +#printing third column of every rows (arguments (r,c)) |
| 60 | +n3 = a[0: ,2] |
| 61 | +print("\nThe number at every row of third column is {}".format(n3)) |
| 62 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 63 | + |
| 64 | +#----8 |
| 65 | +#Finding maximum value in the array |
| 66 | +maxn = a.max() |
| 67 | +print("\nThe maximum value in this array is {}".format(maxn)) |
| 68 | +#Finding minimum value in the array |
| 69 | +minn = a.min() |
| 70 | +print("\nThe minimum value in this array is {}".format(minn)) |
| 71 | +#Finding sum of all the value in the array |
| 72 | +sumn = a.sum() |
| 73 | +print("\nThe sum of all the value in the array is {}".format(sumn)) |
| 74 | +"""----------------------------------------------------------------------------------------------------------------------------------------------------------""" |
| 75 | + |
| 76 | + |
| 77 | +#That's it! |
| 78 | +print("\nThe End!") |
0 commit comments