Metin Kodlamaya Giriş

Python ile Machine Learning için Özellik Mühendisliği

Robert O'Callaghan

Director of Data Science, Ordergroove

Metninizi standartlaştırma

Serbest metin örneği:

Senato ve Temsilciler Meclisi’nin Yurttaşları: Yaşamın değişkenlikleri arasında, emrinizle bildirimi iletilen ve bu ayın ... gününde alınan olaydan daha büyük kaygı vereni olamazdı.

Python ile Machine Learning için Özellik Mühendisliği

Veri kümesi

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...
Python ile Machine Learning için Özellik Mühendisliği

İstenmeyen karakterleri kaldırma

  • [a-zA-Z]: Tüm harf karakterleri
  • [^a-zA-Z]: Harf olmayan tüm karakterler
speech_df['text'] = speech_df['text']\
                   .str.replace('[^a-zA-Z]', ' ')
Python ile Machine Learning için Özellik Mühendisliği

İstenmeyen karakterleri kaldırma

Önce:

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

Sonra:

"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" ...
Python ile Machine Learning için Özellik Mühendisliği

Büyük/küçük harfleri standartlaştırma

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"...
Python ile Machine Learning için Özellik Mühendisliği

Metin uzunluğu

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
Python ile Machine Learning için Özellik Mühendisliği

Kelime sayıları

speech_df['word_cnt'] = 
    speech_df['text'].str.split()
speech_df['word_cnt'].head(1)
['fellow', 'citizens', 'of', 'the', 'senate', 'and',...
Python ile Machine Learning için Özellik Mühendisliği

Kelime sayıları

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
Python ile Machine Learning için Özellik Mühendisliği

Ortalama kelime uzunluğu

speech_df['avg_word_len'] = 
         speech_df['char_cnt'] / speech_df['word_cnt']
Python ile Machine Learning için Özellik Mühendisliği

Haydi pratik yapalım!

Python ile Machine Learning için Özellik Mühendisliği

Preparing Video For Download...