Home > Backend Development > Python Tutorial > How to increase the dimension of numpy

How to increase the dimension of numpy

DDD
Release: 2023-11-22 11:48:30
Original
2372 people have browsed it

Numpy method to add dimensions: 1. Use "np.newaxis" to add dimensions. "np.newaxis" is a special index value used to insert a new dimension at a specified position. You can pass it in the corresponding Use np.newaxis to increase the dimension; 2. Use "np.expand_dims()" to increase the dimension. The "np.expand_dims()" function can insert a new dimension at the specified position to increase the dimension of the array

How to increase the dimension of numpy

Operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.

In NumPy, you can use the np.newaxis or np.expand_dims() function to increase the dimensions of an array. Both methods are described in detail below.

Use np.newaxis to add dimensions:

np.newaxis is a special index value used to insert a new dimension at a specified position. Dimensions can be increased by using np.newaxis at the corresponding location.

import numpy as np
# 创建一个一维数组
a = np.array([1, 2, 3, 4, 5])
print(a.shape)  # 输出 (5,)
# 在a的行维度上增加一个新维度
a_newaxis = a[np.newaxis, :]
print(a_newaxis.shape)  # 输出 (1, 5)
# 在a的列维度上增加一个新维度
a_newaxis = a[:, np.newaxis]
print(a_newaxis.shape)  # 输出 (5, 1)
Copy after login

In the above code, np.newaxis is used to add a new dimension to the row dimension and column dimension of the original array a.

Use np.expand_dims() to increase the dimension:

The np.expand_dims() function can insert a new dimension at the specified position to increase the dimension of the array. .

import numpy as np
# 创建一个二维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b.shape)  # 输出 (2, 3)
# 在b的行维度上增加一个新维度
b_expand_dims = np.expand_dims(b, axis=0)
print(b_expand_dims.shape)  # 输出 (1, 2, 3)
# 在b的列维度上增加一个新维度
b_expand_dims = np.expand_dims(b, axis=1)
print(b_expand_dims.shape)  # 输出 (2, 1, 3)
Copy after login

In the above code, the np.expand_dims() function is used to add a new dimension to the row dimension and column dimension of the original array b, and the axis parameter is used to specify in which dimension the new dimension is to be added.

Whether you use np.newaxis or np.expand_dims(), they are tool functions used to increase the dimensions of an array. You can choose which method to use to achieve the purpose of increasing dimensions according to specific needs.

The above is the detailed content of How to increase the dimension of numpy. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template