>  기사  >  백엔드 개발  >  1D NumPy 배열을 전치하면 어떻게 되나요?

1D NumPy 배열을 전치하면 어떻게 되나요?

Mary-Kate Olsen
Mary-Kate Olsen원래의
2024-11-15 04:22:02135검색

What happens to a 1D NumPy array when you transpose it?

Transpose of a 1D NumPy Array

When working with NumPy arrays, it's important to understand the effects of transposition. Typically, the transpose of an array exchanges its rows and columns, resulting in a new array with swapped dimensions. However, in the case of a 1D array, the transpose operation has a different impact.

Consider the following Python snippet:

import numpy as np
a = np.array([5,4])
print(a)
print(a.T)

Instead of transposing the array, it remains unchanged. This is because the transpose of a 1D array is inherently a 1D array. Unlike in MATLAB, where "1D" arrays are effectively 2D, NumPy treats 1D arrays distinctly.

If you require a transposed 2D representation of your 1D vector, you can achieve it by slicing the vector using np.newaxis:

import numpy as np
a = np.array([5,4])[np.newaxis]
print(a)
print(a.T)

Now, the a.T operation will produce a transposed 2D array.

It's worth noting that adding an extra dimension to a 1D vector is not always necessary. In most cases, NumPy automatically broadcasts 1D arrays for appropriate calculations, eliminating the need to explicitly distinguish between row and column vectors.

위 내용은 1D NumPy 배열을 전치하면 어떻게 되나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.