Berechnen Sie die Pearson -Korrelation jeder Reihen in 2D -Numpy -Array (n, m)Python

Python-Programme
Anonymous
 Berechnen Sie die Pearson -Korrelation jeder Reihen in 2D -Numpy -Array (n, m)

Post by Anonymous »

Code: Select all

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

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

a = array([[1, 2, 4],
[3, 6, 2],
[3, 4, 7],
[9, 7, 7],
[6, 3, 1],
[3, 5, 9]])

b = array([[4, 5, 2],
[9, 2, 5],
[1, 5, 6],
[4, 5, 6],
[1, 2, 6],
[6, 4, 3]])
Ich möchte den Pearson -Korrelationskoeffizienten zwischen dem zuerst row von a und Erste Reihe von b berechnen. zu folgen.

Code: Select all

array([__ , __ , __)
< /code>
Spalte In Bezugcorr = np.corrcoef(a.T, b.T).diagonal(a.shape[1])
< /code>
Ausgabe: < /p>
array([-0.2324843 , -0.03631365, -0.18057878])
update
Obwohl ich die folgende Antwort akzeptiert habe, gibt es diese alternative Lösung für die Frage und befasst

Code: Select all

def corr2_coeff(A, B):
# Rowwise mean of input arrays & subtract from input arrays themeselves
A_mA = A - A.mean(1)[:, None]
B_mB = B - B.mean(1)[:, None]

# Sum of squares across rows
ssA = (A_mA**2).sum(1)
ssB = (B_mB**2).sum(1)

deno = np.sqrt(np.dot(ssA[:, None],ssB[None])) + 0.00000000000001

# Finally get corr coeff
return np.dot(A_mA, B_mB.T) / deno

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post