Giới thiệu về mã hóa văn bản

Feature Engineering cho Machine Learning bằng Python

Robert O'Callaghan

Director of Data Science, Ordergroove

Chuẩn hóa văn bản của bạn

Ví dụ văn bản tự do:

Thưa các công dân của Thượng viện và Hạ viện: TRONG những thăng trầm của cuộc đời, không sự kiện nào có thể khiến tôi lo lắng hơn việc thông báo đã được gửi theo lệnh của quý vị và tôi nhận được vào ngày của tháng này.

Feature Engineering cho Machine Learning bằng Python

Tập dữ liệu

print(speech_df.head())
                  Name           Inaugural Address    \ 
0    George Washington     First Inaugural Address
1    George Washington    Second Inaugural Address
2    John Adams                  Inaugural Address    
3    Thomas Jefferson      First Inaugural Address    
4    Thomas Jefferson     Second Inaugural Address

                        Date                               text
0    Thursday, April 30, 1789    Fellow-Citizens of the Sena...
1       Monday, March 4, 1793    Fellow Citizens: I AM again...
2     Saturday, March 4, 1797    WHEN it was first perceived...
3    Wednesday, March 4, 1801    Friends and Fellow-Citizens...
4       Monday, March 4, 1805    PROCEEDING, fellow-citizens...
Feature Engineering cho Machine Learning bằng Python

Loại bỏ ký tự không mong muốn

  • [a-zA-Z]: Tất cả ký tự chữ cái
  • [^a-zA-Z]: Tất cả ký tự không phải chữ
speech_df['text'] = speech_df['text']\
                   .str.replace('[^a-zA-Z]', ' ')
Feature Engineering cho Machine Learning bằng Python

Loại bỏ ký tự không mong muốn

Trước:

"Fellow-Citizens of the Senate and of the House of  
Representatives: AMONG the vicissitudes incident to   
life no event could have filled me with greater" ...

Sau:

"Fellow Citizens of the Senate and of the House of  
Representatives AMONG the vicissitudes incident to   
life no event could have filled me with greater" ...
Feature Engineering cho Machine Learning bằng Python

Chuẩn hóa chữ hoa/thường

speech_df['text'] = speech_df['text'].str.lower()
print(speech_df['text'][0])
"fellow citizens of the senate and of the house of  
representatives among the vicissitudes incident to   
life no event could have filled me with greater"...
Feature Engineering cho Machine Learning bằng Python

Độ dài văn bản

speech_df['char_cnt'] = speech_df['text'].str.len()
print(speech_df['char_cnt'].head())
0    1889  
1     806  
2    2408  
3    1495  
4    2465
Name: char_cnt, dtype: int64
Feature Engineering cho Machine Learning bằng Python

Số lượng từ

speech_df['word_cnt'] = 
    speech_df['text'].str.split()
speech_df['word_cnt'].head(1)
['fellow', 'citizens', 'of', 'the', 'senate', 'and',...
Feature Engineering cho Machine Learning bằng Python

Số lượng từ

speech_df['word_counts'] = 
    speech_df['text'].str.split().str.len()
print(speech_df['word_splits'].head())
0    1432
1     135
2    2323
3    1736
4    2169
Name: word_cnt, dtype: int64
Feature Engineering cho Machine Learning bằng Python

Độ dài từ trung bình

speech_df['avg_word_len'] = 
         speech_df['char_cnt'] / speech_df['word_cnt']
Feature Engineering cho Machine Learning bằng Python

Ayo berlatih!

Feature Engineering cho Machine Learning bằng Python

Preparing Video For Download...