sklearn.preprocessing.KBinsDiscretizer?
class sklearn.preprocessing.KBinsDiscretizer(n_bins=5, *, encode='onehot', strategy='quantile')
將連續數據分成間隔。
在用戶指南中閱讀更多內容。
0.20版中的新功能。
參數 | 說明 |
---|---|
n_bins | int or array-like, shape (n_features,) (default=5) 產生的箱數。如果n_bins <2,則引發ValueError。 |
encode | {‘onehot’, ‘onehot-dense’, ‘ordinal’}, (default=’onehot’) 用于編碼轉換結果的方法。 onehot 使用一鍵編碼對轉換后的結果進行編碼,然后返回一個稀疏矩陣。忽略的要素總是堆疊在右側。 onehot-dense 用一個熱編碼對轉換后的結果進行編碼,并返回一個密集數組。忽略的要素總是堆疊在右側。 ordinal 返回編碼為整數值的bin標識符。 |
strategy | {‘uniform’, ‘quantile’, ‘kmeans’}, (default=’quantile’) 用于定義箱子寬度的策略。 uniform 每個功能中的所有箱子具有相同的寬度。 quantile 每個功能中的所有存儲箱都具有相同的點數。 kmeans 每個bin中的值都具有一維k均值聚類的最接近中心。 |
屬性 | 說明 |
---|---|
n_bins | int array, shape (n_features,) 每個功能的箱數。寬度過小(即 <= 1e-8)的箱將被移除,并發出警告。 |
bin_edges_ | array of arrays, shape (n_features, ) 每個箱子的邊緣。包含不同形狀的數組(n_bin_2;,),忽略的功能將具有空數組。 |
另見:
sklearn.preprocessing.Binarizer
用于根據參數閾值將值歸為0或1的類。
注釋
在特征i的bin邊緣中,第一個和最后一個值僅用于inverse_transform。在變換過程中,bin邊將擴展到:
np.concatenate([-np.inf, bin_edges_[i][1:-1], np.inf])
如果只想預處理部分功能,則可以將KBinsDiscretizer與sklearn.compose.ColumnTransformer
結合使用。
KBinsDiscretizer可能會產生不變的特征(例如,當encode ='onehot'并且某些bin不包含任何數據時)。
這些特征可以通過特征選擇算法刪除(例如, sklearn.feature_selection.VarianceThreshold
.
示例
>>> X = [[-2, 1, -4, -1],
... [-1, 2, -3, -0.5],
... [ 0, 3, -2, 0.5],
... [ 1, 4, -1, 2]]
>>> est = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
>>> est.fit(X)
KBinsDiscretizer(...)
>>> Xt = est.transform(X)
>>> Xt # doctest: +SKIP
array([[ 0., 0., 0., 0.],
[ 1., 1., 1., 0.],
[ 2., 2., 2., 1.],
[ 2., 2., 2., 2.]])
有時將數據轉換回原始特征空間可能很有用。Inverse_transform函數將合并的數據轉換為原始特征空間。每個值將等于兩個面元邊緣的平均值。
>>> est.bin_edges_[0]
array([-2., -1., 0., 1.])
>>> est.inverse_transform(Xt)
array([[-1.5, 1.5, -3.5, -0.5],
[-0.5, 2.5, -2.5, -0.5],
[ 0.5, 3.5, -1.5, 0.5],
[ 0.5, 3.5, -1.5, 1.5]])
方法
方法 | 說明 |
---|---|
fit (self, X[, y]) |
擬合估計量。 |
fit_transform (self, X[, y]) |
擬合數據,然后對其進行轉換。 |
get_params (self[, deep]) |
獲取此估計量的參數。 |
inverse_transform (self, Xt) |
將離散化的數據轉換回原始特征空間。 |
set_params (self, **params) |
設置此估算器的參數。 |
transform (self, X) |
離散化數據。 |
__init__(self, n_bins=5, *, encode='onehot', strategy='quantile')
初始化self,有關準確的簽名,請參見help(type(self))。
fit(self, X, y=None)
擬合估計量。
參數 | 說明 |
---|---|
X | numeric array-like, shape (n_samples, n_features) 要離散化的數據。 |
y | None 忽略。僅存在此參數是為了與 sklearn.pipeline.Pipeline 兼容。 |
返回值 | 說明 |
---|---|
self | - |
fit_transform(self, X, y=None, **fit_params)
擬合數據,然后對其進行轉換。
使用可選參數fit_params將轉換器擬合到X和y,并返回X的轉換版本。
參數 | 說明 |
---|---|
X | {array-like, sparse matrix, dataframe} of shape (n_samples, n_features) |
y | ndarray of shape (n_samples,), default=None 目標值。 |
**fit_params | dict 其他擬合參數。 |
返回值 | 說明 |
---|---|
X_new | ndarray array of shape (n_samples, n_features_new) 轉換后的數組。 |
get_params(self, deep=True
獲取此估計量的參數。
參數 | 說明 |
---|---|
deep | bool, default = True 如果為True,則將返回此估算器和作為估算器的所包含子對象的參數。 |
返回值 | 說明 |
---|---|
params | mapping of string to any 參數名被映射至他們的值 |
inverse_transform(self, Xt)
將離散化的數據轉換回原始特征空間。
注意,由于離散化舍入,此函數不會重新生成原始數據。
參數 | 說明 |
---|---|
Xt | numeric array-like, shape (n_sample, n_features) 合并空間中的轉換數據。 |
返回值 | 說明 |
---|---|
Xinv | numeric array-like 原始要素空間中的數據。 |
set_params(self, **params)
設置此估算器的參數。
該方法適用于簡單的估計器以及嵌套對象(例如管道)。后者的參數形式為<component>__<parameter>這樣就可以更新嵌套對象的每個組件。
參數 | 說明 |
---|---|
**params | dict 估算器參數。 |
返回值 | 說明 |
---|---|
self | object 估算器實例。 |
transform(self, X)
離散化數據。
參數 | 說明 |
---|---|
Xt | numeric array-like, shape (n_sample, n_features) 要離散化的數據。 |
返回值 | 說明 |
---|---|
Xt | numeric array-like or sparse matrix 合并空間中的數據。 |