十個小眾卻實用的Python庫,用過的都說香!

  • 2020 年 3 月 28 日
  • 筆記

今天,我們將和大家分享一些用於數據科學任務的Python庫,這些庫並不常見,它們不如panda、scikit-learn、matplotlib等知名,但卻十分實用,下面就一起來看看都有哪些庫:

1、Wget

數據提取,尤其是從網路中提取數據,是數據科學家的重要任務之一。Wget是一個免費的工具,用於從Web下載非互動式的文件,它支援HTTP、HTTPS和FTP協議,以及通過HTTP代理進行檢索。由於它是非互動式的,所以即使用戶沒有登錄,它也可以在後台工作。因此,她很適合用於下載一個網站或一個頁面的所有影像。(項目地址: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

事實上,當每個類的樣本數量幾乎相同的情況下,分類演算法的效果是最好的,但在實際項目中大部分的數據集是不平衡的,這些數據集對機器學習演算法的學習階段和後續預測都有影響,imbalanced-learn的創建就是為了解決此類問題,它與scikit-learn兼容,是scikit-learn-contrib項目的一部分。下次如果你遇到不平衡的數據集時,考慮一下它。(項目地址:https://github.com/scikit-learn-contrib/imbalanced-learn

安裝:

pip install -U imbalanced-learn  # or  conda install -c conda-forge imbalanced-learn

4、FlashText

在NLP任務中清理文本數據通常需要替換句子中的關鍵字或從句子中提取關鍵字。這類操作一般使用正則表達式來完成,但是如果搜索的關鍵詞數量達到數千個,就會變得很麻煩。Python的FlashText模組是基於FlashText演算法,它為這種情況提供了一個合適的替代方案。FlashText最好的部分是,不管搜索詞的數量是多少,運行時都是一樣的。

(項目地址: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是一個非常有用的庫。可以快速實現諸如字元串匹配度、令牌匹配度等操作。它還可以方便地匹配保存在不同資料庫中的記錄。(項目地址: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是為處理時間序列問題而構建的Python開源庫。該庫擁有一系列優秀的現代時間序列模型,包括但不限於ARIMA、GARCH和VAR模型。總之,PyFlux為時間序列建模提供了一種高效的方法。值得嘗試。(項目地址:https://github.com/RJT1990/pyflux

安裝:

pip install pyflux

7、Ipyvolume

結果交流是數據科學的一個重要方面,可視化是一個很大的優勢,IPyvolume是一個Python庫,用於在Jupyter筆記型電腦中可視化三維圖形(如三維立體圖等),遺憾的是目前它還處於測試版本階段。(項目地址:https://github.com/maartenbreddels/ipyvolume

安裝:

Using pip  $ pip install ipyvolume  Conda/Anaconda  $ conda install -c conda-forge ipyvolume

示例:

8、Dash

Dash是一個用於構建Web應用程式的高效Python框架。它是基於Flask、Plotly.js和React.js創建的,並結合了現代UI元素(如下拉框、滑塊和圖形)與用戶分析性Python程式碼綁定在一起,而不需要再藉助Javascript。Dash非常適合構建數據可視化應用。然後可以在Web瀏覽器中呈現這些應用程式。(項目地址: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時,可視化數據就變得很方便。

安裝:

pip install bashplotlib

示例:

scatter --file data/texas.txt --pch .
hist --file data/exp.txt

10、Colorama

colorama是一個Python專門用來在控制台、命令行輸出彩色文字的模組,可以跨平台使用,在windows下linux下都工作良好。它使用標準的ANSI轉義碼來著色和樣式終端輸出。(項目地址: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. )

輸出如下:

以上就是我推薦的有關於處理數據科學方面任務的Python庫,不知道有沒有你喜歡的。