本文作者Favio Vázquez從2018年開始發布《數據科學和人工智能每周文摘: & R》系列文章,為數據科學家介紹最好的庫、repos、以及工具。
一年結束,作者列出了2018年的7大最好的庫,這些庫確實地改進了研究人員的工作方式。
TOP7:快速靈活的框架
是一個輕量級的、可擴展的 框架,用于使用算法訓練和部署自適應神經網絡[ et al. ICML 2017]。結合了多個學習子網絡,以減輕設計有效的神經網絡所固有的復雜性。
這個軟件包將幫助你選擇最優的神經網絡架構,實現一種自適應算法,用于學習作為子網絡集合的神經架構。
你需要了解才能使用這個包,因為它實現了 ,但這將通過封裝訓練、評估、預測和導出服務來幫助你簡化機器學習編程。
你可以構建一個神經網絡的集合,這個庫將幫助你優化一個目標,以平衡集合在訓練集上的性能和將其泛化到未見過數據的能力之間的權衡。
1. 安裝
安裝之前需將升級到1.7或以上:
$?pip?install?"tensorflow>=1.7.0"
2. 從源代碼安裝
要從源代碼進行安裝,首先需要安裝bazel。
下一步,復制和cd到它的根目錄:
$?git?clone?https://github.com/tensorflow/adanet?&&?cd?adanet
從根目錄運行測試:
$?cd?adanet
$?bazel?test?-c?opt?//...
確認一切正常后,將安裝為pip包。
現在iris數據集分類器代碼,可以對進行試驗了。
import?adanet
3. 用法
有關的詳細用法,請閱讀官方教程:
TOP6:TPOT一個自動化的機器學習工具
之前我介紹過Auto-Keras,這是一個很棒的庫。現在我們有另一個非常有趣的工具——TPOT。
TPOT全稱是基于樹的優化工具(Tree-based Tool),這是一個非常棒自動機器學習工具,使用遺傳編程優化機器學習。
TPOT可以自動化許多東西,包括生命特性選擇、模型選擇、特性構建等等。如果你是機器學習者,很幸運,TPOT是構建在-learn之上的,所以它生成的所有代碼看起來應該很熟悉。
它的作用是通過智能地探索數千種可能的來自動化機器學習中最繁瑣的部分,找到最適合你的數據的,然后為你提供最佳的 代碼。
它的工作原理如下:
1. 安裝
安裝TPOT之前,請先閱讀教程:
然后,運行以下代碼:
pip?install?tpot
2. 例子
首先讓我們從基本的Iris數據集開始:
1from?tpot?import?TPOTClassifier
2from?sklearn.datasets?import?load_iris
3from?sklearn.model_selection?import?train_test_split
4
5#?Load?iris?dataset
6iris?=?load_iris()
7
8#?Split?the?data
9
10X_trainX_train,?X_test,?y_train,?y_test?=?train_test_split(iris.data,?iris.target,
11????????????????????????????????????????????????????train_size=0.75,?test_size=0.25)
12
13#?Fit?the?TPOT?classifier?
14
15tpot?=?TPOTClassifier(verbosity=2,?max_time_mins=2)
16tpot.fit(X_train,?y_train)
17
18#?Export?the?pipeline
19tpot.export('tpot_iris_pipeline.py')
我們在這里構建了一個非常基本的TPOT ,它將嘗試尋找最佳ML 來預測iris.。然后保存這個。之后,我們要做的就非常簡單了——加載生成的.py文件,你將看到:
1import?numpy?as?np
2from?sklearn.kernel_approximation?import?RBFSampler
3from?sklearn.model_selection?import?train_test_split
4from?sklearn.pipeline?import?make_pipeline
5from?sklearn.tree?import?DecisionTreeClassifier
6#?NOTE:?Make?sure?that?the?class?is?labeled?'class'?in?the?data?file
7tpot_data?=?np.recfromcsv('PATH/TO/DATA/FILE',?delimiter='COLUMN_SEPARATOR',?dtype=np.float64)
8features?=?np.delete(tpot_data.view(np.float64).reshape(tpot_data.size,?-1),?tpot_data.dtype.names.index('class'),?axis=1)
9training_features,?testing_features,?training_classes,?testing_classes?=?\
10????train_test_split(features,?tpot_data['class'],?random_state=42)
11exported_pipeline?=?make_pipeline(
12????RBFSampler(gamma=0.8500000000000001),
13????DecisionTreeClassifier(criterion="entropy",?max_depth=3,?min_samples_leaf=4,?min_samples_split=9)
14)
15exported_pipeline.fit(training_features,?training_classes)
16results?=?exported_pipeline.predict(testing_features)
就是這樣。你已經以一種簡單但強大的方式為Iris數據集構建一個分類器。
現在我們來看看MNIST的數據集:
1from?tpot?import?TPOTClassifier
2from?sklearn.datasets?import?load_digits
3from?sklearn.model_selection?import?train_test_split
4
5#?load?and?split?dataset?
6digitsdigits??==??load_digitsload_di?()
7X_train,?X_test,?y_train,?y_test?=?train_test_split(digits.data,?digits.target,
8????????????????????????????????????????????????????train_size=0.75,?test_size=0.25)
9
10#?Fit?the?TPOT?classifier?
11tpot?=?TPOTClassifier(verbosity=2,?max_time_mins=5,?population_size=40)
12tpot.fit(X_train,?y_train)
13
14#?Export?pipeline
15tpot.export('tpot_mnist_pipeline.py')
接下來我們再次加載生成的 .py文件,你將看到:
1import?numpy?as?np
2from?sklearn.model_selection?import?train_test_split
3from?sklearn.neighbors?import?KNeighborsClassifier
4#?NOTE:?Make?sure?that?the?class?is?labeled?'class'?in?the?data?file
5tpot_data?=?np.recfromcsv('PATH/TO/DATA/FILE',?delimiter='COLUMN_SEPARATOR',?dtype=np.float64)
6features?=?np.delete(tpot_data.view(np.float64).reshape(tpot_data.size,?-1),?tpot_data.dtype.names.index('class'),?axis=1)
7training_features,?testing_features,?training_classes,?testing_classes?=?\
8????train_test_split(features,?tpot_data['class'],?random_state=42)
9exported_pipeline?=?KNeighborsClassifier(n_neighbors=4,?p=2,?weights="distance")
10exported_pipeline.fit(training_features,?training_classes)
11results?=?exported_pipeline.predict(testing_features)
TOP5:SHAP一個解釋任何機器模型輸出的統一方法
解釋機器學習模型并不容易。然而,它對許多商業應用程序來說非常重要。幸運的是,有一些很棒的庫可以幫助我們完成這項任務。在許多應用程序中,我們需要知道、理解或證明輸入變量在模型中的運作方式,以及它們如何影響最終的模型預測。
SHAP ( )是一種解釋任何機器學習模型輸出的統一方法。SHAP將博弈論與局部解釋聯系起來,并結合了之前的幾種方法。
1. 安裝
SHAP可以從PyPI安裝
pip?install?shap
或conda -forge
conda?install?-c?conda-forge?shap
2. 用法
有很多不同的模型和方法可以使用這個包。在這里,我將以中的一個例子為例。
Deep SHAP是深度學習模型中SHAP值的一種高速近似算法,它基于與的連接,如SHAP的NIPS論文所述:
下面這個例子可以看到SHAP如何被用來解釋MNIST數據集的Keras模型結果:
#?this?is?the?code?from?https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py
from?__future__?import?print_function
import?keras
from?keras.datasets?import?mnist
from?keras.models?import?Sequential
from?keras.layers?import?Dense,?Dropout,?Flatten
from?keras.layers?import?Conv2D,?MaxPooling2D
from?keras?import?backend?as?K
batch_size?=?128
num_classes?=?10
epochs?=?12
#?input?image?dimensions
img_rows,?img_cols?=?28,?28
#?the?data,?split?between?train?and?test?sets
(x_train,?y_train),?(x_test,?y_test)?=?mnist.load_data()
if?K.image_data_format()?==?'channels_first':
????x_train?=?x_train.reshape(x_train.shape[0],?1,?img_rows,?img_cols)
????x_test?=?x_test.reshape(x_test.shape[0],?1,?img_rows,?img_cols)
????input_shape?=?(1,?img_rows,?img_cols)
else:
????x_train?=?x_train.reshape(x_train.shape[0],?img_rows,?img_cols,?1)
????x_test?=?x_test.reshape(x_test.shape[0],?img_rows,?img_cols,?1)
????input_shape?=?(img_rows,?img_cols,?1)
x_train?=?x_train.astype('float32')
x_test?=?x_test.astype('float32')
x_train?/=?255
x_test?/=?255
print('x_train?shape:',?x_train.shape)
print(x_train.shape[0],?'train?samples')
print(x_test.shape[0],?'test?samples')
#?convert?class?vectors?to?binary?class?matrices
y_train?=?keras.utils.to_categorical(y_train,?num_classes)
y_test?=?keras.utils.to_categorical(y_test,?num_classes)
model?=?Sequential()
model.add(Conv2D(32,?kernel_size=(3,?3),
?????????????????activation='relu',
?????????????????input_shape=input_shape))
model.add(Conv2D(64,?(3,?3),?activation='relu'))
model.add(MaxPooling2D(pool_size=(2,?2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128,?activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes,?activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
??????????????optimizer=keras.optimizers.Adadelta(),
??????????????metrics=['accuracy'])
model.fit(x_train,?y_train,
??????????batch_size=batch_size,
??????????epochs=epochs,
??????????verbose=1,
??????????validation_data=(x_test,?y_test))
score?=?model.evaluate(x_test,?y_test,?verbose=0)
print('Test?loss:',?score[0])
print('Test?accuracy:',?score[1])
更多示例:
#-
TOP4:使用 和 Spark 輕松實現敏捷數據科學工作流
V2旨在讓數據清理更容易。這個API的設計對新手來說超級簡單,對使用的人來說也非常熟悉。擴展了Spark 功能,添加了.rows和.cols屬性。
使用,你可以以分布式的方式清理數據、準備數據、分析數據、創建分析器和圖表,并執行機器學習和深度學習,因為它的后端有Spark、和Keras。
是數據科學敏捷方法的完美工具,因為它幾乎可以幫助你完成整個過程的所有步驟,并且可以輕松地連接到其他庫和工具。
(pip):
pip?install?optimuspyspark
1. 用法
在這個示例中,你可以從 URL 加載數據,對其進行轉換,并應用一些預定義的清理功能:
from?optimus?import?Optimus
op?=?Optimus()
#?This?is?a?custom?function
def?func(value,?arg):
????return?"this?was?a?number"
df?=op.load.url("https://raw.githubusercontent.com/ironmussa/Optimus/master/examples/foo.csv")
df\
????.rows.sort("product","desc")\
????.cols.lower(["firstName","lastName"])\
????.cols.date_transform("birth",?"new_date",?"yyyy/MM/dd",?"dd-MM-YYYY")\
????.cols.years_between("birth",?"years_between",?"yyyy/MM/dd")\
????.cols.remove_accents("lastName")\
????.cols.remove_special_chars("lastName")\
????.cols.replace("product","taaaccoo","taco")\
????.cols.replace("product",["piza","pizzza"],"pizza")\
????.rows.drop(df["id"]<7)\
????.cols.drop("dummyCol")\
????.cols.rename(str.lower)\
????.cols.apply_by_dtypes("product",func,"string",?data_type="integer")\
????.cols.trim("*")\
????.show()
你可以將這個表格
轉換為這樣:
是不是很酷?這個庫還可以做更多事情iris數據集分類器代碼,具體請閱讀:
TOP3:spacy使用和的工業級自然語言處理
spaCy旨在幫助你完成實際的工作——構建真實的產品,或收集真實的見解。這個庫尊重你的時間,盡量避免浪費。它易于安裝,而且它的API簡單而高效。spaCy被視為自然語言處理的Ruby on Rails。
spaCy是為深度學習準備文本的最佳方法。它與、、-learn、以及強大的AI生態系統的其他部分無縫交互。使用spaCy,你可以很容易地為各種NLP問題構建語言復雜的統計模型。
1. 安裝
pip3?install?spacy
$?python3?-m?spacy?download?en
這里,我們還下載了英語語言模型。你可以在這里找到德語,西班牙語,意大利語,葡萄牙語,法國語等版本的模型:
下面是主頁面的一個示例:
#?python?-m?spacy?download?en_core_web_sm
import?spacy
#?Load?English?tokenizer,?tagger,?parser,?NER?and?word?vectors
nlp?=?spacy.load('en_core_web_sm')
#?Process?whole?documents
text?=?(u"When?Sebastian?Thrun?started?working?on?self-driving?cars?at?"
????????u"Google?in?2007,?few?people?outside?of?the?company?took?him?"
????????u"seriously.?“I?can?tell?you?very?senior?CEOs?of?major?American?"
????????u"car?companies?would?shake?my?hand?and?turn?away?because?I?wasn’t?"
????????u"worth?talking?to,”?said?Thrun,?now?the?co-founder?and?CEO?of?"
????????u"online?higher?education?startup?Udacity,?in?an?interview?with?"
????????u"Recode?earlier?this?week.")
doc?=?nlp(text)
#?Find?named?entities,?phrases?and?concepts
for?entity?in?doc.ents:
????print(entity.text,?entity.label_)
#?Determine?semantic?similarities
doc1?=?nlp(u"my?fries?were?super?gross")
doc2?=?nlp(u"such?disgusting?fries")
similarity?=?doc1.similarity(doc2)
print(doc1.text,?doc2.text,?similarity)
在這個示例中,我們首先下載 , , , NER和word 。然后創建一些文本,打印找到的實體、短語和概念,最后確定兩個短語的語義相似性。運行這段代碼,你會得到:
Sebastian?Thrun?PERSON
Google?ORG
2007?DATE
American?NORP
Thrun?PERSON
Recode?ORG
earlier?this?week?DATE
my?fries?were?super?gross?such?disgusting?fries?0.7139701635071919
TOP2:
對我來說,是年度最佳。幾乎所有人都在像這樣的筆記本上工作,但是我們也在項目的更核心部分使用像這樣的IDE。
好消息是,你可以在自己喜歡的IDE中起草和測試普通腳本,在使用時可以將IDE作為在中打開。在中運行以生成輸出,關聯.ipynb表示,并作為普通腳本或傳統 進行保存和分享。
下圖展示了這個包的作用: