2017-05-20 1 views
-1

내가. 모든 상관 관계

방법 대신 내가 가진 모든 상관 관계의 3 가장 긍정적 인 상관 관계를 보여주기 위해 상관 관계 매트릭스 상관 행렬이 보이는 데이터 집합을 가지고 + 상관 관계 중 유일한 긍정적 인 상관 관계를 보여 숫자로 표시되어야 함)

답변

0

아래 그림과 같이 관심이없는 모든 값은 마스크 할 수 있습니다.

# Set the diagonal to -np.inf 
corr[np.diag_indices_from(corr)] = -np.inf 
# Find the value of the k-largest correlation 
k = 3 
threshold = np.sort(corr)[-k] 
# Mask all values that are below the threshold 
corr[corr < threshold] = np.nan 
# Do your plotting as before 
0

데모 :

In [156]: df = pd.DataFrame(np.random.randint(1, 6, size=(5, 5))).add_prefix('col').corr() 

In [157]: df 
Out[157]: 
      col0  col1  col2  col3  col4 
col0 1.000000 0.000000 0.060193 -0.722222 -0.218218 
col1 0.000000 1.000000 -0.233126 -0.215166 0.845154 
col2 0.060193 -0.233126 1.000000 0.541736 0.118217 
col3 -0.722222 -0.215166 0.541736 1.000000 0.036370 
col4 -0.218218 0.845154 0.118217 0.036370 1.000000 

In [158]: corr = df.values 

In [159]: corr[np.tril_indices_from(corr)] = np.nan 

In [160]: x = pd.DataFrame(corr, columns=df.columns, index=df.index) 

In [161]: x.stack(dropna=False).nlargest(3).unstack() 
Out[161]: 
      col3  col4 
col1  NaN 0.845154 
col2 0.541736 0.118217 

In [162]: sns.heatmap(x.stack(dropna=False).nlargest(3).unstack()) 
Out[162]: <matplotlib.axes._subplots.AxesSubplot at 0xcacf7b8> 

enter image description here