首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'scipy.sparse.csr.csr_matrix‘的散点图

'scipy.sparse.csr.csr_matrix‘的散点图
EN

Stack Overflow用户
提问于 2020-05-20 23:52:02
回答 1查看 393关注 0票数 0

我有一个来自TfidfVectorizer的矩阵,那么能不能画出一个图来呢?

(0,6164) 0.019338613120625153

(0,8791) 0.030431754891299245

(0,13418) 0.019338613120625153

(0,12251) 0.040303890966260525

(0,2896) 0.017899943021264794

(0,2172) 0.12091167289878157

(0,3413) 0.040303890966260525

(0,6571) 0.014970123315314715

(0,13039) 0.015495789594635422

(0,11488) 0.03593540116095009

(0,12423) 0.030431754891299245

(0,11803) 0.017899943021264794

(0,14555) 0.017899943021264794

EN

回答 1

Stack Overflow用户

发布于 2020-05-21 01:06:13

可以按如下方式使用plt.scatter

代码语言:javascript
复制
import matplotlib.pyplot as plt
from scipy.sparse import csr_matrix
import numpy as np

# create a 15000x15000 sparse matrix with some toy data
N = 15000
npnts = 1000
# mtrx = csr_matrix(([y for (x1,x2),y in data], ([x1 for (x1,x2),y in data], [x2 for (x1,x2),y in data])), shape=(N, N))
mtrx = csr_matrix((np.random.uniform(0, 0.2, npnts),
                   (np.random.randint(0, N, npnts), np.random.randint(0, N, npnts))),
                  shape=(N, N))

# convert the dense matrix to dictionary format, get an array of xy-coordinates and an array of values
mtrx_dict = mtrx.todok()
xy = np.array(list(mtrx_dict.keys()))
vals = np.array(list(mtrx_dict.values()))

# create a scatter plot
plt.scatter(xy[:,0], xy[:,1], s=5, c=vals, cmap='inferno')
plt.colorbar()
plt.show()

根据数据的含义和您想要显示的内容,您可以尝试着色、alpha、大小等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61917404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档