[PYTHON] How to increase the axis

pytorch Use ʻunsqueeze ()`.

a = torch.rand((3, 3))
a.size() # -> [3, 3]

a = a.unsqueeze(0)
a.size() # -> [1, 3, 3]

a = a.unsqueeze(1)
a.size() # -> [3, 1, 3]

numpy There is a way to use reshape, newaxis, ʻexpand_dims. If you use reshape or newaxis, you can increase more than one at the same time. Reshape is annoying, so I wonder if it's a new axis`.

a = np.random.normal(size=(3,3))
a.shape # -> [3, 3]

# reshape
b = a.reshape(1, 3, 3)
b.shape # -> [1, 3, 3]

c = a.reshape(3, 1, 3)
c.shape # -> [3, 1, 3]

d = a.reshape(1, *a.shape)
d.shape # -> [1, 3, 3]

# newaxis
b = a[np.newaxis]
b.shape # -> [1, 3, 3]

c = a[:, np.newaxis]
c.shape # -> [3, 1, 3]

# expand_dims
b = np.expand_dims(a, 0)
b.shape # -> [1, 3, 3]

c = np.expand_dims(a, 1)
c.shape # -> [3, 1, 3]

Recommended Posts

How to increase the axis
How to use the generator
How to use the decorator
How to start the program
How to calculate the autocorrelation coefficient
How to use the zip function
How to use the optparse module
How to read the SNLI dataset
How to get the Python version
[Python] How to import the library
How to overwrite the output to the console
How to use the ConfigParser module
How to increase the processing speed of vertex position acquisition
How to increase the number of machine learning dataset images
How to display the progress bar (tqdm)
How to use the Spark ML pipeline
How to check the version of Django
How to solve the bin packing problem
How to set the server time to Japanese time
How to manually update the AMP cache
[Linux] How to use the echo command
How to use the Linux grep command
How to get colored output to the console
How to operate Linux from the console
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to assign multiple values to the Matplotlib colorbar
How to calculate the volatility of a brand
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to find the area of the Voronoi diagram
How to use MkDocs for the first time
How to specify the NIC to scan with amazon-dash
How to draw a 2-axis graph with pyplot
[Python] How to change the date format (display format)
How to try the friends-of-friends algorithm with pyfof
How to print debug messages to the Django console
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to get the files in the [Python] folder
How to identify the system call number ausyscall
How to Learn Kaldi with the JUST Corpus
How to find the correlation for categorical variables
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header