我有一个来自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
发布于 2020-05-21 01:06:13
可以按如下方式使用plt.scatter:
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、大小等。
https://stackoverflow.com/questions/61917404
复制相似问题