728x90

Shape (모양)란? 

Numpy 다차원 배열의 사용할 때 표현/제어하는 개념을 의미한다.

 

배열의 모양 변화시키는 예제 ( numpy.reshape example )

# [0 1 2 3 4 5] 배열을 3x2 배열로 변환.
a = np.arange(6).reshape((3, 2))
a

''' output
array([[0, 1],
       [2, 3],
       [4, 5]])
'''

 

다음은 1차원의 numpy.array을 reshape메소를 통해서 2 x 2 x 3 모양의 다차원 배열로 변환하는 예제입니다.

import numpy as np

temperatures = np.array([
29.3, 42.1, 18.8, 16.1, 38.0, 12.5,
12.6, 49.9, 38.6, 31.3, 9.2, 22.2
]).reshape(2, 2, 3)

print(f"size : {temperatures.shape}")

temperatures

'''out 
size : (2, 2, 3)
array([[[29.3, 42.1, 18.8],
        [16.1, 38. , 12.5]],

       [[12.6, 49.9, 38.6],
        [31.3,  9.2, 22.2]]])
'''

numpy.reshape(a, newshape, order='C') api 

더보기

numpy.reshape(a, newshape, order='C')

Gives a new shape to an array without changing its data.

 

Parameters :

a array_like

Array to be reshaped.

 

newshape : int or tuple of ints

The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

 

order {‘C’, ‘F’, ‘A’}, optional

Read the elements of a using this index order, and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. ‘A’ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.

 

Returns :

reshaped_array : ndarray

This will be a new view object if possible; otherwise, it will be a copy. Note there is no guarantee of the memory layout (C- or Fortran- contiguous) of the returned array.

 

reference : 

https://numpy.org/doc/stable/reference/generated/numpy.reshape.html

 

 

다음은 두 축(axis)을 서로 교환(interchage)하는 예정입니다. 

swap_temperatures = np.swapaxes(temperatures, 1, 2)
print(f"size : {swap_temperatures.shape}")
swap_temperatures

''' output
size : (2, 3, 2)
array([[[29.3, 16.1],
        [42.1, 38. ],
        [18.8, 12.5]],

       [[12.6, 31.3],
        [49.9,  9.2],
        [38.6, 22.2]]])
        
'''

 

numpy.swapaxes(a, axis1, axis2) api

더보기

numpy.swapaxes(a, axis1, axis2)

Interchange two axes of an array.

 

Parameters :

a : array_like

Input array.

axis1 : int

First axis.

axis2 : int

Second axis.

 

Returns:

a_swapped : ndarray

For NumPy >= 1.10.0, if a is an ndarray, then a view of a is returned; otherwise a new array is created. For earlier NumPy versions a view of a is returned only if the order of the axes is changed, otherwise the input array is returned.

 

Reference : 

https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html#numpy.swapaxes


<NumPy Tutorial>

[Python] NumPy Tutorial #1 : Introduce & Vectorization & Boadcasting


https://unsplash.com/photos/LqKhnDzSF-8

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기
반응형