Python機器學習庫scikit-learn安裝與基本使用教程
本文實例講述了Python機器學習庫scikit-learn安裝與基本使用。分享給大家供大家參考,具體如下:
引言
scikit-learn是Python的一個開源機器學習模塊,它建立在NumPy
,SciPy
和matplotlib
模塊之上能夠為用戶提供各種機器學習算法接口,可以讓用戶簡單、高效地進行數(shù)據(jù)挖掘和數(shù)據(jù)分析。
scikit-learn安裝
python 中安裝許多模板庫之前都有依賴關系,安裝 scikit-learn 之前需要以下先決條件:
Python(>= 2.6 or >= 3.3)
NumPy (>= 1.6.1)
SciPy (>= 0.9)
如無意外,下面用 pip 的安裝方法可以順利完成~~
安裝 numpy
sudo pip install numpy
安裝 scipy
需要先安裝 matplotlib ipython ipython-notebook pandas sympy
sudo apt-get install python-matplotlib ipython ipython-notebook sudo apt-get install python-pandas python-sympy python-nose sudo pip install scipy
安裝 scikit-learn
sudo pip install -U scikit-learn
測試
在 terminal 里面輸入
pip list
這個會列出 pip 安裝的所有東西,如果里面有 sklearn 這一項,應該就是大功告成了!
或者嘗試著將幾個模板庫導入進來
import numpy import scipy import sklearn
加載數(shù)據(jù)(Data Loading)
本文所使用的數(shù)據(jù)集為‘今日頭條'近期兩篇熱門新聞“牛!川大學霸寢室5人獲16份名校通知書”、“張超凡的最后14天:山西15歲休學少年是如何殞命網(wǎng)吧的”分別500條評論,共1000條評論。
去除停用詞后得到了詞庫大小為3992的詞庫。因此構(gòu)建了1000×3992的特征矩陣,以及長度為1000的對應評論所屬類別列表
class_result_save.npy 下載 feature_matrix_save.npy下載
import numpy as np feature_matrix = np.load('dataSet/feature_matrix_save.npy') class_list = np.load('dataSet/class_result_save.npy')
數(shù)據(jù)歸一化(Data Normalization)
大多數(shù)機器學習算法中的梯度方法對于數(shù)據(jù)的縮放和尺度都是很敏感的,在開始跑算法之前,我們應該進行歸一化或者標準化的過程,這使得特征數(shù)據(jù)縮放到0-1范圍中。scikit-learn提供了歸一化的方法:
from sklearn import preprocessing # 歸一化(Normalization) normalized_X = preprocessing.normalize(feature_matrix) print normalized_X # 標準化(Standardization) standardized_X = preprocessing.scale(feature_matrix) print standardized_X
特征選擇(Feature Selection)
在解決一個實際問題的過程中,選擇合適的特征或者構(gòu)建特征的能力特別重要。這成為特征選擇或者特征工程。
特征選擇時一個很需要創(chuàng)造力的過程,更多的依賴于直覺和專業(yè)知識,并且有很多現(xiàn)成的算法來進行特征的選擇。
下面的樹算法(Tree algorithms)計算特征的信息量:
from sklearn.ensemble import ExtraTreesClassifier model = ExtraTreesClassifier() print feature_matrix.shape # 原特征矩陣規(guī)模 feature_matrix = model.fit(feature_matrix, class_list).transform(feature_matrix) print feature_matrix.shape # 特征選擇后 特征矩陣的規(guī)模
特征提取(Feature Extraction)
用TFIDF算法來計算特征詞的權重值是表示當一個詞在這篇文檔中出現(xiàn)的頻率越高,同時在其他文檔中出現(xiàn)的次數(shù)越少,則表明該詞對于表示這篇文檔的區(qū)分能力越強,所以其權重值就應該越大。
from sklearn.feature_extraction.text import TfidfTransformer tfidf_transformer = TfidfTransformer() feature_matrix = tfidf_transformer.fit_transform(feature_matrix).toarray()
樸素貝葉斯(Naive Bayes)
樸素貝葉斯是一個很著名的機器學習算法,主要是根據(jù)訓練樣本的特征來計算各個類別的概率,在多分類問題上用的比較多。
from sklearn import metrics from sklearn.naive_bayes import GaussianNB # 構(gòu)建樸素貝葉斯模型 model = GaussianNB() model.fit(feature_matrix, class_list) print model # 使用測試集進行測試(此處將訓練集做測試集) expected = class_list predicted = model.predict(feature_matrix) # 輸出測試效果 print metrics.classification_report(expected, predicted) print metrics.confusion_matrix(expected, predicted)
k近鄰(k-Nearest Neighbours)
k近鄰算法常常被用作是分類算法一部分,比如可以用它來評估特征,在特征選擇上我們可以用到它。
from sklearn import metrics from sklearn.neighbors import KNeighborsClassifier # 構(gòu)建knn模型 model = KNeighborsClassifier() model.fit(feature_matrix, class_list) print model # 使用測試集進行測試(此處將訓練集做測試集) expected = class_list predicted = model.predict(feature_matrix) # 輸出測試效果 print metrics.classification_report(expected, predicted) print metrics.confusion_matrix(expected, predicted)
決策樹(Decision Tree)
分類與回歸樹(Classification and Regression Trees ,CART)算法常用于特征含有類別信息的分類或者回歸問題,這種方法非常適用于多分類情況。
from sklearn import metrics from sklearn.tree import DecisionTreeClassifier # 構(gòu)建決策數(shù)模型 model = DecisionTreeClassifier() model.fit(feature_matrix, class_list) print model # 使用測試集進行測試(此處將訓練集做測試集) expected = class_list predicted = model.predict(feature_matrix) # 輸出測試效果 print metrics.classification_report(expected, predicted) print metrics.confusion_matrix(expected, predicted)
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python?GUI利用tkinter皮膚ttkbootstrap實現(xiàn)好看的窗口
這篇文章主要介紹了Python?GUI利用tkinter皮膚ttkbootstrap實現(xiàn)好看的窗口,文章基于python的相關資料展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06python利用opencv如何實現(xiàn)答題卡自動判卷
由于工作需要,最近在研究關于如何通過程序識別答題卡的客觀題的答案,所以下面這篇文章主要介紹了python利用opencv如何實現(xiàn)答題卡自動判卷的相關資料,需要的朋友可以參考下2021-08-08對Python 多線程統(tǒng)計所有csv文件的行數(shù)方法詳解
今天小編就為大家分享一篇對Python 多線程統(tǒng)計所有csv文件的行數(shù)方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02