基於 Python 和 Scikit-Learn 的機器學習介紹
點擊
上方藍字
,快速關注我們)
編譯:伯樂在線 - xeonqq
如有好文章投稿,請點擊 → 這裡了解詳情
你好,%用戶名%!
我叫Alex,我在機器學習和網路圖分析(主要是理論)有所涉獵。我同時在為一家俄羅斯移動運營商開發大數據產品。這是我第一次在網上寫文章,不喜勿噴。
現在,很多人想開發高效的演算法以及參加機器學習的競賽。所以他們過來問我:」該如何開始?」。一段時間以前,我在一個俄羅斯聯邦政府的下屬機構中領導了媒體和社交網路大數據分析工具的開發。我仍然有一些我團隊使用過的文檔,我樂意與你們分享。前提是讀者已經有很好的數學和機器學習方面的知識(我的團隊主要由MIPT(莫斯科物理與技術大學)和數據分析學院的畢業生構成)。
這篇文章是對數據科學的簡介,這門學科最近太火了。機器學習的競賽也越來越多(如,Kaggle, TudedIT),而且他們的資金通常很可觀。
R和Python是提供給數據科學家的最常用的兩種工具。每一個工具都有其優缺點,但Python最近在各個方面都有所勝出(僅為鄙人愚見,雖然我兩者都用)。這一切的發生是因為Scikit-Learn庫的騰空出世,它包含有完善的文檔和豐富的機器學習演算法。
請注意,我們將主要在這篇文章中探討機器學習演算法。通常用Pandas包去進行主數據分析會比較好,而且這很容易你自己完成。所以,讓我們集中精力在實現上。為了確定性,我們假設有一個特徵-對象矩陣作為輸入,被存在一個*.csv文件中。
數據載入
首先,數據要被載入到內存中,才能對其操作。Scikit-Learn庫在它的實現用使用了NumPy數組,所以我們將用NumPy來載入*.csv文件。讓我們從UCI Machine Learning Repository下載其中一個數據集。
import
numpy
as
np
import
urllib
# url with dataset
url
=
"http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
# download the file
raw_data
=
urllib
.
urlopen
(
url
)
# load the CSV file as a numpy matrix
dataset
=
np
.
loadtxt
(
raw_data
,
delimiter
=
","
)
# separate the data from the target attributes
X
=
dataset
[
:
,
0
:
7
]
y
=
dataset
[
:
,
8
]
我們將在下面所有的例子里使用這個數據組,換言之,使用X特徵物數組和y目標變數的值。
數據標準化
我們都知道大多數的梯度方法(幾乎所有的機器學習演算法都基於此)對於數據的縮放很敏感。因此,在運行演算法之前,我們應該進行標準化,或所謂的規格化。標準化包括替換所有特徵的名義值,讓它們每一個的值在0和1之間。而對於規格化,它包括數據的預處理,使得每個特徵的值有0和1的離差。Scikit-Learn庫已經為其提供了相應的函數。
from
sklearn
import
preprocessing
# normalize the data attributes
normalized_X
=
preprocessing
.
normalize
(
X
)
# standardize the data attributes
standardized_X
=
preprocessing
.
scale
(
X
)
特徵的選取
毫無疑問,解決一個問題最重要的是是恰當選取特徵、甚至創造特徵的能力。這叫做特徵選取和特徵工程。雖然特徵工程是一個相當有創造性的過程,有時候更多的是靠直覺和專業的知識,但對於特徵的選取,已經有很多的演算法可供直接使用。如樹演算法就可以計算特徵的信息量。
from
sklearn
import
metrics
from
sklearn
.
ensemble
import
ExtraTreesClassifier
model
=
ExtraTreesClassifier
()
model
.
fit
(
X
,
y
)
# display the relative importance of each attribute
(
model
.
feature_importances_
)
其他所有的方法都是基於對特徵子集的高效搜索,從而找到最好的子集,意味著演化了的模型在這個子集上有最好的質量。遞歸特徵消除演算法(RFE)是這些搜索演算法的其中之一,Scikit-Learn庫同樣也有提供。
from
sklearn
.
feature_selection
import
RFE
from
sklearn
.
linear_model
import
LogisticRegression
model
=
LogisticRegression
()
# create the RFE model and select 3 attributes
rfe
=
RFE
(
model
,
3
)
rfe
=
rfe
.
fit
(
X
,
y
)
# summarize the selection of the attributes
(
rfe
.
support_
)
(
rfe
.
ranking_
)
演算法的開發
正像我說的,Scikit-Learn庫已經實現了所有基本機器學習的演算法。讓我來瞧一瞧它們中的一些。
邏輯回歸
大多數情況下被用來解決分類問題(二元分類),但多類的分類(所謂的一對多方法)也適用。這個演算法的優點是對於每一個輸出的對象都有一個對應類別的概率。
from
sklearn
import
metrics
from
sklearn
.
linear_model
import
LogisticRegression
model
=
LogisticRegression
()
model
.
fit
(
X
,
y
)
(
model
)
# make predictions
expected
=
y
predicted
=
model
.
predict
(
X
)
# summarize the fit of the model
(
metrics
.
classification_report
(
expected
,
predicted
))
(
metrics
.
confusion_matrix
(
expected
,
predicted
))
樸素貝葉斯
它也是最有名的機器學習的演算法之一,它的主要任務是恢復訓練樣本的數據分布密度。這個方法通常在多類的分類問題上表現的很好。
from
sklearn
import
metrics
from
sklearn
.
naive_bayes
import
GaussianNB
model
=
GaussianNB
()
model
.
fit
(
X
,
y
)
(
model
)
# make predictions
expected
=
y
predicted
=
model
.
predict
(
X
)
# summarize the fit of the model
(
metrics
.
classification_report
(
expected
,
predicted
))
(
metrics
.
confusion_matrix
(
expected
,
predicted
))
k-最近鄰
kNN(k-最近鄰)方法通常用於一個更複雜分類演算法的一部分。例如,我們可以用它的估計值做為一個對象的特徵。有時候,一個簡單的kNN演算法在良好選擇的特徵上會有很出色的表現。當參數(主要是metrics)被設置得當,這個演算法在回歸問題中通常表現出最好的質量。
from
sklearn
import
metrics
from
sklearn
.
neighbors
import
KNeighborsClassifier
# fit a k-nearest neighbor model to the data
model
=
KNeighborsClassifier
()
model
.
fit
(
X
,
y
)
(
model
)
# make predictions
expected
=
y
predicted
=
model
.
predict
(
X
)
# summarize the fit of the model
(
metrics
.
classification_report
(
expected
,
predicted
))
(
metrics
.
confusion_matrix
(
expected
,
predicted
))
決策樹
分類和回歸樹(CART)經常被用於這麼一類問題,在這類問題中對象有可分類的特徵且被用於回歸和分類問題。決策樹很適用於多類分類。
from
sklearn
import
metrics
from
sklearn
.
tree
import
DecisionTreeClassifier
# fit a CART model to the data
model
=
DecisionTreeClassifier
()
model
.
fit
(
X
,
y
)
(
model
)
# make predictions
expected
=
y
predicted
=
model
.
predict
(
X
)
# summarize the fit of the model
(
metrics
.
classification_report
(
expected
,
predicted
))
(
metrics
.
confusion_matrix
(
expected
,
predicted
))
支持向量機
SVM(支持向量機)是最流行的機器學習演算法之一,它主要用於分類問題。同樣也用於邏輯回歸,SVM在一對多方法的幫助下可以實現多類分類。
from
sklearn
import
metrics
from
sklearn
.
svm
import
SVC
# fit a SVM model to the data
model
=
SVC
()
model
.
fit
(
X
,
y
)
(
model
)
# make predictions
expected
=
y
predicted
=
model
.
predict
(
X
)
# summarize the fit of the model
(
metrics
.
classification_report
(
expected
,
predicted
))
(
metrics
.
confusion_matrix
(
expected
,
predicted
))
除了分類和回歸問題,Scikit-Learn還有海量的更複雜的演算法,包括了聚類, 以及建立混合演算法的實現技術,如Bagging和Boosting。
如何優化演算法的參數
在編寫高效的演算法的過程中最難的步驟之一就是正確參數的選擇。一般來說如果有經驗的話會容易些,但無論如何,我們都得尋找。幸運的是Scikit-Learn提供了很多函數來幫助解決這個問題。
作為一個例子,我們來看一下規則化參數的選擇,在其中不少數值被相繼搜索了:
import
numpy
as
np
from
sklearn
.
linear_model
import
Ridge
from
sklearn
.
grid_search
import
GridSearchCV
# prepare a range of alpha values to test
alphas
=
np
.
array
([
1
,
0.1
,
0.01
,
0.001
,
0.0001
,
0
])
# create and fit a ridge regression model, testing each alpha
model
=
Ridge
()
grid
=
GridSearchCV
(
estimator
=
model
,
param_grid
=
dict
(
alpha
=
alphas
))
grid
.
fit
(
X
,
y
)
(
grid
)
# summarize the results of the grid search
(
grid
.
best_score_
)
(
grid
.
best_estimator_
.
alpha
)
有時候隨機地從既定的範圍內選取一個參數更為高效,估計在這個參數下演算法的質量,然後選出最好的。
import
numpy
as
np
from
scipy
.
stats
import
uniform
as
sp_rand
from
sklearn
.
linear_model
import
Ridge
from
sklearn
.
grid_search
import
RandomizedSearchCV
# prepare a uniform distribution to sample for the alpha parameter
param_grid
=
{
"alpha"
:
sp_rand
()}
# create and fit a ridge regression model, testing random alpha values
model
=
Ridge
()
rsearch
=
RandomizedSearchCV
(
estimator
=
model
,
param_distributions
=
param_grid
,
n_iter
=
100
)
rsearch
.
fit
(
X
,
y
)
(
rsearch
)
# summarize the results of the random parameter search
(
rsearch
.
best_score_
)
(
rsearch
.
best_estimator_
.
alpha
)
至此我們已經看了整個使用Scikit-Learn庫的過程,除了將結果再輸出到一個文件中。這個就作為你的一個練習吧,和R相比Python的一大優點就是它有很棒的文檔說明。
在下一篇文章中,我們將深入探討其他問題。我們尤其是要觸及一個很重要的東西——特徵的建造。我真心地希望這份材料可以幫助新手數據科學家儘快開始解決實踐中的機器學習問題。最後,我祝願那些剛剛開始參加機器學習競賽的朋友擁有耐心以及馬到成功!
看完本文有收穫?請轉發分享給更多人
關注「大數據與機器學習文摘」,成為Top 1%


