修改python代码

with open('train_semantic.txt','w',encoding='utf-8') as f:
    for i in range(len(X_train_counts)):
        str1 = str(X_train_counts[i])+"\t"+"__label__"+str(y_train[i])+'\n'
        f.write(str1)

with open('test_semantic.txt','w',encoding='utf-8') as f:
    for i in range(len(X_test_counts)):
        str1 = str(X_test_counts[i])+"\t"+"__label__"+str(y_test[i])+'\n'
        f.write(str1)
 

这段代码的意义是为了把处理后的数据写成fasttext形式,但是我写完存为txt形式后,发现里面没有之前的word,只有数字了。请大神们帮忙修改一下。谢谢

w改a或a+

我试了你的方法,还是不行。我把上面的数据处理跟您描述一下:

我用了高斯混合函数,对数据进行了聚类,然后形成了一个COOrdinate format的形式。我不知道是不是我哪里没有处理好的缘故。

我的高斯混合模型代码是:

from sklearn.mixture import GaussianMixture
from scipy import sparse as sp
X_train = sp.rand(10000, 10000, density=0.1)
gm = GaussianMixture(n_components=3)
gm.fit(X_train.todense())
probs = gm.predict_proba(X_train.todense())
print(probs)
[[1. 0. 0.]
 [0. 0. 1.]
 [0. 0. 1.]
 ...
 [0. 0. 1.]
 [1. 0. 0.]
 [0. 0. 1.]]

这个会是导致出现数字的问题吗?

虽然没有解决问题,但是感谢你的帮助!