파이썬[Python]: numpy - ones_like 함수

numpy 모듈 - ones_like 함수(function)


/// 설명

주어진 배열의 차원과 데이터 형식을 가진 새로운 배열을 생성합니다.

prototype: 결과물에 차원과 데이터 형식을 부여하는 prototype입니다.
dtype: 출력할 배열의 데이터 형식입니다.
order: 결과물의 메모리 저장 형식을 결정합니다. 'C'는 C언어 형식, 'F'는 포트란 형식입니다. 'A'는 prototype이 포트란 연속(Fortran contiguous)이면 'F', 아니면 'C'로 결정됩니다. 'K'는 prototype의 차원(layout)에 가장 부합하는 형태로 결정되어집니다.
subok: True이면 생성되는 배열이 prototype의 sub-class 형식을 사용합니다. 그렇지 않으면 base-class를 사용합니다. 기본값은 True 입니다.
shape: 결과물의 차원을 결정합니다. 만약 저장형식(order)이 'K'이고 차원이 변경되지 않았다면, 저장형식은 변하지 않지만, 그렇지 않다면 C언어 형식(order='C')이 적용됩니다.

※ 형식
numpy.ones_like(prototype, dtype=None, order='K', subok=True, shape=None)

reference
https://numpy.org/devdocs/reference/generated/numpy.ones_like.html

/// 예제 array-like

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
 
array = ((12), (34))
print(array)
# ((1, 2), (3, 4))
 
test_np = np.ones_like(array)
print(test_np)
# [[1 1]
#  [1 1]]
 
cs

/// 예제 numpy array

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
 
np_array = [[12], [34]]
print(np_array)
# [[1, 2], [3, 4]]
 
test_np = np.ones_like(np_array, dtype=float)
print(test_np)
# [[1. 1.]
#  [1. 1.]]
 
cs

/// 예제 numpy array - arange

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np
 
np_array1 = np.arange(4)
print(np_array1)
# [0 1 2 3]
 
np_array1 = np_array1.reshape((22))
print(np_array1)
# [[0 1]
#  [2 3]]
 
test_np = np.ones_like(np_array1, dtype=int, order='F')
print(test_np)
# [[1 1]
#  [1 1]]
 
 
def np_with_comas(object):
    return np.array2string(object, separator=', ')
 
 
test_np = np.ones_like(((22), (22)), shape=4)
print(np_with_comas(test_np))
# [1, 1, 1, 1]
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수