※字典對象的 Pythonic 用法
※Python將是人工智慧時代的最佳編程語言
※Python 編碼錯誤的本質原因
※Airflow:Python 工作流管理利器
TAG:Python |
※機器學習基石-The Learning Problem
※基於 Python Schema 的機器學習庫——Smart Fruit
※Python 機器學習 Scikit-learn 完全入門指南
※機器學習基石-Noise and Error
※機器學習技法-lecture5:Kernel Logistic Regression
※Learning Memory Access Patterns,資料庫+機器學習探索
※微軟機器學習Machine Learning Studio學習筆記
※機器學習與Scikit Learn學習庫
※谷歌發布機器學習規則 (Rules of Machine Learning):關於機器學習工程的最佳實踐(上)
※谷歌發布機器學習規則 (Rules of Machine Learning):關於機器學習工程的最佳實踐(下)
※Facebook發布Tensor Comprehensions:自動編譯高性能機器學習核心的C+庫
※scikit-learn機器學習初體驗
※Machine Learning:十大機器學習演算法
※用AI 打造遊戲,Unity 機器學習 Agent——ml-agents
※英特爾宣布Windows機器學習Movidius Myriad X VPU
※Ethics for AI and Robotics 人工智慧與機器人的倫理學-筆記
※使用TensorFlow,Kafka和MemSQL進行實時機器學習
※Feature Tools:可自動構造機器學習特徵的Python庫
※如何使用 Android Things 和 TensorFlow 在物聯網上應用機器學習
※Leslie Valiant:機器學習所面臨的挑戰