日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scikit-Learn机器学习入门

發布時間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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机器学习入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。