NumPy에서 행 수 얻기

Muhammad Maisam Abbas 2021년7월4일
NumPy에서 행 수 얻기

이 튜토리얼에서는 NumPy 배열의 행 수를 얻는 방법을 소개합니다.

array.shape속성을 사용하여 NumPy 배열의 행 수 얻기

NumPy 배열의 array.shape속성은 배열의 모양을 가져옵니다. 배열의 모양은 배열의 각 차원에있는 요소 수를 나타냅니다. 다음 코드 예제는array.shape속성을 사용하여 2D NumPy 배열의 행 수와 열 수를 가져 오는 방법을 보여줍니다.

import numpy as np

array = np.array([[1, 3, 5], [2, 4, 6]])

rows, columns = array.shape

print("Rows = ", rows)
print("Columns = ", columns)

출력:

Rows =  2
Columns =  3

먼저np.array()함수를 사용하여 2D NumPy 배열을 만들었습니다. 그런 다음array.shape속성을 사용하여 배열의 행과 열 수를 결정하고 결과를rowscolumns변수에 저장했습니다. 마지막으로rowscolumns에 값을 인쇄했습니다.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn