1. Basic Arithmetics
1.1 Adding values
The ndarray with the same length generate new ndarrays by adding elements of each index. In Python's case, the numpy packages is much more efficient in terms of algebric operations because the elements for each list must be extracted and added. The reason why the numpy object is much faster than the Python list that the numpy package is written in the C language, which is the low-level programming language. The low-level programming language has a fast performance speed instead of a low abstraction for computer preprocessing tasks.
def add_list_values(list1, list2):
res = []
for i in range(len(list1)):
res.append(list1[i] + list2[i])
return res
list1 = [3, 4, 1, 2, 3]
list2 = [6, 7, 3, 4, 8]
list3 = add_list_values(list1, list2)
print(list3)
# Comparision execution time of adding values between list and ndarray
import time
import random
random.seed(1234)
# Generate test lists
list1 = [random.randint(0, 1000) for _ in range(100000)]
list2 = [random.randint(0, 1000) for _ in range(100000)]
# Measure the execution time of adding lists
start = time.time()
add_list_values(list1, list2)
end = time.time()
time_list = end - start
# Write your code below
import numpy as np
x1 = np.array(list1)
x2 = np.array(list2)
start = time.time()
x3 = x1+x2
end = time.time()
time_array = end - start
ratio = time_list/time_array
print(ratio)
1.2 Cautions of arithmetics to n-dimensional array
A possible operation method in a 1-dimensional array may be extended to an n-dimensional array. Since these operations must match each of the dimensions, it is very important to verify the dimensions. The attribute that checks the row and column of ndarray is shape.
2darray = np.array([[1, 3, 2, 3, 5],
[3, 5, 6, 7, 2],
[1, 5, 9, 3, 2],
[2, 3, 1, 5, 6]])
subset1 = scores[:, 0:2]
subset2 = scores[:, 2:4]
shape1 = subset1.shape
shape2 = subset2.shape
print(shape1, shape2)
add_subsets = subset1 + subset2
print(add_subsets)
1.3 Arithmetic methods
The method of using the max() method in numpy packages is as follows. In addition, by entering the parameters axis = 0/1 (0 : row direction, 1 : colum direction) inside the max() method, a vector can be generated without extracting each row.
2darray = np.array([[1, 3, 2, 3, 5],
[3, 5, 6, 7, 2],
[1, 5, 9, 3, 2],
[2, 3, 1, 5, 6]])
max_row_value = 2darray.max(axis = 0)
min_row_value = 2darray.min(axis = 0)
max_col_value = 2darray.max(axis = 1)
min_row_value = 2darray.min(axis = 1)
The sum() method performs an operation of adding an element inside the ndarray. Similarly, it is possible to specify which direction to add each element through axis = 0/1.
row_sum_value = 2darray.sum(axis = 0)
2. Broadcasting Numpy Arrays
2.1 Array with same value
When we want to change each element of the array by 1, there is a limit to generating it directly in the case of a large array. np.ones() has the same shape and produces an array with each element of 1.
import numpy as np
x = np.array([
[7., 9., 2., 2.],
[3., 2., 6., 4.],
[5., 6., 5., 7.]
])
ones = np.ones(x.shape)
x = x - ones
2.2 Broadcasting
When we want to chagne a value other than 1, the numpy pacakges guarantees an operation between the ndarray and the constant. Broadcasting is to proceed with the operation considering that there exists a constant ndarray equal to the shape of ndarray in the operation between the ndarray and the constant. It is important to pay attention to the method in which the operation proceed by expanding the value with less order on one side to a large order.
x = np.array([
[4, 2, 1, 5],
[6, 7, 3, 8]
])
y = np.array([
[1],
[2]
])
z = x + y
print(z)
2.3 reshape method
It is necessary to change the shape of the array for matrix operation differnet from broadcsting. The reshape() method rearrange the array based on the tuple input there in.
x = np.array([1, 2, 3, 4, 5])
y = x.reshape((5, 1))
print(y)
The reshape method extracts elements of ndarray in rows and sotres them a new. To extract elements based on columns, we must set the parameter with order = 'F'.
numbering_by_row = x.reshape((5,1))
numbering_by_col = x.reshape((5,1), order = 'F')
'Data Science > Numpy' 카테고리의 다른 글
[numpy] Processing Datasets, Boolean, and Datatypes in Numpy (0) | 2022.10.03 |
---|---|
[numpy] Basic operations of Numpy array (0) | 2022.10.03 |