The numpy library lets you run mathematical expressions on elements of a list. The math library cannot do this. Study the examples below and then run the code cell.
What if you wanted to change the shape of the array?
For example, we can turn the 2D grid from above into a 1D array. Here, the -1 means automatically fit all values into this 1D shape
np_1D = np.reshape(np_grid, (1, -1))
print(np_1D)
We can also create a 2D array of zeros or ones
which is useful for car world creation and analysis. For example, create a 5x4 array
print('\nExample of squaring elements in a list')
print(np.square([1, 2, 3, 4, 5]))
print('\nExample of taking the square root of a list')
print(np.sqrt([1, 4, 9, 16, 25]))
print('\nExamples of taking the cube of a list')
print(np.power([1, 2, 3, 4, 5], 3))
Example of squaring elements in a list
[ 1 4 9 16 25]
Example of taking the square root of a list
[ 1. 2. 3. 4. 5.]
Examples of taking the cube of a list
[ 1 8 27 64 125]