Created
December 20, 2021 03:03
-
-
Save jcheong0428/c2c9cfe26e71e0f63b90d6ea96422c85 to your computer and use it in GitHub Desktop.
sim_mat2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upper(df): | |
'''Returns the upper triangle of a correlation matrix. | |
You can use scipy.spatial.distance.squareform to recreate matrix from upper triangle. | |
Args: | |
df: pandas or numpy correlation matrix | |
Returns: | |
list of values from upper triangle | |
''' | |
try: | |
assert(type(df)==np.ndarray) | |
except: | |
if type(df)==pd.DataFrame: | |
df = df.values | |
else: | |
raise TypeError('Must be np.ndarray or pd.DataFrame') | |
mask = np.triu_indices(df.shape[0], k=1) | |
return df[mask] | |
# Compare that the upper triangle is properly extracted. | |
print("Extracted upper triangle from m1") | |
print(np.round(upper(m1)[:25],3)) | |
print("Top 3 rows from m1") | |
display(m1.iloc[:3, :].round(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment