Arrays are indispensable in scientific and technical calculations. Perform basic array operations using numpy.
(1) Creating an array from the "list" (2) Display / retrieval of array elements (3) "Add" and "Concatenate" arrays (4) Sorting of array elements (5) "Four arithmetic operations" of arrays (6) Applying functions to array elements [Addendum] Differences from the Python standard "(multiple) list"
import numpy as np
"""
Creating an "array"
"""
a_list = [0,1,2,3,4,5] # a_Create a list named list and element 0, 1, 2, 3, 4,Store 5
a_array = np.array(a_list) # a_Make list an "array"
print(a_array)
[0 1 2 3 4 5]
(2)
a_list = [0,1,2,3,4,5] # a_Create a list named list and element 0, 1, 2, 3, 4,Store 5
a_array = np.array(a_list) # a_Make list an "array"
print(a_array[0]) #Display element zero of the array(Count from zero) # A
print(a_array[1]) # **No. 1#B
print(a_array[-1]) #Count from the back. From the back-1, -2, ...It is in the order.#C
print(a_array[1:3]) #Element number 1(The second element from the left because it counts from zero)Or create an array up to the third array element#D
print(a_array[2:]) #Element number 2(The third element from the left because it counts from zero)Create an array containing from to one before the last element#D
print(a_array[:4]) #Create an array containing element numbers 0 to 4th array element#F
0 #A 1 #B 5 #C [1 2] #D [2 3 4 5] #E [0 1 2 3] EF
(Note) The alphabet at the end corresponds to the alphabet on the output line of the print function in the above code.
Let np.append (array, element).
example(3-1)
import numpy as np
a_list = [0,1,2,3,4,5] # a_Create a list named list and element 0, 1, 2, 3, 4,Store 5
a_array = np.array(a_list) # a_Make list an "array"
a_array = np.append(a_array,10) #element'10'Array a_Add to array
print(a_array)
[ 0 1 2 3 4 5 10]
example(3-2)Array concatenation
import numpy as np
a_list = [0,1,2,3,4,5] # a_Create a list named list and element 0, 1, 2, 3, 4,Store 5
a_array = np.array(a_list) # a_list is "array" a_to array
b_list=[200, 300, 400] #b_Create a list named list and element 200, 300,Store 400
b_array = np.array(b_list) # b_list to "array" b_to array
a_array = np.append(a_array, b_array) ##Array a_Array b in array_Concatenate arrays
print(a_array)
[ 0 1 2 3 4 5 200 300 400]
You can use sort to get an array in which the array elements are arranged in order from the smallest number to the largest number (ascending order). Use the sorted and ** reverse ** options to sort in the reverse (descending order) of sort.
(4)sort
import numpy as np
c_list = [14, 90, 4, 23, 61, 110, 75]
c_array = np.array(c_list)
sorted1=np.sort(c_array) #Array c_Construct an array named sorted1 with arrays arranged in ascending order
sorted2=np.array(sorted(c_array, reverse=True)) #Array c_Build an array named sorted2 with arrays arranged in descending order
print(sorted1)
print(sorted2)
[ 4 14 23 61 75 90 110] [110 90 75 61 23 14 4]
import numpy as np
"""
"Four arithmetic operations" of arrays
"""
D1_array=np.array([1, 2, 3])
D2_array=np.array([4, 5, 6])
print(D1_array+D2_array) #Addition= numpy.add(D1_array, D2_array) #A
print(D1_array-D2_array) #Subtraction= numpy.subtract(D1_array, D2_array)#B
print(D1_array*D2_array) #Multiply= numpy.multiply(D1_array, D2_array) #C
print(D1_array/D2_array) #division= numpy.divide(D1_array, D2_array) #D
print(D1_array%D2_array) #Surplus= numpy.add(D1_array, D2_array) #E
print(D1_array**D2_array) #Exponentiation numpy.power(D1_array, D2_array) #F
[5 7 9] #A [-3 -3 -3] #B [ 4 10 18] #C [ 0.25 0.4 0.5 ] #D [1 2 3] #E [ 1 32 729] #F
(Note) The alphabet at the end of the list corresponds to the alphabet on the output line of the print function in the above code.
Example(6)Function mapping
import numpy as np
"""
Applying the same function to array elements
"""
D_array=np.array([1, 2, 3]) #Element 1, 2,Array D with 3_Build array
print(np.cos(D_array)) # D_Evaluate the cos function value with all elements of array as arguments
print(np.sqrt(D_array)) # D_Sqrt function with all elements of array as arguments(root)Evaluate the value
[ 0.54030231 -0.41614684 -0.9899925 ] [ 1. 1.41421356 1.73205081]
An [object] called ndarray created by numpy.array (list) (https://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%96%E3%82%B8%E3%82% A7% E3% 82% AF% E3% 83% 88_ (% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3 Comparing% E3% 82% B0)) with a list object by python, it has the following features (Reference [1]). Calculation processing can be speeded up by 1 and 2. This is because the overhead during calculation (memory access / time required for function call) is reduced. See reference [1] for details.
[1] Kenji Nakaku, "Introduction to Python for Science and Technology Calculations", Gijutsu-Hyoronsha, 2016.