Thursday, December 12, 2019

Beginning Numpy

This post I'm looking at continuing building my knowledge of Pandas through the training Pandas for Pentester from Pentester Academy.

Numpy allows us to use N Dimensional Arrays (NDArray)

Numpy can be very helpful when dealing with large datasets. It can be very efficient from a CPU utilisation perspective.


 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3

import numpy as np

def main():

    #print the installe version of numpy
    print('[*] The current version of numpy is {0}' .format(np.__version__))

    # Sample numpy array from a python list - one dimensional
    my_list = [10, 20, 30, 40, 50, -30, -20, 10]
    my_list = np.array(my_list)
    print('\n[*] numpy one dimensional array created from python list \n {}' .format(my_list))


    #numpy array - 2 dimensional
    my_np_array = np.array([[10,20], [30,40], [50,-30], [-20,10]])
    print('\n[*] Sample two dimensional numpy array \n{}'.format(my_np_array))

    print('\n[*] Each 2nd item in the array \n {}' .format(my_np_array[::2]))

    # To Access content in the array we can slice as follows

    print('\n[*] Printing the frist row in the array \n {}'.format(my_np_array[0]))

    print('\n[*] Printing the 2nd and 3rd row in the array \n {}'.format(my_np_array[1:3]))

    print('\n[*] Printing the 2nd colum only \n {}'.format(my_np_array[:,1]))

    print('\n[*] Printing the 2nd row and column \n {}'.format(my_np_array[1,1]))

    #Let's now multiply the value in the array by 2

    my_np_array_mul = my_np_array * 2
    print('\n[*] Array with values multiplied by 2 \n {}'.format(my_np_array_mul))

    #Add values to the np array by 10
    my_np_array_sum = my_np_array + 10

    print('\n[*] Array with values added by 10 \n {}'.format(my_np_array_sum))

    #Subtract values from np array by 15
    my_np_array_sub = my_np_array - 15

    print('\n[*] Array with values -10 \n {}'.format(my_np_array_sub))

    #Divide values from np array by 5
    my_np_array_div = my_np_array / 5
    print('\n[*] Array divide by 5 \n{}' .format(my_np_array_div))

    # Generate a 3 rows by 3 columns random array
    my_random_a = np.random.randint(1,300, (3,3))
    print('\n[*] A randomly generated 3 by 3 array \n{}'.format(my_random_a))
    my_random_b = np.random.randint(4,700,(3,3))
    print('\n[*] A randomly generated 3 by 3 array \n{}'.format(my_random_b))
    
# Let's now do one math operation against those two arrays
    print('\n[*] Sum of the values in the two random arrays \n {}'.format(my_random_a + my_random_b))

    # Let's now get the len and shape of the new combined array
    print('\n[*] The shape of the summed up array is \n {}' .format(np.shape(my_random_a + my_random_b)))
    print('\n[*] The dimension of the summed up array is \n {}' .format(np.ndim(my_random_a + my_random_b)))


    #Let's now create an array consisting of True and False
    my_true_false = np.array([True, False]*20)
    print('\n[*] Printing 20 True False \n {}'.format(my_true_false))

    # Creating a new 6 dimensional random array
    my_new_random_array = np.random.randint(1,1000, (6,6))
    print('\n[*] 6x6 array \n {}' .format(my_new_random_array))
    print('\n[*] values of X less tan 500 \n{}' .format(my_new_random_array < 500))
    print('\n[*] values of X less tan 500 \n{}' .format(my_new_random_array [my_new_random_array < 500]))


    #Create a range of numbers from 0 to 100
    create_range = np.arange(100)
    print('\n[*] Range of numbers 0 to 100 \n{}'.format(create_range))
    print('\n[*] The square of the numers in create_range are: \n {}'.format(np.square(create_range)))
    print('\n[*] The square root of the numers in create_range are: \n {}'.format(np.sqrt(create_range)))
    print('\n[*] The data type of the numers in create_range are: \n {}'.format(create_range.dtype))



'''
    #Replacing all values greater than  500 with -500 
    my_new_random_array_replaced = my_new_random_array[my_new_random_array > 500] = -500
    print('\n[*] Replacing all values greater than  500 with -500 \n{}' .format(my_new_random_array_replaced))

'''


if __name__ == '__main__':

    main()


Reference:
Pandas for Pentesters
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html
NumPy Reference


Posts in this series:
Beginning Numpy
Beginning Pandas
Pandas String Operations, etc.

No comments:

Post a Comment