问题描述
我正在研究从 kaggle 获得的 youtube 数据集,我正在学习有关文本分类的教程,https://github.com/agrawal-rohit/Text-Classification-Analysis/blob/master/Text%20Classification%20Analysis.ipynb。 在这个数据集中,有一个视频类别列,它是一个描述视频的字符串。 我的问题是标签编码器部分。我知道用于将文本转换为数字以进行分类的标签编码器。这是代码,
# Encode classes
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
le.fit(data.Category)
data.Category = le.transform(data.Category)
data.head(5)
然后,使用 TF-IDF 对文本特征进行矢量化,
# TF-IDF
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_title = TfidfVectorizer(sublinear_tf=True,min_df=5,norm='l2',encoding='latin-1',ngram_range=(1,2),stop_words='english')
tfidf_desc = TfidfVectorizer(sublinear_tf=True,stop_words='english')
labels = data.Category
features_title = tfidf_title.fit_transform(data.Title).toarray()
features_description = tfidf_desc.fit_transform(data.Description).toarray()
print('Title Features Shape: ' + str(features_title.shape))
print('Description Features Shape: ' + str(features_description.shape))
最后,为每个类打印 5 个最重要的特征..
# Best 5 keywords for each class using Title Feaures
from sklearn.feature_selection import chi2
import numpy as np
N = 5
for current_class in list(le.classes_):
current_class_id = le.transform([current_class])[0]
features_chi2 = chi2(features_title,labels == current_class_id)
indices = np.argsort(features_chi2[0])
feature_names = np.array(tfidf_title.get_feature_names())[indices]
unigrams = [v for v in feature_names if len(v.split(' ')) == 1]
bigrams = [v for v in feature_names if len(v.split(' ')) == 2]
print("# '{}':".format(current_class))
print("Most correlated unigrams:")
print('-' *30)
print('. {}'.format('\n. '.join(unigrams[-N:])))
print("Most correlated bigrams:")
print('-' *30)
print('. {}'.format('\n. '.join(bigrams[-N:])))
print("\n")
我的问题是,如果我已经有一个 category_id 列和一个类别列,则显示哪个 id 属于哪个类(我在这里不需要编码器部分)。 在示例中,for 循环从编码器列表中提取类别。那么,如何在不使用编码部分的情况下遍历 category_id 列并打印属于该 id 的类别?
for current_class in list(le.classes_):
current_class_id = le.transform([current_class])[0]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)