반응형
ValueError: shapes (100,) and (1,100) not aligned: 100 (dim 0) != 1 (dim 0)¶
dot,cosine_similarity등 매트리스끼리 연산할때, 두 매트리스의shape가 연산규칙에 맞지 않다면ValueError가 발생하네요.
In [1]:
import numpy as np
(100) X (1,100)어레이는 에러가 산출되네요
In [2]:
a = np.random.rand(100)
b = np.random.rand(1, 100)
np.dot(a, b)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[2], line 3 1 a = np.random.rand(100) 2 b = np.random.rand(1, 100) ----> 3 np.dot(a, b) ValueError: shapes (100,) and (1,100) not aligned: 100 (dim 0) != 1 (dim 0)
(1, 100) X (1, 100)어레이도 에러를 반환하네요
In [3]:
a = np.random.rand(1, 100)
b = np.random.rand(1, 100)
np.dot(a, b)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 3 1 a = np.random.rand(1, 100) 2 b = np.random.rand(1, 100) ----> 3 np.dot(a, b) ValueError: shapes (1,100) and (1,100) not aligned: 100 (dim 1) != 1 (dim 0)
(100, 1) X (1,100)어레이는 계산이 됩니다.
In [4]:
# Example arrays
a = np.random.rand(100, 1) # This will create an array of shape (100,)
b = np.random.rand(1, 100) # This will create an array of shape (1, 100)
np.dot(a, b)
Out[4]:
array([[0.25148646, 0.25288578, 0.02490737, ..., 0.12144668, 0.31535732,
0.28806871],
[0.13723606, 0.13799967, 0.01359194, ..., 0.06627341, 0.17209036,
0.15719899],
[0.02842853, 0.02858672, 0.00281558, ..., 0.01372858, 0.03564862,
0.03256387],
...,
[0.60409463, 0.60745594, 0.0598299 , ..., 0.2917266 , 0.75751857,
0.69196873],
[0.38783055, 0.38998852, 0.03841097, ..., 0.18728934, 0.48632917,
0.44424598],
[0.21079122, 0.21196411, 0.02087689, ..., 0.10179432, 0.26432657,
0.24145378]])
- 구글링해보니 어레이 선언은 일괄적으로 하고 필요한 어레이에 transpose 해 계산해 주는게 대세인 것 같습니다.
In [5]:
a = np.random.rand(1, 100)
b = np.random.rand(1, 100)
np.dot(a.T, b)
Out[5]:
array([[0.10471007, 0.07425017, 0.20002182, ..., 0.14549293, 0.39086824,
0.41040463],
[0.02366827, 0.01678323, 0.04521217, ..., 0.03288667, 0.08835037,
0.09276631],
[0.12110494, 0.08587581, 0.23134003, ..., 0.16827333, 0.45206802,
0.47466331],
...,
[0.03313391, 0.02349533, 0.06329386, ..., 0.04603902, 0.1236843 ,
0.1298663 ],
[0.07990834, 0.0566632 , 0.15264445, ..., 0.11103132, 0.29828679,
0.31319577],
[0.19737529, 0.1399593 , 0.37703503, ..., 0.27424972, 0.7367747 ,
0.77360021]])
반응형
'python' 카테고리의 다른 글
| 블로그에서 사용하는 배너 파이썬으로 그리기 (0) | 2024.03.29 |
|---|---|
| 최대 마진 관련성(Maximal Marginal Relevance, MMR) (0) | 2024.03.26 |
| 파이썬으로 점점 늘어났다 줄어드는 숫자 랜덤하게 만들기 (0) | 2024.03.19 |
| SyntaxError: '(' was never closed (0) | 2024.03.07 |
| 파이썬 순서도 그리기 (0) | 2024.03.02 |
댓글