python - dask 中 np.fill_diagonal 的等价物是什么
问题描述
我有一个相关矩阵,我使用 np.fill_diagonal 用 1 填充对角线,然后使用 np.triu 取上三角形。但是对于相关矩阵,我使用的是 dataframe.corr,它会为超过 5000 的列数提供内存错误。因此,我正在创建一个 dask 数据帧,然后使用 corr 来计算相关矩阵。
但是,我无法在对角线上复制 fill_diagonal 和 np.triu。有人可以帮我吗?
下面是样本数据和预期输出的快照。我还放了当前使用的python代码来实现预期的输出。我的目标是在 dask 中执行这些操作,以避免大型数据帧(500 万条记录和 5800 多列)的内存错误并计算结果。此代码是从数据中删除多重共线变量的更大函数的一部分,并且正在 dask 中复制。
input
h_id cu_id tax rev_m1 io_m1
0 0 0 0 0
0 0 0 1 0
0 0 1 0 -1
-1 1 0 0 1
1 0 0 0 -1
0 0 0 0 0
0 0 0 0 -1
1 0 0 -1 0
-1 1 0 0 0
0 0 0 0 -1
python code to acheive the desired output:
column_names = data.columns
#Create covariance matrix
correlation_matrix = data.corr()
print('*'*10 + "\nDone creating correlation matrix of standardized X.")
print(datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S'))
#Save the Covariance Matrix to a CSV
df_correlation_matrix = pd.DataFrame(correlation_matrix, columns = column_names, index = column_names)
np.fill_diagonal(df_correlation_matrix.values, 1)
# Writing values from upper triangle of the correlation matrix to new data frame
df1 = df_correlation_matrix.where(np.triu(np.ones(df_correlation_matrix.shape)).astype(bool)).stack().reset_index()
df1.columns = ['Variable1','Variable2','Value']
df1.drop(df1[df1.Variable1 == df1.Variable2].index, inplace=True)
Expected output:
Variable1 Variable2 Value
h_id cu_id -0.79
h_id tax 0.0
h_id rev_m1 -0.35
h_id io_m1 -0.49
cu_id tax -0.16
cu_id rev_m1 0.0
cu_id io_m1 0.62
tax rev_m1 0.0
tax io_m1 -0.36
rev_m1 io_m1 0.0
解决方案
推荐阅读
- php - 带有上传文件附件的表格
- javascript - 制表符不适用于远程分页和 ajaxURLGenerator
- python - 从 DataFrames 列表中创建一个具有最佳值的 DataFrame
- c++ - 矩阵构造函数的大括号封闭初始化程序在 std::complex 类型中失败
> - angular - 让 Angular 将 TSLint 迁移到 ESLint Banana-In-Box 错误
- c++ - 计算1DPlot,确定最大值和它们之间的距离
- c++ - 使 gcc 跳过特定 .cpp 文件的“-Werror=unused-parameter”
- javascript - 防止用户在表单提交后返回 - PHP
- hololens - 如何在 Hololens 2 上强制卸载应用程序
- vue.js - vue 的 src 文件夹中缺少 store 文件夹