A double array can be treated as a "matrix" in mathematics. For example, if you write np.array ([list, list, list ...]) Double array data (ndarray type) is created.
Following mathematics, the hierarchy of multiple arrays is sometimes called a "dimension". A double array is a two-dimensional array, and so on.
The array of nparray is
shape
It has a member variable called.
nparray array.shape #When you write this, the number of elements in the array is recorded.
For example, if you have a double array that corresponds to a 2-by-3 matrix The ndarray array.shape has a value of (2, 3).
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
print(arr)
print(arr.shape)
#output
[[1 2 3]
[4 5 6]]
#As for the number of rows and columns, the number of rows is the number of lists and the number of columns is the number of elements as shown below.
(2, 3)
The shape of the array is
ndarray array.reshape()
You can change it with the method.
Using the two arguments a and b
ndarray array.reshape(a,b)
If you write, the array data corresponding to the matrix of a row and b column will be obtained as the return value.
At this time, the total number of elements and the argument of the reshape method must correspond. If you use -1 as an argument, it will be transformed by guessing the appropriate value from other arguments.
import numpy as np
arrA = np.array([1,2,3,4,5,6])
print(arrA)
arrB = arrA.reshape(2,3)
print(arrB)
arrC = arrA.reshape(-1,2)
print(arrC)
#output
# arrA
[1 2 3 4 5 6]
# arrB:Adjust the number of matrices from the argument
[[1 2 3]
[4 5 6]]
# arrC: -Adjust from the argument of 1 to the argument of the column 2
[[1 2]
[3 4]
[5 6]]
For a two-dimensional array, if you specify only one index, you can get any row in the array.
arr = np.array([[1, 2 ,3], [4, 5, 6]])
print(arr[1])
#Output result
[4 5 6]
You need to specify two indexes to get to the individual element or scalar value. That is, access like arr [1] [2] or arr [1, 2].
arr [1] [2] is accessing the third element of the array retrieved by arr [1] In arr [1, 2], the same element is accessed by specifying the axes of the two-dimensional array.
arr = np.array([[1, 2 ,3], [4, 5, 6]])
print(arr[1][2])
print(arr[1,2])
#Output result
6
6
It is also possible to use slices when referencing a two-dimensional array.
arr = np.array([[1, 2 ,3], [4, 5, 6]])
print(arr[1,1:])
#Output result
[5 6]
# :Use to retrieve the elements after and before the element.
axis
From a two-dimensional array, the concept of axis becomes important.
Axis is like a coordinate axis. There are many situations where you can set axis as an argument to a NumPy function.
In the case of a two-dimensional array, the axis is set as shown in the figure below. The axis that processes each column is axis = 0 The axis that processes line by line is axis = 1 It means that.
For example, consider the sum () method of an ndarray array. You can add elements together with ndarray.sum ().
mport numpy as np
arr = np.array([[1, 2 ,3], [4, 5, 6]])
print(arr.sum()) # sum()If nothing is specified in the argument of, the total value will be a scalar (integer, decimal, etc.)
print(arr.sum(axis=0)) # axis=If 0 is specified, vertical addition is performed and the elements become a one-dimensional array with three elements.
print(arr.sum(axis=1)) # axis=If you specify 1, you can see that the elements are added horizontally to form two one-dimensional arrays.
#Output result
21
[5 7 9]
[ 6 15]
# axis=Element at 0(By index number)
# axis=1 for each primary list
It is a method of using an array of indexes for index reference.
To extract rows from an ndarray array in a particular order Pass an array showing that order as an index reference.
[[]] #Use double brackets for fancy index references.
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
#Extract the elements in the 3rd, 2nd, and 0th rows in that order to create a new element.
#Index numbers start at 0.
print(arr[[3, 2, 0]])
#Output result
[[7 8]
[5 6]
[1 2]]
In a matrix, swapping rows and columns is called transposing. To transpose an ndarray array
np.transpose()
How to use this
.T
There are two ways to use
import numpy as np
arr = np.arange(10).reshape(2, 5)
#Transpose the variable arr and output
print(arr.T)
#Or
print(arr.transpose())
#output
[[0 5]
[1 6]
[2 7]
[3 8]
[4 9]]
There are several ways to sort, all of which have different uses.
sort() The ndarray array can be sorted by sort () as well as the list type.
For a two-dimensional array, taking 0 as an argument sorts the elements by column.
import numpy as np
arr = np.array([[15, 5, 200], [2, 100, 60], [100, 50, 8]])
print(arr)
arr.sort(axis=0) #It is specified in the column here.
print(arr)
#Output result
[[ 15 5 200]
[ 2 100 60]
[100 50 8]]
[[ 2 5 8]
[ 15 50 60]
[100 100 200]]
If 1 is taken as an argument, the elements will be sorted by row.
import numpy as np
arr = np.array([[15, 5, 200], [2, 100, 60], [100, 50, 8]])
print(arr)
arr.sort(1) #Specify 1 without axis and specify line by line
print(arr)
#Output result
[[ 15 5 200]
[ 2 100 60]
[100 50 8]]
[[ 5 15 200]
[ 2 60 100]
[ 8 50 100]]
np.sort()
You can also sort using np.sort (). However, please note that the usage is different.
The method of arr that saved the data
arr.sort() #Sort the data in arr.
on the other hand
np.sort(arr) #The arr remains the same, creating another array with the data sorted.
When using np.sort (), the sorted data cannot be used unless the newly created variable is assigned.
import numpy as np
arrA = np.array([[15, 5, 200], [2, 100, 60], [100, 50, 8]])
arrB = np.sort(arrA)
print(arrA) #Array before sorting
print(arrB) #Array after sorting
#sort method (arr.sort()If you write similar code using), the behavior will change completely.
import numpy as np
arrA = np.array([[15, 5, 200], [2, 100, 60], [100, 50, 8]])
arrB = arrA.sort()
print(arrA) #Array after sorting
print(arrB) # None
# np.By adding, a new array will be created.
When using np.sort (), the sorted data cannot be used unless the newly created variable is assigned.
NumPy is designed for handling large amounts of data, and uses a design that is unusual for a Python module, such as returning None to some methods.
Methods often used in machine learning
argsort() #There is also this.
The argsort () method returns the sorted "array index".
import numpy as np
arr = np.array([15, 30, 5])
arr.argsort()
#When executed, the following results will be obtained.
#Output result
[2 0 1]
If you do arr.sort (), it will be [5 15 30]. The element "5" that was in the "second" in the original array is the 0th The element "15" that was in the "0th" in the original array is the 1st The element "30" that was in the "first" in the original array becomes the second element.
So if you do [15, 30, 5] with .argsort () The value is returned as [2 0 1] because it becomes the "2nd, 0th, 1st" element in order.
For functions that perform matrix calculations
np.dot(a,b) #Returns the matrix product of two matrices
np.linalg.norm(a) #Returns the norm
Calculating the matrix product creates a new matrix whose elements are the inner product of the row and column vectors inside the matrix.
The norm is the one that returns the length of the vector The squared values of the elements are added together to cover the route.
A statistical function is a function that performs mathematical processing centered on the entire ndarray array or a specific axis. Or a method.
np.argmax() #Index number of the maximum value of the element
np.argmin() #Index number of the minimum value of the element
Moreover, these functions
np.average() #average
Except for this average
Apply to ndarray array
ndarray.mean()
ndarray.max()
There are also methods for the object itself, such as.
As a thing treated in statistics
np.std() #standard deviation
np.var() #Distributed
And so on.
np.sum()
#Now by specifying with axis
#As if we were able to decide which axis to focus on
np.mean()
mean()
#You can specify the axis in the same way with these.
np.argmax()
argmax()
np.argmin()
argmin()
#For these methods
#Returns the maximum or minimum index for each axis specified by axis.
When operating between ndarray arrays of different sizes A process called broadcast is performed automatically.
Broadcast is when operating between two ndarray arrays Automatically fits rows and columns of smaller arrays to larger arrays.
When the number of rows in the two arrays does not match Copy the missing lines from the existing lines to match the number of lines with the fewest lines.
If the number of columns does not match, the same process is performed. Not all arrays can be broadcast, Broadcasting is possible when all elements are processed in the same way as shown in the figure below.
x = np.arange(6).reshape(2, 3)
print(x + 1)
#Output result
[[1 2 3]
[4 5 6]]
Recommended Posts