반응형
파이썬에서 두 벡터의 상관 관계를 얻는 방법
이 질문에 이미 답변이 있습니다.
MATLAB에서는
a=[1,4,6]
b=[1,2,3]
corr(a,b)
.9934를 반환합니다. 나는 시도 numpy.correlate
했지만 완전히 다른 것을 반환합니다. 두 벡터의 상관 관계를 얻는 가장 간단한 방법은 무엇입니까?
문서는 numpy.correlate
귀하가 찾고있는 것이 아님을 나타냅니다 .
numpy.correlate(a, v, mode='valid', old_behavior=False)[source]
Cross-correlation of two 1-dimensional sequences.
This function computes the correlation as generally defined in signal processing texts:
z[k] = sum_n a[n] * conj(v[n+k])
with a and v sequences being zero-padded where necessary and conj being the conjugate.
대신 다른 의견이 제안했듯이 Pearson 상관 계수를 찾고 있습니다. scipy로이 작업을 수행하려면 :
from scipy.stats.stats import pearsonr
a = [1,4,6]
b = [1,2,3]
print pearsonr(a,b)
이것은 준다
(0.99339926779878274, 0.073186395040328034)
다음을 사용할 수도 있습니다 numpy.corrcoef
.
import numpy
print numpy.corrcoef(a,b)
이것은 다음을 제공합니다.
[[ 1. 0.99339927]
[ 0.99339927 1. ]]
참고 URL : https://stackoverflow.com/questions/19428029/how-to-get-correlation-of-two-vectors-in-python
반응형
'programing' 카테고리의 다른 글
파이썬 변수 포인터입니까? (0) | 2020.11.20 |
---|---|
파이썬 변수의 메모리 주소 인쇄 (0) | 2020.11.20 |
Android에서 java.lang.OutOfMemoryError 문제를 해결하는 방법 (0) | 2020.11.20 |
Android Studio : 등록되지 않은 VCS 루트 감지 (0) | 2020.11.20 |
Angular2의 열거 형을 기반으로 선택 (0) | 2020.11.20 |