我最近偶遇的六個(gè)很酷的Python庫
譯文【51CTO.com快譯】Python是機(jī)器學(xué)習(xí)不可或缺的一部分,庫讓我們的生活更簡單。最近,我在處理機(jī)器學(xué)習(xí)項(xiàng)目時(shí)遇到了6個(gè)很棒的庫。它們幫我節(jié)省了大量時(shí)間,本文將介紹它們。
1. clean-text
clean-text是真正很出色的庫,如果您需要處理抓取內(nèi)容或社交媒體數(shù)據(jù),它應(yīng)該是您的首選。最棒的是它不需要任何冗長的花哨代碼或正則表達(dá)式來清理數(shù)據(jù)。不妨看幾個(gè)例子:
安裝
- !pip install cleantext
例子
- #Importing the clean text library
- from cleantext import clean# Sample texttext = """ Zürich, largest city of Switzerland and capital of the canton of 633Zürich. Located in an Al\u017eupine. (https://google.com). Currency is not ₹"""# Cleaning the "text" with clean textclean(text,
- fix_unicode=True,
- to_ascii=True,
- lower=True,
- no_urls=True,
- no_numbers=True,
- no_digits=True,
- no_currency_symbols=True,
- no_punct=True,
- replace_with_punct=" ",
- replace_with_url="",
- replace_with_number="",
- replace_with_digit=" ",
- replace_with_currency_symbol="Rupees")
輸出
我們從上面可以看到,它在Zurich一詞中含有Unicode(字母“u”已被編碼)、ASCII 字符(在Al\u017eupine 中)、盧比貨幣符號(hào)、HTML 鏈接和標(biāo)點(diǎn)符號(hào)。
您只需在clean函數(shù)中提及所需的ASCII、Unicode、URL、數(shù)字、貨幣和標(biāo)點(diǎn)符號(hào)?;蛘?,可以將它們換成上述函數(shù)中的替換參數(shù)。比如說,我將盧比符號(hào)換成了Rupees。
絕對(duì)不需要使用正則表達(dá)式或長代碼。非常方便的庫,如果您想清理來自抓取內(nèi)容或社交媒體數(shù)據(jù)的文本,尤為方便。您還可以根據(jù)要求單獨(dú)傳遞參數(shù),而不是將它們?nèi)拷M合在一起。
欲了解更多詳細(xì)信息,請查看該GitHub 存儲(chǔ)庫。
2. drawdata
drawdata是我發(fā)現(xiàn)的另一個(gè)很酷的Python庫。需要向團(tuán)隊(duì)解釋機(jī)器學(xué)習(xí)概念這種情形有多常見?肯定經(jīng)常發(fā)生,因?yàn)閿?shù)據(jù)科學(xué)注重團(tuán)隊(duì)合作。該庫可幫助您在Jupyter筆記本中繪制數(shù)據(jù)集。
我向團(tuán)隊(duì)解釋機(jī)器學(xué)習(xí)概念時(shí),確實(shí)很喜歡使用這個(gè)庫。感謝創(chuàng)建這個(gè)庫的開發(fā)人員!
Drawdata僅面向有四個(gè)類的分類問題。
安裝
- !pip install drawdata
例子
- # Importing the drawdata
- from drawdata import draw_scatterdraw_scatter()
輸出
執(zhí)行draw_Scatter()后將打開上述繪圖窗口。很顯然,有四個(gè)類,即A、B、C和D??梢渣c(diǎn)擊任何類,并繪制所需的點(diǎn)。每個(gè)類代表圖形中的不同顏色。您還可以選擇將數(shù)據(jù)作為csv或json文件來下載。此外,可以將數(shù)據(jù)復(fù)制到剪貼板,并從下面的代碼中讀?。?/p>
- #Reading the clipboardimport pandas as pd
- df = pd.read_clipboard(sep=",")
- df
這個(gè)庫的局限之一是它只提供有四個(gè)類的兩個(gè)數(shù)據(jù)點(diǎn)。但除此之外,它絕對(duì)有其價(jià)值。欲了解更多詳細(xì)信息,請查看該GitHub 鏈接。
3. Autoviz
我永遠(yuǎn)不會(huì)忘記花在使用matplotlib進(jìn)行探索性數(shù)據(jù)分析上的時(shí)間。有很多簡單的可視化庫。然而,我最近發(fā)現(xiàn)Autoviz僅用一行代碼即可自動(dòng)直觀顯示任何數(shù)據(jù)集。
安裝
- !pip install autoviz
例子
我在這個(gè)例子中使用了IRIS數(shù)據(jù)集。
- # Importing Autoviz class from the autoviz library
- from autoviz.AutoViz_Class import AutoViz_Class#Initialize the Autoviz class in a object called df
- df = AutoViz_Class()# Using Iris Dataset and passing to the default parametersfilename = "Iris.csv"
- sep = ","graph = df.AutoViz(
- filename,
- sep=",",
- depVar="",
- dfte=None,
- header=0,
- verbose=0,
- lowess=False,
- chart_format="svg",
- max_rows_analyzed=150000,
- max_cols_analyzed=30,
- )
上述參數(shù)是默認(rèn)值。欲了解更多信息,請點(diǎn)擊此處。
輸出
我們可以看到所有的視覺元素,僅用一行代碼完成我們的EDA。有很多自動(dòng)可視化庫,但我特別喜歡這個(gè)庫。
4. Mito
每個(gè)人都喜歡Excel,是不是?它是初次探索數(shù)據(jù)集的最簡單方法之一。幾個(gè)月前我遇到了Mito,但最近才試了試,我絕對(duì)愛不釋手!
它是一個(gè)帶GUI支持的Jupyter-lab擴(kuò)展Python庫,添加了電子表格功能。您可以加載 csv數(shù)據(jù),將數(shù)據(jù)集作為電子表格來編輯,它可自動(dòng)生成Pandas代碼。很酷。
Mito值得寫一篇完整的博文來介紹,但是今天不作詳細(xì)介紹。這是為您提供的簡單的任務(wù)演示。欲知更多詳情,請查看此處。
安裝
- #First install mitoinstaller in the command prompt
- pip install mitoinstaller# Then, run the installer in the command prompt
- python -m mitoinstaller install# Then, launch Jupyter lab or jupyter notebook from the command prompt
- python -m jupyter lab
想了解安裝方面的更多信息,請查看此處。
- # Importing mitosheet and ruuning this in Jupyter labimport mitosheet
- mitosheet.sheet()
執(zhí)行上述代碼后,mitosheet將在jupyter實(shí)驗(yàn)室中打開。我使用IRIS數(shù)據(jù)集。首先,我創(chuàng)建了兩個(gè)新列。一個(gè)是Sepal平均長度,另一個(gè)是Sepal總寬度。其次,我更改了Sepal平均長度的列名。最后,我為Sepal平均長度列創(chuàng)建了一個(gè)直方圖。
執(zhí)行上述步驟后,代碼會(huì)自動(dòng)生成。
輸出
為上述步驟生成以下代碼:
- from mitosheet import * # Import necessary functions from Mito
- register_analysis('UUID-119387c0-fc9b-4b04-9053-802c0d428285') # Let Mito know which analysis is being run# Imported C:\Users\Dhilip\Downloads\archive (29)\Iris.csv
- import pandas as pd
- Iris_csv = pd.read_csv('C:\Users\Dhilip\Downloads\archive (29)\Iris.csv')# Added column G to Iris_csv
- Iris_csv.insert(6, 'G', 0)# Set G in Iris_csv to =AVG(SepalLengthCm)
- Iris_csv['G'] = AVG(Iris_csv['SepalLengthCm'])# Renamed G to Avg_Sepal in Iris_csv
- Iris_csv.rename(columns={"G": "Avg_Sepal"}, inplace=True)
5. Gramformer
另一個(gè)出色的庫Gramformer基于生成模型,可幫助我們糾正句子中的語法。這個(gè)庫有三個(gè)模型,有檢測器、熒光筆和校正器。檢測器識(shí)別文本是否有錯(cuò)誤的語法。熒光筆標(biāo)記錯(cuò)誤的部分,校正器修復(fù)錯(cuò)誤。Gramformer是完全開源的軟件,目前處于早期階段。但它不適合長段落,因?yàn)樗鼉H適用于句子,已針對(duì)64個(gè)字符長度的句子進(jìn)行了訓(xùn)練。
目前,校正器和熒光筆模型切實(shí)有用。不妨看幾個(gè)例子。
安裝
- !pip3 install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git
為Gramformer創(chuàng)建實(shí)例
- gf = Gramformer(models = 1, use_gpu = False) # 1=corrector, 2=detector (presently model 1 is working, 2 has not implemented)
例子
- #Giving sample text for correction under gf.correctgf.correct(""" New Zealand is island countrys in southwestern Paciific Ocaen. Country population was 5 million """)
輸出
我們從上面的輸出中可以看到它糾正了語法,甚至糾正了拼寫錯(cuò)誤。一個(gè)非常棒的庫,功能也很棒。我還沒有在這里試過熒光筆,您可以試試,查看該GitHub文檔,以獲取更多詳細(xì)信息。
6. Styleformer
在使用Gramformer方面的良好體驗(yàn)鼓勵(lì)我尋找更多獨(dú)特的庫。我因此發(fā)現(xiàn)了Styleformer,這是另一個(gè)極具出色的Python庫。Gramformer和Styleformer都是由Prithiviraj Damodaran創(chuàng)建的,兩者都基于生成模型。感謝創(chuàng)建者開源。
Styleformer幫助將隨意句轉(zhuǎn)換成正式句、將正式句轉(zhuǎn)換成隨意句、將主動(dòng)句轉(zhuǎn)換成被動(dòng)句以及將被動(dòng)句轉(zhuǎn)換成主動(dòng)句。
不妨看幾個(gè)例子:
安裝
- !pip install git+https://github.com/PrithivirajDamodaran/Styleformer.git
為Styleformer創(chuàng)建實(shí)例
- sf = Styleformer(style = 0)# style = [0=Casual to Formal, 1=Formal to Casual, 2=Active to Passive, 3=Passive to Active etc..]
例子
- # Converting casual to formal sf.transfer("I gotta go")
- # Formal to casual
- sf = Styleformer(style = 1) # 1 -> Formal to casual# Converting formal to casual
- sf.transfer("Please leave this place")
- # Active to Passive
- sf = Styleformer(style = 2) # 2-> Active to Passive# Converting active to passive
- sf.transfer("We are going to watch a movie tonight.")
- # passive to active
- sf = Styleformer(style = 2) # 2-> Active to Passive# Converting passive to active
- sf.transfer("Tenants are protected by leases")
看到上面的輸出,轉(zhuǎn)換準(zhǔn)確。我使用這個(gè)庫將隨意句轉(zhuǎn)換成正式句,尤其是在我有一次分析時(shí)用于社交媒體帖子。欲了解更多詳細(xì)信息,請查看GitHub。
您可能熟悉了前面提到的一些庫,但像Gramformer和Styleformer這樣的庫是最近出現(xiàn)的庫。它們被嚴(yán)重低估了,當(dāng)然值得廣為人知,因?yàn)樗鼈優(yōu)槲夜?jié)省了大量時(shí)間,我在NLP項(xiàng)目中大量使用它們。
原文標(biāo)題:6 Cool Python Libraries That I Came Across Recently,作者:Dhilip Subramanian
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】