機器學習演算法的使用以及實踐到應用
機器學習演算法的使用
scikit-learn實現了機器學習的大部分基礎演算法,讓我們快速了解一下。
1.邏輯回歸大多數問題都可以歸結為二元分類問題。這個演算法的優點是可以給出數據所在類別的概
率。
from sklearn import metrics
from sklearn.linear_model import LogisticRegression model = LogisticRegression()
model.fit(X, y)
print(model)
# make predictions
expected = y
predicted = model.predict(X)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))
結果:
2.樸素貝葉斯這也是著名的機器學習演算法,該方法的任務是還原訓練樣本數據的分布密度,其在多類
別分類中有很好的效果。
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
model.fit(X, y)
print(model)
# make predictions
expected = y
predicted = model.predict(X)
# summarize the fit of the model print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))
結果:
3.k近鄰
k近鄰演算法常常被用作是分類演算法一部分,比如可以用它來評估特徵,在特徵選擇上我
們可以用到它。
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier
# fit a k-nearest neighbor model to the data
model = KNeighborsClassifier()
model.fit(X, y)
print(model)
# make predictions
expected = y
predicted = model.predict(X)
# summarize the fit of the model print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))
結果:
4.決策樹
分類與回歸樹(Classification and Regression Trees ,CART)演算法常用於特徵含有類
別信息的分類或者回歸問題,這種方法非常適用於多分類情況。
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
# fit a CART model to the data
model = DecisionTreeClassifier()
model.fit(X, y)
print(model)
# make predictions
expected = y
predicted = model.predict(X)
# summarize the fit of the model print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))
結果:
5.支持向量機
SVM是非常流行的機器學習演算法,主要用於分類問題,如同邏輯回歸問題,它可以使用
一對多的方法進行多類別的分類。
from sklearn import metrics
from sklearn.svm import SVC
# fit a SVM model to the data
model = SVC()
model.fit(X, y)
print(model)
# make predictions
expected = y
predicted = model.predict(X)
# summarize the fit of the model print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted))
結果:
除了分類和回歸演算法外,scikit-learn提供了更加複雜的演算法,比如聚類演算法,還實 現了演算法組合的技術,如Bagging和Boosting演算法。
※機器學習開發者應該收藏的 DIY 計算機視覺和深度學習項目
※機器學習演算法知識圖譜
TAG:機器學習 |