這十個不常見但卻十分實用的Python庫,你知道幾個?
Python是一門神奇的語言,它是世界上發(fā)展最快的編程語言之一,尤其在數(shù)據(jù)科學方面的作用大家是有目共睹,Python的整個生態(tài)系統(tǒng)和它的庫使它成為全世界用戶(初學者和高級用戶)的最佳選擇。它的成功和流行的一個原因是背后強大的庫集合。
今天,我們將和大家分享一些用于數(shù)據(jù)科學任務的Python庫,這些庫并不常見,它們不如panda、scikit-learn、matplotlib等知名,但卻十分實用,下面就一起來看看都有哪些庫:
1. Wget
數(shù)據(jù)提取,尤其是從網(wǎng)絡中提取數(shù)據(jù),是數(shù)據(jù)科學家的重要任務之一。Wget是一個免費的工具,用于從Web下載非交互式的文件,它支持HTTP、HTTPS和FTP協(xié)議,以及通過HTTP代理進行檢索。由于它是非交互式的,所以即使用戶沒有登錄,它也可以在后臺工作。因此,她很適合用于下載一個網(wǎng)站或一個頁面的所有圖像。
(項目地址:https://pypi.org/project/wget/)
安裝:
- $ pip install wget
示例:
- import wget
- url = 'http://www.futurecrew.com/skaven/song_files/mp3/razorback.mp3'
- filename = wget.download(url)
- 100% [................................................] 3841532 / 3841532
- filename
- 'razorback.mp3'
2. Pendulum
對于那些需要在Python項目中使用日期時間的人來說,Pendulum就是一項不錯的項目選自。它是一個用于簡化datetimes操作的Python包。它完全可以替代Python的原生類。
(項目地址:https://github.com/sdispater/pendulum)
安裝:
- $ pip install pendulum
示例:
- import pendulum
- dt_toronto = pendulum.datetime(2012, 1, 1, tz='America/Toronto')
- dt_vancouver = pendulum.datetime(2012, 1, 1, tz='America/Vancouver')
- print(dt_vancouver.diff(dt_toronto).in_hours())
- 3
3. imbalanced-learn
事實上,當每個類的樣本數(shù)量幾乎相同的情況下,分類算法的效果是最好的,但在實際項目中大部分的數(shù)據(jù)集是不平衡的,這些數(shù)據(jù)集對機器學習算法的學習階段和后續(xù)預測都有影響,imbalanced-learn的創(chuàng)建就是為了解決此類問題,它與scikit-learn兼容,是scikit-learn-contrib項目的一部分。下次如果你遇到不平衡的數(shù)據(jù)集時,考慮一下它。
(項目地址:https://github.com/scikit-learn-contrib/imbalanced-learn)
安裝:
- pip install -U imbalanced-learn
- # or
- conda install -c conda-forge imbalanced-learn
4. FlashText
在NLP任務中清理文本數(shù)據(jù)通常需要替換句子中的關鍵字或從句子中提取關鍵字。這類操作一般使用正則表達式來完成,但是如果搜索的關鍵詞數(shù)量達到數(shù)千個,就會變得很麻煩。Python的FlashText模塊是基于FlashText算法,它為這種情況提供了一個合適的替代方案。FlashText最好的部分是,不管搜索詞的數(shù)量是多少,運行時都是一樣的。
(項目地址:https://github.com/vi3k6i5/flashtext)
安裝:
- $ pip install flashtext
示例:
- from flashtext import KeywordProcessor
- keyword_processor = KeywordProcessor()
- # keyword_processor.add_keyword(<unclean name>, <standardised name>)
- keyword_processor.add_keyword('Big Apple', 'New York')
- keyword_processor.add_keyword('Bay Area')
- keywords_found = keyword_processor.extract_keywords('I love Big Apple and Bay Area.')
- keywords_found
- ['New York', 'Bay Area']
關鍵詞替換:
- keyword_processor.add_keyword('New Delhi', 'NCR region')
- new_sentence = keyword_processor.replace_keywords('I love Big Apple and new delhi.')
- new_sentence
- 'I love New York and NCR region.'
5. Fuzzywuzzy
這個名字聽起來確實很奇怪,但是涉及到字符匹配時,fuzzywuzzy是一個非常有用的庫??梢钥焖賹崿F(xiàn)諸如字符串匹配度、令牌匹配度等操作。它還可以方便地匹配保存在不同數(shù)據(jù)庫中的記錄。
(項目地址:https://github.com/seatgeek/fuzzywuzzy)
安裝:
- $ pip install fuzzywuzzy
示例:
- from fuzzywuzzy import fuzz
- from fuzzywuzzy import process
- # Simple Ratio
- fuzz.ratio("this is a test", "this is a test!")
- 97
- # Partial Ratio
- fuzz.partial_ratio("this is a test", "this is a test!")
- 100
6. PyFlux
時間序列分析是機器學習領域最常遇到的問題之一。PyFlux是為處理時間序列問題而構(gòu)建的Python開源庫。該庫擁有一系列優(yōu)秀的現(xiàn)代時間序列模型,包括但不限于ARIMA、GARCH和VAR模型??傊?,PyFlux為時間序列建模提供了一種高效的方法。值得嘗試。
(項目地址:https://github.com/RJT1990/pyflux)
安裝:
- pip install pyflux
7. Ipyvolume
結(jié)果交流是數(shù)據(jù)科學的一個重要方面,可視化是一個很大的優(yōu)勢,IPyvolume是一個Python庫,用于在Jupyter筆記本中可視化三維圖形(如三維立體圖等),遺憾的是目前它還處于測試版本階段。
(項目地址:https://github.com/maartenbreddels/ipyvolume)
安裝:
- Using pip
- $ pip install ipyvolume
- Conda/Anaconda
- $ conda install -c conda-forge ipyvolume
示例:
8. Dash
Dash是一個用于構(gòu)建Web應用程序的高效Python框架。它是基于Flask、Plotly.js和React.js創(chuàng)建的,并結(jié)合了現(xiàn)代UI元素(如下拉框、滑塊和圖形)與用戶分析性Python代碼綁定在一起,而不需要再借助Javascript。Dash非常適合構(gòu)建數(shù)據(jù)可視化應用。然后可以在Web瀏覽器中呈現(xiàn)這些應用程序。
(項目地址:https://github.com/plotly/dash)
安裝:
- pip install dash==0.29.0 # The core dash backend
- pip install dash-html-components==0.13.2 # HTML components
- pip install dash-core-components==0.36.0 # Supercharged components
- pip install dash-table==3.1.3 # Interactive DataTable component (new!)
示例:
9. Bashplotlib
Bashplotlib是一個Python包和命令行工具,用于在終端生成基本的繪圖,使用Python編寫的,當用戶無法訪問GUI時,可視化數(shù)據(jù)就變得很方便。
安裝:
- pip install bashplotlib
示例:
- scatter --file data/texas.txt --pch .
- hist --file data/exp.txt
10. Colorama
colorama是一個Python專門用來在控制臺、命令行輸出彩色文字的模塊,可以跨平臺使用,在windows下linux下都工作良好。它使用標準的ANSI轉(zhuǎn)義碼來著色和樣式終端輸出。(項目地址:https://github.com/tartley/colorama)
安裝:
- pip install colorama
示例:
- import colorama
- from colorama import Fore, Back, Style
- colorama.init()
- # Set the color semi-permanently
- print(Fore.CYAN)
- print("The Text will appear in cyan until it is reset")
- print(Style.RESET_ALL)
- # Colorize a single line and then reset
- print(Fore.RED + 'Colorize a single line in RED' + Style.RESET_ALL)
- # Colorize a single word in the output
- print('You can also colorize a single word' + Back.GREEN + 'words' + Style.RESET_ALL + ' can be highlighted')
- # Combine foreground and background color
- print(Fore.BLUE + Back.WHITE)
- print('Foreground, background, and styles can be combined')
- print("========== ")
- print(Style.RESET_ALL)
- print('Reset everything back to normal.')
輸出如下:
以上就是我推薦的有關于處理數(shù)據(jù)科學方面任務的Python庫,不知道有沒有你喜歡的。