import numpy
NumPy
Create Array
= [1,2,3] mylist
= numpy.array(mylist) myarray
print(myarray)
[1 2 3]
print(myarray.shape)
(3,)
Access Data
= [[1,2,3], [4,5,6]] mylist
= numpy.array(mylist) myarray
print(myarray)
[[1 2 3]
[4 5 6]]
print(myarray.shape)
(2, 3)
print(f'First row is: {myarray[0]}')
First row is: [1 2 3]
print(f'Last row is: {myarray[-1]}')
Last row is: [4 5 6]
print(f'An element at specific row {0} and col {2} is: {myarray[0,2]}')
An element at specific row 0 and col 2 is: 3
print(f'Whole col {2} is: {myarray[:,2]}')
Whole col 2 is: [3 6]
Arithmetic
= numpy.array([2,3,4])
array1 = numpy.array([5,6,7]) array2
print(f'Adding two arrays: {array1 + array2}')
Adding two arrays: [ 7 9 11]
print(f'Multiplying two arrays: {array1*array2}')
Multiplying two arrays: [10 18 28]