Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to only extract upper or lower triangular matrix into tabular form
#1
Hi,

I have correlation matrix as below

my raw data is stored in variable "x"

x:
A B C D
1.2 2.3 5.6 1.0
1.3 5.6 2.3 2.9
5.6 0.7 3.9 2.8
1.3 1.9 2.8 0.8
3.3 0.9 2.8 1.3
4.3 0.6 1.3 2.8
7.3 2.8 0.05 2.8
2.3 2.8 1.03 0.6

import seaborn as sns
f,ax=plt.subplots(figsize=(10,10))
corr=x.corr()
sns.heatmap(corr,mask=np.zeros_like(corr,dtype=np.bool),cmap=sns.diverging_palette(220,10,as_cmap=True),square=True,ax,ax)

a=x.corr()
b=x.corr()
corrFilter=x.corr()
s=corrFilter.unstack()
so=s.sort_values(kind="quicksort")


I want to remove diagonal, and only extract upper or lower triangular matrix. I use the below code, but it still retain for example B-->A (But it same as A-->B).
How to remove duplicate in reverse order(meaning, just retain A-->B, and omit B-->A)
Quote
#2
Is x assumed to be a data frame (pandas)?
If you want to extract upper/lower triangle of a matrix, you can use numpy.triu and numpy.tril utility functions, e.g.:
import pandas as pd

# a piece of code where x becomes defined

pd.np.triu(x.corr().values)
pd.np.tril(x.corr().values)
Gribouillis likes this post
Quote
#3
x is my input data, and after correlation matrix, want to extract upper or lower triangle matrix into tabular form. Here 4 variable,

I want to upper (or lower triangle) matrix:

Column1  Column2  Corr.coeficient
A         B        0.4
A         C        0.8
A         D        0.5
B         C        0.5
B         D        0.8
C         D        0.6

Quote
#4
x.corr().where(pd.np.triu(pd.np.ones(x.shape), k=1).astype(bool)).stack().reset_index()
You can change k to 0 to include the main diagonal of the correlation matrix.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to change a dataframe column to lower case zhujp98 5 297 Oct-29-2019, 06:42 PM
Last Post: zhujp98
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 106 Oct-21-2019, 01:01 PM
Last Post: enyrb
  How can one generate triangular and sawtooth waves in python? xBlackHeartx 6 818 Sep-25-2019, 11:52 PM
Last Post: metulburr
  Upper-Bound Exclusive Meaning Johnny1998 1 337 Aug-02-2019, 08:32 PM
Last Post: ichabod801
  Retrieve protocol in lower case Variables 1 279 May-02-2019, 04:46 AM
Last Post: Larz60+
  why is user = form.getfirst("user", "").upper() safe? shanepy 6 852 Nov-27-2018, 07:13 PM
Last Post: buran
  How to extract specific numbers from a matrix? Alberto 0 715 Mar-06-2018, 02:52 PM
Last Post: Alberto
  change only one element of list to upper case python1980 3 3,694 Dec-03-2017, 07:23 PM
Last Post: python1980
  How do I loop through a list and delete numerical elements that are 1 lower/higher? neko 4 1,420 Sep-05-2017, 02:25 PM
Last Post: ichabod801
  matrix from matrix python numpy array shei7141 1 1,384 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)