[PYTHON] Difference between numpy.ndarray and list (dimension, size)

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.

Summary

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 **.

Size acquisition function

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.

Notation of size

dimension ndarray type Built-in list type
1D (3, ) (3, )
2D (3, 4) (3, 4) ※

numpy.ndarray type

The numpy.array () function seems to be a handy tool to easily create an ndarray.

code

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]

Next, let's look at the size. Check with the instance variable ndarray.shape. A one-dimensional ndarray type array is represented by `(number of elements,)`.
#Array size
A_np.shape : (2,)
B_np.shape : (2,)
C_np.shape : (2,)
D_np.shape : (2,)

Built-in list type

Python built-in type

Code one-dimensional array

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]

#### result Similarly, let's look at the size next. Even in the built-in list, the array size is returned by numpy.shape (). There are two elements in a one-dimensional array.
#Size np.shape()Function version
np.shape(A) : (2,)
np.shape(B) : (2,)
np.shape(C) : (2,)
np.shape(D) : (2,)

In the case of one dimension, the size (= number of elements) can also be confirmed with the built-in variable len (). In the case of 2D, the size is not returned. It is just the number of elements in list.
#Size len()Function version
len(A) : 2
len(B) : 2
len(C) : 2
len(D) : 2

Code 2D array

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

There are two ways to find the 2D array size of the built-in list type. The first is to use the np.shape function. The second method is to convert to numpy.ndarray type and then use ndarray.shape.

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)

Caution

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

Difference between numpy.ndarray and list (dimension, size)
Difference between list () and [] in Python
Difference between append and + = in Python list
[Python Iroha] Difference between List and Tuple
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
List concatenation method in python, difference between list.extend () and “+” operator
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between SQLAlchemy filter () and filter_by ()
Memorandum (difference between csv.reader and csv.dictreader)
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Difference between sort and sorted (memorial)
Difference between python2 series and python3 series dict.keys ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
[Xg boost] Difference between softmax and softprob
difference between statements (statements) and expressions (expressions) in Python
[Django ORM] Difference between values () and only ()
Test method for size difference between groups
Difference between @classmethod and @staticmethod in Python
Difference between nonlocal and global in Python
Difference between linear regression, Ridge regression and Lasso regression
Difference between docker-compose env_file and .env file
[python] Difference between rand and randn output
speed difference between wsgi, Bottle and Flask
Difference between ls -l and cat command
Difference and compatibility verification between keras and tf.keras # 1
[Introduction to Python] What is the difference between a list and a tuple?
What is the difference between `pip` and` conda`?
Difference between using and import on shield language
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
list and sum
list and numpy
Differences and commonalities between dict, list, and tuple types
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?
Consideration of the difference between ROC curve and PR curve
The rough difference between Unicode and UTF-8 (and their friends)
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
Difference between Ruby and Python in terms of variables
What is the difference between usleep, nanosleep and clock_nanosleep?
Difference between Numpy (n,) and (n, 1) notation [Difference between horizontal vector and vertical vector]
Difference between return, return None, and no return description in Python
How to use argparse and the difference between optparse
Between parametric and nonparametric
What is the difference between a symbolic link and a hard link?
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
Difference between SQLAlchemy back_populates and backref and when neither is used
The difference between foreground and background processes understood by the principle