Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对数据排序后,标签文档对在绘图中出现错误_Python_Python 3.x_Sorting_Matplotlib_Numpy Ndarray - Fatal编程技术网

Python 对数据排序后,标签文档对在绘图中出现错误

Python 对数据排序后,标签文档对在绘图中出现错误,python,python-3.x,sorting,matplotlib,numpy-ndarray,Python,Python 3.x,Sorting,Matplotlib,Numpy Ndarray,我有一个文档列表,我将其转换为稀疏矩阵 tfidf_vectorizer = TfidfVectorizer() sparse_matrix = tfidf_vectorizer.fit_transform(documents) 经过进一步的处理后,我对矩阵进行排序并绘制 问题是,排序后,我无法跟踪绘图的文档标签对。 我怎样才能解决这个问题 部分守则 path = "C:\\Users\\user\\Desktop\\texts\\dataset" text_files

我有一个
文档列表
,我将其转换为
稀疏矩阵

tfidf_vectorizer = TfidfVectorizer()

sparse_matrix = tfidf_vectorizer.fit_transform(documents)
经过进一步的处理后,我对矩阵进行排序并绘制

问题是,排序后,我无法跟踪绘图的文档标签对。 我怎样才能解决这个问题

部分守则

path = "C:\\Users\\user\\Desktop\\texts\\dataset"
text_files = os.listdir(path)

documents = [open(f, encoding="utf-8").read() for f in text_files if f.endswith('.txt')]
tfidf_vectorizer = TfidfVectorizer()
sparse_matrix = tfidf_vectorizer.fit_transform(documents)

labels = []
for f in text_files:
    if f.endswith('.txt'):
        labels.append(f)

fig, ax = plt.subplots(figsize=(20,20))
cax = ax.matshow(sim_mat_sorted, interpolation='spline16')
ax.grid(True)
plt.title('News articles similarity matrix')
plt.xticks(range(23), labels, rotation=90);
plt.yticks(range(23), labels);
fig.colorbar(cax, ticks=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
plt.show()