Intuitively learn the reshape of Python np

background

I somehow didn't understand the reshape of np. However, when I wrote the code, I somehow understood it, so I wanted to share it for those who want to study intuitively.

Sample code

np_reshape_.py



#1 row 12 columns
list_1 = [10.0, 14.0, 23.0, 27.0, 37.0, 58.0, 81.0, 82.0, 135.0, 169.0, 344.0, 319.0]
print(list_1)
#[10.0, 14.0, 23.0, 27.0, 37.0, 58.0, 81.0, 82.0, 135.0, 169.0, 344.0, 319.0]

#3 rows 4 columns
list_2 = np.array(list_1).reshape(3, 4)
print(list_2)
"""
[[ 10.  14.  23.  27.]
 [ 37.  58.  81.  82.]
 [135. 169. 344. 319.]]
"""

#6 rows 2 columns
list_3 = np.array(list_1).reshape(6, 2)
print(list_3)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

#6 rows, 2 columns, 2 columns, coordinate system
list_4 = np.array(list_1).reshape(-1, 2)
print(list_4)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

#6 rows 2 columns
list_5 = np.array(list_1).reshape(6, -1)
print(list_5)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

Reflection

I intuitively understood how to use -1 of reshape of np.

Recommended Posts

Intuitively learn the reshape of Python np
Learn the basics of Python ① Beginners
the zen of Python
Towards the retirement of Python2
Learn Nim with Python (from the beginning of the year).
About the ease of Python
Learn the design pattern "Chain of Responsibility" in Python
About the features of Python
The Power of Pandas: Python
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
[python] -1 meaning of numpy's reshape method
About the basics list of Python basics
Learn the design pattern "Prototype" in Python
Learn the design pattern "Builder" in Python
Change the length of Python csv strings
Check the behavior of destructor in Python
Learn the basics of Theano once again
[Python3] Understand the basics of Beautiful Soup
Learn the design pattern "Flyweight" in Python
Learn the design pattern "Observer" in Python
Learn the design pattern "Memento" in Python
Learn the basics while touching python Variables
Learn the design pattern "Proxy" in Python
Pass the path of the imported python module
Learn the design pattern "Command" in Python
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
Learn the design pattern "Visitor" in Python
[Python] Understand the content of error messages
Learn the design pattern "Bridge" in Python
Learn the design pattern "Mediator" in Python
I didn't know the basics of Python
Learn the design pattern "Decorator" in Python
The result of installing python in Anaconda
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
The story of manipulating python global variables
Learn the design pattern "Iterator" in Python
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
The Python project template I think of.
Learn the design pattern "Strategy" in Python
Learn the design pattern "Composite" in Python
[Linux] Learn the basics of shell commands
Python Basic Course (at the end of 15)
[Python] Get the character code of the file
Learn the design pattern "State" in Python
The story of blackjack A processing (python)
Python Note: The secret role of commas
Learn the design pattern "Adapter" in Python