我目前正在尝试寻找一种方法来按行随机分配数据帧中的项目。我想保留列名以及索引。我只想更改数据框中的条目顺序。
当前,我正在使用
data = data.sample(frac=1).reset_index(drop=True)
但是,这在输出方面造成了一些问题。我不认为这些行被正确地洗了。还有另一种方法可以实现这一目标吗?
问题是,我正在进行文本分析,当我与每个班级查看最相关的单字组和二元组时,对于随机和原始数据我会得到不同的答案。
这是我用于字母组合和双字母组合的代码
tfidf = TfidfVectorizer(sublinear_tf=True,
min_df=5,
stop_words=STOPWORDS,
norm = 'l2',
encoding='latin-1',
ngram_range=(1, 2))
feat = tfidf.fit_transform(data['Combine']).toarray()
N = 5 # Number of examples to be listed
for f, i in sorted(category_labels.items()):
chi2_feat = chi2(feat, labels == i)
indices = np.argsort(chi2_feat[0])
feat_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [w for w in feat_names if len(w.split(' ')) == 1]
bigrams = [w for w in feat_names if len(w.split(' ')) == 2]
print("\nFlair '{}':".format(f))
print("Most correlated unigrams:\n\t. {}".format('\n\t. '.join(unigrams[-N:])))
print("Most correlated bigrams:\n\t. {}".format('\n\t. '.join(bigrams[-N:])))
一种方法是在改组后传递基础的numpy数组:
data[:] = data.sample(frac=1).values