Эта строка кода ниже здесь, где gram_result будет использовать этот фрейм данных, который состоит из двух классов ("7" и "другие") наборов данных
df = pd.read_csv(output.txt, sep="|")
Ниже приведены коды, которые я пробовал для Ngrams:
def gram_result():
# number of unigram/bigram to print out
'''N = no.of unigram, bigram or trigram to print out'''
'''Up to you how many no of unigram, bigram or trigram you want to print out'''
N = 5
for cat_id in df["cat_id"].sort_values().drop_duplicates():
features_chi2 = chi2(features, labels == cat_id)
indices = np.argsort(features_chi2[0])
feature_names = np.array(tfidf.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]
trigrams = [v for v in feature_names if len(v.split(' ')) == 3]
print("# '{}':".format(cat_id))
print(" . Most correlated unigrams:\n. {}".format('\n. '.join(unigrams[-N:])))
print(" . Most correlated bigrams:\n. {}".format('\n. '.join(bigrams[-N:])))
print(" . Most correlated trigrams:\n. {}".format('\n. '.join(trigrams[-N:])))
unigram_dict = {}
bigram_dict = {}
trigram_dict = {}
full_dict = {}
unigrams_array = []
bigram_array = []
trigram_array = []
'''Add keywords into a dictionary'''
for item in unigrams[-N:]:
unigrams_array.append(item)
unigram_dict[str(cat_id)] = unigrams_array
for item_bigram in bigrams[-N:]:
bigram_array.append(item_bigram)
for item_trigram in trigrams[-N:]:
trigram_array.append(item_trigram)
full_dict[cat_id] = (trigram_array, bigram_array, unigrams_array)
print(full_dict[cat_id])
print()
Вывод метода gram_result:
# '7':
. Most correlated unigrams:
. immobile
. junl
. tear
. uncommunicative
. indian
. Most correlated bigrams:
. air afebrile
. water hydration
. ptot keyed
. response plans
. indian nd
. Most correlated trigrams:
. reassessment indicated immobile
. water swallow test
. total stay standard
. room air afebrile
. iv cannula insitu
# 'others':
. Most correlated unigrams:
. immobile
. junl
. tear
. uncommunicative
. indian
. Most correlated bigrams:
. air afebrile
. water hydration
. ptot keyed
. response plans
. indian nd
. Most correlated trigrams:
. reassessment indicated immobile
. water swallow test
. total stay standard
. room air afebrile
. iv cannula insitu
Два класса («7» и «другие») имеют два разных набора данных.
Однако мне любопытно, почему два разных класса с разными наборами данных выводят одинаковые ключевые слова ngrams ...
Пожалуйста, помогите мне взглянуть на мои коды для gram_result () и дайте мне знать, если я могу улучшить метод ...