绘制虚拟列的数据帧的稀疏性

2024-05-08 17:53:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图可视化包含01列的数据帧的稀疏性(总共是0的90%和1的10%)

到目前为止,我已经尝试了以下方法:

sparse_df = df[binary_feat_cols].astype(pd.SparseDtype("int", 0))
sparse_df.sparse.density
plt.figure(figsize=(10, 6))
plt.spy(sparse_df, markersize=1);

sparse_matrix = scipy.sparse.csr_matrix(df[binary_feat_cols].to_numpy())
plt.figure(figsize=(10, 6))
plt.spy(sparse_matrix, markersize=1);

在这两种情况下,我都能看到图像。知道我做错了什么吗

enter image description here

编辑:我刚刚意识到这与矩阵的形状有关,因为我尝试了以下示例:

x = np.random.randn(910, 51)
x[15, :] = 0.
x[:, 40] = 0.
plt.figure(figsize=(10, 6))
plt.spy(x, markersize = 5)

我得到了这个新的情节: enter image description here

所以,问题应该是dataframe有900K行和50列,并且无法打印


1条回答
网友
1楼 · 发布于 2024-05-08 17:53:05

我在这里检查了matplotlib.pyplot.spy,看到了aspect参数:

aspect{'equal', 'auto', None} or float, default: 'equal' The aspect ratio of the Axes. This parameter is particularly relevant for images since it determines whether data pixels are square. This parameter is a shortcut for explicitly calling Axes.set_aspect. See there for further details. 'equal': Ensures an aspect ratio of 1. Pixels will be square. 'auto': The Axes is kept fixed and the aspect is adjusted so that the data fit in the Axes. In general, this will result in non-square pixels. None: Use rcParams["image.aspect"] (default: 'equal').

所以,做plt.spy(sparse_df, markersize=1, aspect = 'auto')解决了我的问题。谢谢

相关问题 更多 >

    热门问题