scikit-learn庫 安裝須要numpy,pandas等庫算法
特徵抽取對文本數據進行特徵值化,方便計算機去理解數據。ide
字典特徵抽取:對字典數據進行特徵值化大數據
# Author:song from sklearn.feature_extraction import DictVectorizer def dictvec(): """字典數據抽取""" dict_vec = DictVectorizer(sparse=False) data = dict_vec.fit_transform([{'city':'A市','num':100},{'city':'D市','num':100},{'city':'B市','num':80},{'city':'C市','num':56}]) print(data)#sparse矩陣 print(dict_vec.get_feature_names()) print(dict_vec.inverse_transform(data)) return None if __name__ =="__main__": dictvec() 結果: [[ 1. 0. 0. 0. 100.] [ 0. 0. 0. 1. 100.] [ 0. 1. 0. 0. 80.] [ 0. 0. 1. 0. 56.]] ['city=A市', 'city=B市', 'city=C市', 'city=D市', 'num'] [{'num': 100.0, 'city=A市': 1.0}, {'city=D市': 1.0, 'num': 100.0}, {'city=B市': 1.0, 'num': 80.0}, {'city=C市': 1.0, 'num': 56.0}]
文本特徵抽取:對文本數據進行特徵值化。spa
統計全部文章當中全部的次,重複的只看作一次(詞的列表)code
對每篇文章,在詞的列表裏面進行統計每一個詞次數,單個字母不統計(由於單字母不能體現主題)。component
對於中文的特徵值化,須要先分詞處理(下載jieba, jieba.cut(‘文本內容’))orm
from sklearn.feature_extraction.text import CountVectorizer def contentvec(): """字典數據抽取""" con_vec = CountVectorizer() data = con_vec.fit_transform({'If the day is done ,','If birds sing no more .','If the wind has fiagged tired '}) print(data.toarray()) print(con_vec.get_feature_names()) return None if __name__ =="__main__": contentvec() 結果 [[0 1 1 0 0 1 1 0 0 0 1 0 0] [1 0 0 0 0 1 0 1 1 1 0 0 0] [0 0 0 1 1 1 0 0 0 0 1 1 1]] ['birds', 'day', 'done', 'fiagged', 'has', 'if', 'is', 'more', 'no', 'sing', 'the', 'tired', 'wind']
tf - idf主要思想:若是某個詞或短語在一篇文章出現頻率高,而且其餘文章不多出現,則認爲此詞或者短語具備很好的類別區分能力,適合用來分類。 做用是用以評估一字詞對於一個文件集或者一個語料庫中的其中一份文件的重要程度。blog
from sklearn.feature_extraction.text import TfidfVectorizer def tfvec(): """字典數據抽取""" tf_vec = TfidfVectorizer(stop_words=None) data = tf_vec.fit_transform({'If the day is done ,','If birds sing no more .','If the wind has fiagged tired '}) print(data.toarray()) print(tf_vec.get_feature_names()) return None if __name__ =="__main__": tfvec() 結果: [[ 0. 0. 0. 0.45050407 0.45050407 0.26607496 0. 0. 0. 0. 0.34261996 0.45050407 0.45050407] [ 0. 0.50461134 0.50461134 0. 0. 0.29803159 0.50461134 0. 0. 0. 0.38376993 0. 0. ] [ 0.47952794 0. 0. 0. 0. 0.28321692 0. 0.47952794 0.47952794 0.47952794 0. 0. 0. ]] ['birds', 'day', 'done', 'fiagged', 'has', 'if', 'is', 'more', 'no', 'sing', 'the', 'tired', 'wind']
數值型數據:標準縮放,一、歸一化;二、標準化;三、缺失值ip
歸一化特色:經過對原始數據進行轉化把數據映射到(0,1)之間。目的是爲了讓某個特徵對最終結果不會形成影響(若是求結果的數據有好幾個特徵對結果影響程度相同)ci
公式: 在特定的場景下最大值和最小值是變化的,另外最大值與最小值很是容易受異常點影響,因此這種方法魯棒性較差。
from sklearn.preprocessing import MinMaxScaler def mm(): mm = MinMaxScaler(feature_range=(0,1)) #參數限定區間 data = mm.fit_transform([[90,2,10,40],[60,5,15,20],[80,3,12,30]]) print(data) return None if __name__=="__main__": mm() 結果 [[ 1. 0. 0. 1. ] [ 0. 1. 1. 0. ] [ 0.66666667 0.33333333 0.4 0.5 ]]
標準化,使用最普遍,特色處理以後每列的全部數據都彙集在均值爲0附近標準差爲1,在已有樣本比較多的狀況下比較穩定,適合大數據場景。
公式: µ爲平均值,σ爲標準差。
from sklearn.preprocessing import StandardScaler def stand(): std = StandardScaler() data = std.fit_transform([[1,-1,3],[2,3,2],[4,5,6]]) print(data) return None if __name__=="__main__": stand() 結果: [[-1.06904497 -1.33630621 -0.39223227] [-0.26726124 0.26726124 -0.98058068] [ 1.33630621 1.06904497 1.37281295]]
缺失值通常使用pandas處理,按照行值來填補。
數據降維(此處的維度表明的是特徵的數量)
特徵選擇
方法一:filter(過濾式)
from sklearn.feature_selection import VarianceThreshold def var(): var = VarianceThreshold(threshold=0)#刪除方差爲0的 data = var.fit_transform([[0,2,0,3],[0,1,4,3],[0,1,1,3]]) print(data) return None if __name__=="__main__": var() 結果: [[2 0] [1 4] [1 1]]
方式二:嵌入式,正則決策
PCA(主成分分析)是一種分析簡化數據集的技術,維數壓縮,特徵達到上百時候。
from sklearn.decomposition import PCA def pca(): pca = PCA(n_components=0.9)#小數表明保留數據百分比,整數表示保留幾個特徵 data = pca.fit_transform([[5,2,0,3],[9,1,4,3],[5,1,1,3]]) print(data) return None if __name__=='__main__': pca() 結果: [[-2.16802239] [ 3.55798483] [-1.38996267]]