Python資料科學(三) python與資料科學應用(Ⅲ)

一隻寫程式的猿發表於2017-12-14

Python資料科學(三)  python與資料科學應用(Ⅲ)

傳送門:

1.使用Python計算文章中的字

speech_text = '''
  I love you,Not for what you are,But for what I amWhen I am with you.I love you,Not
 only for whatYou have made of yourself,But for whatYou are making of me.I love
 youFor the part of meThat you bring out;I love youFor putting your handInto my
 heaped-up heartAnd passing overAll the foolish, weak thingsThat you can’t
 helpDimly seeing there,And for drawing outInto the lightAll the beautiful
 belongingsThat no one else had lookedQuite far enough to find.I love you because
 youAre helping me to makeOf the lumber of my lifeNot a tavernBut a temple;Out of 
the worksOf my every dayNot a reproachBut a song.I love youBecause you have
 doneMore than any creedCould have doneTo make me goodAnd more than any
 fateCould have doneTo make me happy.You have done itWithout a touch,Without a
 word,Without a sign.You have done itBy being yourself.Perhaps that is whatBeing a 
friend means,After all.
'''

speech = speech_text.split()

dic = {}
for word in speech:
    if word not in dic:
        dic[word]=1
    else:
        dic[word]=dic[word] + 1


dic.items()
複製程式碼

Python資料科學(三)  python與資料科學應用(Ⅲ)

Python資料科學(三)  python與資料科學應用(Ⅲ)

在使用nltk的時候,發現一直報錯,可以使用下邊兩行命令安裝nltk

import nltk
nltk.download()
複製程式碼

會彈出以下視窗,下載nltk.

Python資料科學(三)  python與資料科學應用(Ⅲ)
正在下載

如果這種方式下載完成了 那就直接跳過下一步

我下了很多次最後都下載失敗了,現在說第二種方法。 直接下載打包好的安裝包:下載地址1:雲盤密碼znx7,下來的包nltk_data.zip 解壓到C盤根目錄下,這樣是最保險的,防止找不到包。下載地址2:雲盤密碼4cp3

感謝【V_can--Python與自然語言處理_第一期_NLTK入門之環境搭建提供的安裝包】

去除停用詞

2.使用第二種方法直接使用python中的第三方庫Counter

#程式碼如下
from collections import Counter
c = Counter(speech)
c. most_common(10)#出現的前十名
print(c. most_common(10))

for sw in stop_words:
    del c[sw]
c.most_common(10)
複製程式碼

Counter 是實現的 dict 的一個子類,可以用來方便地計數。

Python資料科學(三)  python與資料科學應用(Ⅲ)

  • 附上完整程式碼

speech_text = '''
I love you,
Not for what you are,
But for what I amWhen I am with you.
I love you,
Not only for whatYou have made of yourself,
But for whatYou are making of me.
I love youFor the part of meThat you bring out;
I love youFor putting your handInto my heaped-up heartAnd passing overAll the foolish, 
weak thingsThat you can’t helpDimly seeing there,
And for drawing outInto the lightAll the beautiful belongingsThat no one else had lookedQuite far enough to find.
I love you because youAre helping me to makeOf the lumber of my lifeNot a tavernBut a temple;
Out of the worksOf my every dayNot a reproachBut a song.
I love youBecause you have doneMore than any creedCould have doneTo make me goodAnd more than any fateCould have doneTo make me happy.
You have done itWithout a touch,
Without a word,
Without a sign.
You have done itBy being yourself.
Perhaps that is whatBeing a friend means,
After all.
'''

#解決大小寫的問題
speech = speech_text.lower().split()
print(speech)

dic = {}
for word in  speech:
    if word not in dic:
        dic[word] = 1
    else:
        dic[word] = dic[word] + 1

import operator
swd = sorted(dic.items(),key=operator.itemgetter(1),reverse=True)
print(swd)

#停用詞處理
from nltk.corpus import stopwords
stop_words = stopwords.words('English')

for k,v in swd:
    if k not in stop_words:
        print(k,v)


from collections import Counter
c = Counter(speech)
c. most_common(10)#出現的前十名
print(c. most_common(10))

for sw in stop_words:
    del c[sw]
c.most_common(10)
複製程式碼

通過這兩種方法我們就不難明白為什麼現在Python 在資料分析、科學計算領域用得越來越多,除了語言本身的特點,第三方庫也很多很好用。

所以還等什麼,人生幾何,何不Python當歌。 跟我一塊學Python吧。

相關文章