Bag of words và N-grams

Feature Engineering cho Machine Learning bằng Python

Robert O'Callaghan

Director of Data Science, Ordergroove

Vấn đề với bag of words

 

Nghĩa tích cực

Từ đơn: happy

Nghĩa tiêu cực

Bi-gram: not happy

Nghĩa tích cực

Trigram: never not happy

Feature Engineering cho Machine Learning bằng Python

Dùng N-grams

tv_bi_gram_vec = TfidfVectorizer(ngram_range = (2,2))

# Fit và áp dụng bộ vector hóa bigram
tv_bi_gram = tv_bi_gram_vec\
               .fit_transform(speech_df['text'])

# In các đặc trưng bigram
print(tv_bi_gram_vec.get_feature_names())
[u'american people', u'best ability ',
 u'beloved country', u'best interests' ... ]

Feature Engineering cho Machine Learning bằng Python

Tìm từ phổ biến

# Tạo DataFrame với các đặc trưng Counts
tv_df = pd.DataFrame(tv_bi_gram.toarray(),
                     columns=tv_bi_gram_vec.get_feature_names())\
                        .add_prefix('Counts_')

tv_sums = tv_df.sum()
print(tv_sums.head())
Counts_administration government    12
Counts_almighty god                 15
Counts_american people              36
Counts_beloved country               8
Counts_best ability                  8
dtype: int64
Feature Engineering cho Machine Learning bằng Python

Tìm từ phổ biến

print(tv_sums.sort_values(ascending=False)).head()
Counts_united states         152
Counts_fellow citizens        97
Counts_american people        36
Counts_federal government     35
Counts_self government        30
dtype: int64
Feature Engineering cho Machine Learning bằng Python

Hãy thực hành!

Feature Engineering cho Machine Learning bằng Python

Preparing Video For Download...