Numpy&Pandas 002: Basic Operation in NumPy

We make two arrays first.

1
2
3
4
5
6
7
8
9
a = np.array([[2,2,2],[3,3,3],[4,4,4]])
b = np.ones((3,3))

#array([[2, 2, 2],
# [3, 3, 3],
# [4, 4, 4]])
#array([[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]])

Arithmetic Operation

As numbers, it is possible to add, minus, multiple, divide…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#add
a+b

#array([[3., 3., 3.],
# [4., 4., 4.],
# [5., 5., 5.]])

#minus
a-b

#array([[1., 1., 1.],
# [2., 2., 2.],
# [3., 3., 3.]])

#mutiple
a*b

#array([[2., 2., 2.],
# [3., 3., 3.],
# [4., 4., 4.]])

#divide
b/a

#array([[0.5 , 0.5 , 0.5 ],
# [0.33333333, 0.33333333, 0.33333333],
# [0.25 , 0.25 , 0.25 ]])\

#pow
a**2

#array([[ 4, 4, 4],
# [ 9, 9, 9],
# [16, 16, 16]], dtype=int32)

For all of them, it executes corresponding element in the array.

Functional Operation

Besides, it can also do for Trigonometric function.

1
2
3
4
5
np.sin(a)

#array([[ 0.90929743, 0.90929743, 0.90929743],
# [ 0.14112001, 0.14112001, 0.14112001],
# [-0.7568025 , -0.7568025 , -0.7568025 ]])

For the cos and tan or others is same.

We can do some conditions, but the output is boolean value

1
2
3
4
5
6
7
8
9
10
11
12
a>3

#array([[False, False, False],
# [False, False, False],
# [ True, True, True]]


a==2

#array([[ True, True, True],
# [False, False, False],
# [False, False, False]])

If we wanna compute the value of multiplication of the matrix, we need a method of the numpy

1
2
3
4
5
np.dot(a,b)

#array([[ 6., 6., 6.],
# [ 9., 9., 9.],
# [12., 12., 12.]])

For an array, we can use some inner functions to compute it conviently

1
2
3
np.sum(a)	#27
np.max(a) #4
np.min(a) #2

Specially, we can add one more parameter and it will execute on every rows or columns

1
2
3
4
5
np.sum(a, axis=0)	#column
#array([9, 9, 9])

np.sum(a, axis=1) #row
#array([ 6, 9, 12])

It is available for min and max as well.

Besides, we can output the index of the maximum number or minimum

1
2
3
4
5
6
7
8
9
# make a new array
A = np.arange(12,0, -1).reshape((3,4))

#array([[12, 11, 10, 9],
# [ 8, 7, 6, 5],
# [ 4, 3, 2, 1]])

np.argmax(A) #0
np.argmin(A) #11

Computing the value of average

1
2
3
4
np.mean(A)	#6.5
A.mean() #6.5

np.average(A) #6.5

And median

1
np.median(A)	#6.5

There is a method, it makes an increase array and the element is summed by last element

1
2
np.cumsum(A)
#array([12, 23, 33, 42, 50, 57, 63, 68, 72, 75, 77, 78], dtype=int32)

Similarly, a minus method. But there are some differences. This method is still keep the type of the original array and the number of the element in every row will decrease one.

1
2
3
4
np.diff(A)
#array([[-1, -1, -1],
# [-1, -1, -1],
# [-1, -1, -1]])

A detection method to check whether it is zero or not, if it is not zero, the position of it will be written down. e.g. (0,0), (0,1), (0,2)…

1
2
3
4
5
np.nonzero(A) 
#(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64),
# array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))

#it means (0,1) (0,1)... of the original array are not zero

It also has sort method, differently, it sorts every row, not all elements

1
2
3
4
np.sort(A)
#array([[ 9, 10, 11, 12],
# [ 5, 6, 7, 8],
# [ 1, 2, 3, 4]])

It is possible to transpose matrix

1
2
3
4
5
6
7
8
np.transpose(A)
# or
A.T

#array([[12, 8, 4],
# [11, 7, 3],
# [10, 6, 2],
# [ 9, 5, 1]])

And there is a method called clip, it has three parameters, first is the array, second is the minimal value, last is the maximal value and it will return another array.

1
2
3
4
5
np.clip(A, 3, 9)

#array([[9, 9, 9, 9],
# [8, 7, 6, 5],
# [4, 3, 3, 3]])

As we see, the element bigger than you set, they will be the set one and for minimal value is also.

Lastly, let us see random method.

1
2
3
4
r = np.random.random((2,2))

#array([[0.38912605, 0.55904679],
# [0.11785658, 0.20156226]])

It will generate the number between 0 to 1.

Thanks for your reading, I hope it is useful for you.


End

Illustrator / Cagy

Text / Cagy

Editor / Cagy

Design / Cagy