Since numpy's ndarray and Python's built-in list were messed up, I tried to summarize each dimension and size in my own way. This is a summary of beginners.
As a result of google, the major size acquisition method was to use the len () function, np.shape () function, and instance variable ndarray.shape.
In conclusion, I think it's cheap pie ** if you use ** np.shape (). Also, I think it is good to remember that the ** built-in list type realizes a two-dimensional array representation by using the list comprehension notation **.
How to get the size | ndarray type | Built-in list type |
---|---|---|
len() | 1D only | 1D only |
np.shape() | n dimensions | n dimensions |
ndarray.shape | n dimensions | - ※ |
Also, of course, the output representation did not change depending on the type and dimension.
dimension | ndarray type | Built-in list type |
---|---|---|
1D | (3, ) | (3, ) |
2D | (3, 4) | (3, 4) ※ |
The numpy.array () function seems to be a handy tool to easily create an ndarray.
Since it is a numpy.ndarray type, ** there is no ,
between numbers **.
import numpy as np
A_np = np.array([5, 2], dtype=int)
B_np = np.array([10, 9], dtype=int)
C_np = np.array([1, 11], dtype=int)
D_np = np.array([9, 1], dtype=int)
#Result of substitution
A_np : [5 2]
B_np : [10 9]
C_np : [ 1 11]
D_np : [9 1]
#Array size
A_np.shape : (2,)
B_np.shape : (2,)
C_np.shape : (2,)
D_np.shape : (2,)
Python built-in type
Since it is a built-in list format, **, `is inserted between numbers **.
A = [5, 2]
B = [10, 9]
C = [1, 11]
D = [9, 1]
#Result of substitution
A : [5, 2]
B : [10, 9]
C : [1, 11]
D : [9, 1]
#Size np.shape()Function version
np.shape(A) : (2,)
np.shape(B) : (2,)
np.shape(C) : (2,)
np.shape(D) : (2,)
#Size len()Function version
len(A) : 2
len(B) : 2
len(C) : 2
len(D) : 2
The 2D of the built-in list uses "list comprehension notation".
A = [[12, -8, 4], [0, 6, -10]
#Result of substitution
[[12, -8, 4], [0, 6, -10]
The output of the built-in variable len () has 2 elements in list.
#size
len(A) : 2
First, the built-in list type remains
A = [[12, -8, 4], [0, 6, -10]]
A :
[[12, -8, 4], [0, 6, -10]]
np.shape(A) : (2, 3)
Second, convert to ndarray type
Since the built-in list type is changed to dumpy.ndarray type, ,
between numbers disappear.
A = [[12, -8, 4], [0, 6, -10]
A_np = np.array(A)
A_np :
[[ 12 -8 4]
[ 0 6 -10]]
A_np.shape : (2, 3)
When you want to use a two-dimensional array with the built-in list type, it seems to be troublesome when the number of elements of each element is different. Specifically,
A = [[12, -8, 4], [0, 6, -10, 2]]
At this time, it is a built-in list type two-dimensional array with two list type elements inside.
The lengths of those elements are different.
The result at this time is
len(A) : 2
np.shape(A) : (2,)
Even if you convert it to numpy.ndarray
A_np = np.array(A)
A_np :
[[12, -8, 4] [0, 6, -10, 2]]
np.shape(A_np) : (2,)
The ,
between the numbers are not gone, and the elements are a list type ndarray type array (?).
I'm sorry if I said something strange. I'm not confident.
By the way, np.array ([[12, -8, 4] [0, 6, -10, 2]], dtype = int)
resulted in an error.
In other words, list comprehensions with different numbers of elements return the length of the list because the size cannot be measured as a matrix. Well, is it natural?
Recommended Posts