無量綱化,是指特徵的規格不一樣,不能夠放到一起進行比較。
包
類引數列表
類別fit方法有用
說明sklearn.preprocessing
standardscaler
特徵無監督
y標準化
sklearn.preprocessing
minmaxscaler
特徵無監督
y區間縮放
sklearn.preprocessing
normalizer
特徵無資訊
n歸一化
sklearn.preprocessing
binarizer
特徵無資訊
n定量特徵二值化
sklearn.preprocessing
onehotencoder
特徵無監督
y定性特徵編碼
sklearn.preprocessing
imputer
特徵無監督
y缺失值計算
sklearn.preprocessing
polynomialfeatures
特徵無資訊
n多項式變換(fit方法僅僅生成了多項式的表示式)
sklearn.preprocessing
functiontransformer
特徵無資訊
n自定義函式變換(自定義函式在transform方法中呼叫)
sklearn.feature_selection
variancethreshold
特徵無監督
y方差選擇法
sklearn.feature_selection
selectkbest
特徵/特徵+目標值
無監督/有監督
y自定義特徵評分選擇法
sklearn.feature_selection
selectkbest+chi2
特徵+目標值
有監督y
卡方檢驗選擇法
sklearn.feature_selection
rfe特徵+目標值
有監督y
遞迴特徵消除法
sklearn.feature_selection
selectfrommodel
特徵+目標值
有監督y
自定義模型訓練選擇法
sklearn.decomposition
pca特徵
無監督y
pca降維
sklearn.lda
lda特徵+目標值
有監督y
lda降維
#標準化
from sklearn.preprocessing import standardscaler
standardscaler(
).fit_transform(iris.data)
#區間縮放法
from sklearn.preprocessing import minmaxscaler
minmaxscaler(
).fit_transform(iris.data)
#歸一化
from sklearn.preprocessing import normalizer
normalizer(
).fit_transform(iris.data)
#定量二值法
from sklearn.preprocessing import binarizer
binarizer(threshold=3)
.fit_transform(iris.data)
#啞編碼
from sklearn.preprocessing import onehotencoder
onehotencoder(
).fit_transform(iris.target.reshape((-
1,1)
))from numpy import vstack, array, nan
from sklearn.preprocessing import imputer
#缺失值計算,返回值為計算缺失值後的資料
#引數missing_value為缺失值的表示形式,預設為nan
#引數strategy為缺失值填充方式,預設為mean(均值)
imputer(
).fit_transform(vstack(
(array(
[nan, nan, nan, nan]
), iris.data)
))
# 多項式變化
from sklearn.preprocessing import polynomialfeatures
polynomialfeatures(
).fit_transform(data)
# 單元函式變換
from numpy import log1p
from sklearn.preprocessing import functiontransformer
functiontransformer(log1p)
.fit_transform(data)
判斷特徵是否發散:如果方差為0,那麼在這個特徵上基本無差異,無法區分類別
特徵與目標相關性:相關性高,優先選擇
embedded: 通過機器學習演算法和模型進行訓練,得到特徵的權值係數,確定特徵優劣
#方差選擇法
from sklearn.feature_selection import variancethreshold
varancethreshold(threhold=3)
.fit_transform(data)
# 相關係數法
from sklearn.feature_selection import selectkbest
from scipy.stats import pearsonr #皮爾公尺係數
selectkbest(
lambda x, y: array(
map(
lambda x:pearsonr(x, y)
, x.t)
).t, k=2)
.fit_transform(iris.data, iris.target)
#x每一列和y求p值得到最好的k列
# 卡方檢測
from sklearn.feature_selection import selectkbest,chi2
selectkbest(chi2,k=2)
.fit_transform(data,target)
#互資訊法--
--from sklearn.feature_selection import selectkbest
from scipy.stats import pearsonr
#選擇k個最好的特徵,返回選擇特徵後的資料
#第乙個引數為計算評估特徵是否好的函式,該函式輸入特徵矩陣和目標向量,輸出二元組(評分,p值)的陣列,陣列第i項為第i個特徵的評分和p值。在此定義為計算相關係數
#引數k為選擇的特徵個數
selectkbest(
lambda x, y: array(
map(
lambda x:pearsonr(x, y)
, x.t)
).t, k=2)
.fit_transform(iris.data, iris.target)--
--
from sklearn.feature_selection import rfe
from sklearn.linear_model import logisticregression
#遞迴特徵消除法,返回特徵選擇後的資料
#引數estimator為基模型
#引數n_features_to_select為選擇的特徵個數
rfe(estimator=logisticregression(
), n_features_to_select=2)
.fit_transform(iris.data, iris.target)
嵌入式通過預訓練模型,從而選出主要特徵。同時l1泛化能夠起到特徵選擇的作用。後期補充嵌入式各種模型的使用方法。
from sklearn.linear_model import logisticregression
lr = logisticregression(penalty=
"l1"
,c=0.1
)from sklearn.feature_selection import selectfrommodel
x_l1 = selectfrommodel(estimator = lr)
.fit_transform(x,y)
包含pca降維和lda降維,pca只和x值相關,是通過矩陣變換得到的;
lda 線性判別法,通過x與y之間的關係來降維。
from sklearn.decomposition import pca
pca(2)
.fit_transform(data)
from sklearn.lad import lda
lda(2)
.fit_transfrom(data,target)
機器學習特徵值特徵抽取
根據文字的的特徵值,進行特徵值的抽取 from sklearn.feature extraction import dictvectorizer from sklearn.feature extraction.text import countvectorizer def countvec 對文字進...
機器學習 資料預處理(特徵值轉化)
我們知道,mnist資料集中的樣本特徵是從0 255的灰度值,0表示白,而255表示黑,中間的數值代表不同深度的灰色。通過除以255的操作,我們可以把所有的特徵值限定到0 1之間,從而有利於模型計算,提高模型的準確率,這就是一種簡單的資料預處理 data preprocessing 資料預處理的方法...
機器學習4 特徵向量與特徵值
a為n階矩陣,若數 和n維非0列向量x滿足ax x,那麼數 稱為a的特徵值,x稱為a的對應於特徵值 的特徵向量。式ax x也可寫成 a e x 0,並且 e a 叫做a 的特徵多項式。當特徵多項式等於0的時候,稱為a的特徵方程,特徵方程是乙個齊次線性方程組,求解特徵值的過程其實就是求解特徵方程的解。...