Introducción a la codificación de texto

Ingeniería de características para Machine Learning en Python

Robert O'Callaghan

Director of Data Science, Ordergroove

Estandariza tu texto

Ejemplo de texto libre:

«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 anxieties than that of which the notification was transmitted by your order, and received on the th day of the present month.»

Ingeniería de características para Machine Learning en Python

Conjunto de datos

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...
Ingeniería de características para Machine Learning en Python

Eliminar caracteres no deseados

  • [a-zA-Z]: Todas las letras
  • [^a-zA-Z]: Todo lo que no es letra
speech_df['text'] = speech_df['text']\
                   .str.replace('[^a-zA-Z]', ' ')
Ingeniería de características para Machine Learning en Python

Eliminar caracteres no deseados

Antes:

"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" ...

Después:

"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" ...
Ingeniería de características para Machine Learning en Python

Estandarizar mayúsculas y minúsculas

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"...
Ingeniería de características para Machine Learning en Python

Longitud del texto

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
Ingeniería de características para Machine Learning en Python

Conteo de palabras

speech_df['word_cnt'] = 
    speech_df['text'].str.split()
speech_df['word_cnt'].head(1)
['fellow', 'citizens', 'of', 'the', 'senate', 'and',...
Ingeniería de características para Machine Learning en Python

Conteo de palabras

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
Ingeniería de características para Machine Learning en Python

Longitud media de palabra

speech_df['avg_word_len'] = 
         speech_df['char_cnt'] / speech_df['word_cnt']
Ingeniería de características para Machine Learning en Python

¡Vamos a practicar!

Ingeniería de características para Machine Learning en Python

Preparing Video For Download...