Scikit-Learn机器学习入门
生活随笔
收集整理的這篇文章主要介紹了
Scikit-Learn机器学习入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
現在最常用的數據分析的編程語言為R和Python。每種語言都有自己的特點,Python因為Scikit-Learn庫贏得了優勢。Scikit-Learn有完整的文檔,并實現很多機器學習算法,而每種算法使用的接口幾乎相同,可以非常快的測試其它學習算法。
Pandas一般和Scikit-Learn配合使用,它是基于Numpy構建的含有更高級數據結構和工具的數據統計工具,可以把它當成excel。
加載數據
首先把數據加載到內存。下載UCI數據集:
| 1 2 3 4 5 6 7 8 9 10 11 | import numpy as np import urllib # 數據集的url url = "http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data" # 下載文件 raw_data = urllib.urlopen(url) # 把數據加載到numpy matrix dataset = np.loadtxt(raw_data, delimiter=",") # 分離數據集 X = dataset[:,0:7]??# 屬性集 y = dataset[:,8]????# 標簽 |
?
數據標準化
在開始應用學習算法之前,應首先對數據執行標準化,這是為了確保特征值的范圍在0-1。對數據進行預處理:
| 1 2 3 4 5 | from sklearn import preprocessing # normalize normalized_X = preprocessing.normalize(X) # standardize standardized_X = preprocessing.scale(X) |
?
分類
ExtraTreesClassifier(基于樹):
| 1 2 3 4 5 6 | from sklearn import metrics from sklearn.ensemble import ExtraTreesClassifier model = ExtraTreesClassifier() model.fit(X, y) # 顯示屬性的相對重要性 print(model.feature_importances_) |
LogisticRegression:
| 1 2 3 4 5 6 7 8 9 | from sklearn.feature_selection import RFE from sklearn.linear_model import LogisticRegression model = LogisticRegression() rfe = RFE(model, 3) rfe = rfe.fit(X, y) print(rfe.support_) print(rfe.ranking_) |
?
機器學習算法
Logistic?regression
通常用來解決分類問題(binary),但是也支持多個分類。這個算法會給出屬于某一分類的概率:
| 1 2 3 4 5 6 7 8 9 10 11 | from sklearn import metrics from sklearn.linear_model import LogisticRegression model = LogisticRegression() model.fit(X, y) print(model) # 做預測 expected = y predicted = model.predict(X) # 輸出 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) |
樸素貝葉斯-Naive Bayes
這也是廣為人知的機器學習算法,用來學習數據分布的密度,在多分類問題中可以提供高質量的預測結果。
| 1 2 3 4 5 6 7 8 9 10 11 | from sklearn import metrics from sklearn.naive_bayes import GaussianNB model = GaussianNB() model.fit(X, y) print(model) # 預測 expected = y predicted = model.predict(X) # 結果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) |
KNN算法(K-Nearest?Neighbours)
- 使用Python實現K-Nearest Neighbor算法
它通常用在更復雜分類算法的一部分,它在回歸問題中可以提供很好的結果。
決策樹-Decision Trees
能很好的處理回歸和分類問題。
| 1 2 3 4 5 6 7 8 9 10 11 12 | from sklearn import metrics from sklearn.tree import DecisionTreeClassifier # fit a CART model to the data model = DecisionTreeClassifier() model.fit(X, y) print(model) # 預測 expected = y predicted = model.predict(X) # 結果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) |
支持向量機-Support Vector Machines
- 使用Python實現Support Vector Machine算法
?
| 1 2 3 4 5 6 7 8 9 10 11 12 | from sklearn import metrics from sklearn.svm import SVC # fit a SVM model to the data model = SVC() model.fit(X, y) print(model) # 預測 expected = y predicted = model.predict(X) # 結果 print(metrics.classification_report(expected, predicted)) print(metrics.confusion_matrix(expected, predicted)) |
Scikit-Learn還提供了一堆更復雜的算法,包括clustering,Bagging?和?Boosting。
?
轉載于:https://www.cnblogs.com/gejuncheng/p/8127446.html
總結
以上是生活随笔為你收集整理的Scikit-Learn机器学习入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch中cluste
- 下一篇: java IO(一):File类