Python Forum
How to only extract upper or lower triangular matrix into tabular form
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)
Reply
#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)
Reply
#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
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Export data from PDF as tabular format zinho 5 647 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to apply function lower() to the list? Toltimtixma 2 758 Feb-10-2023, 05:15 PM
Last Post: Toltimtixma
  Replace with upper(string) WJSwan 7 1,546 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  Beginner Higher Lower Game wallytan 2 1,541 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,722 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  Build a matrix by pressing buttons of an interface in Tkinter which extract data from juandiegopulla 1 1,897 Sep-13-2021, 07:28 PM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,304 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How can I add string.upper() noahneature 2 1,834 Oct-11-2020, 06:41 PM
Last Post: noahneature
  lower() applied at in operator Beerforfree 3 2,260 Mar-22-2020, 11:51 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020