Python: Numpay – P3
Learning : Python Numpy – P3
Subject: numpy array and some basic commands
The numpy lessons and basic commands will take us to plotting the data and presenting the numbers using the numpy and plot packages, but first we need to do more practices on arrays and functions in the numpy.
To get a row or a column from the array we use:
# Generate a 5x5 random array: ar = np.random.randint(10,60, size=(5,5)) print('\n A random generated array 5x5 is: \n',ar) # get the rows from 1 to 3 (rows 1 and 2): print('\n The rows from 1 to 3 is: \n',ar[1:3]) # get row 1 and row 3: print('\n The row 1 and row 2 is: \n',ar[1],ar[3]) # get the column 1 and column 3: print('\n The column 1 and column 3: \n',ar[:,[1,3]]) [Output]: A random generated array 5x5 is: [[59 43 46 44 39] [16 15 14 19 22] [59 16 33 59 19] [21 15 51 41 28] [48 46 58 33 19]] The rows from 1 to 3 is: [[16 15 14 19 22] [59 16 33 59 19]] The row 1 and row 2 is: [16 15 14 19 22] [21 15 51 41 28] The column 1 and column 3: [[43 44] [15 19] [16 59] [15 41] [46 33]]
To change a value in the array we give the position and new value as:
# Generate a 5x5 random array: ar = np.random.randint(10,60, size=(5,5)) print('\n A random generated array 5x5 is: \n',ar) print('\n Value in position (1,1):',ar[1][1]) # Re-set the value in position (1,1) to 55 ar[1][1] = 55 print('\n The array ar\n',ar) code [Output]: A random generated array 5x5 is: [[39 53 34 59 30] [33 10 42 20 36] [10 37 20 35 28] [26 18 14 41 24] [48 22 19 18 44]] Value in position (1,1): 10 The array ar [[39 53 34 59 30] [33 55 42 20 36] [10 37 20 35 28] [26 18 14 41 24] [48 22 19 18 44]]
If we have a one dimension array with values, and we want to create another array with values after applying a certain conditions, such as all values grater than 7.
# Create 1D array of range 10 ar = np.arange(10) print(ar) # ar_g7 is a sub array from ar of values grater then 7 ar_g7= np.where(ar >7) print('ar_g7:'ar_g7) [Output]: [0 1 2 3 4 5 6 7 8 9] ar_g7:(array([8, 9]),)
If we want to pass a 3×3 array and then we want the values to be changed to (1) if it is grater than 7 and to be (0) if it is less than 7.
# Generate a 3x3 array of random numbers. ar2 = np.random.randint(1,10, size =(3,3)) print(ar2) # Change any value grater than 7 to 1 and if less than 7 to 0. ar_g7= np.where(ar2 >7, 1 ,0) print('ar_g7:',ar_g7) [Output]: [[6 4 2] [8 5 1] [5 2 8]] ar_g7: [[0 0 0] [1 0 0] [0 0 1]]
Also we can say if, the value in the array is equal to 6 or 8 then change it to -1.
# Generate array of 3x3 ar2 = np.random.randint(1,10, size =(3,3)) print(ar2) # If the = 6 or 8 change it to (-1) ar_get_6_8_value= np.where((ar2 == 6) |( ar2==8), -1 ,ar2) print('ar_get_6_8_value:',ar_get_6_8_value) [Output]: [[3 4 8] [1 9 3] [5 6 6]] ar_get_6_8_value: [[ 3 4 -1] [ 1 9 3] [ 5 -1 -1]]
We can get the index location of the certain conditions values, and then we can print it out.
# # Generate array of 3x3 ar_less_6= np.where((ar2 < 6) ) print('ar_less_6 locations:',ar_less_6) # print out the values on those locations. print('ar_less_6 values: ',ar2[ar_less_6]) [Output]: [[6 1 9] [1 8 6] [6 9 2]] ar_less_6 locations: (array([0, 1, 2]), array([1, 0, 2])) ar_less_6 values :[1 1 2]
:: numpy Sessions ::
Sessions 1 | Sessions 2 | Sessions 3 | Sessions 4 |
To Download my Python code (.py) files Click-Here
-
November 13, 2019 at 10:48 amPython: Numpay – P2 | Ali's Photography Space...
-
November 13, 2019 at 10:55 amlearning – Temp | Ali's Photography Space...