KBINS离散化料仓边缘

2024-03-29 05:54:46 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有人知道是否必须解释KBinsDiscretizer提供的箱子边缘? 由于它使用numpy linspace进行统一的装箱,并且默认值为endpoint=True,所以装箱应该包括最右边的边。那么我如何用大于和小于符号来写呢

以下是一个例子:

from sklearn.datasets import load_iris
from sklearn.preprocessing import KBinsDiscretizer
iris_data = load_iris()
x = iris_data.data
# binning of first feature
est = KBinsDiscretizer(n_bins=3, encode='onehot-dense', strategy='uniform')
x1 = est.fit_transform(x[:,0].reshape(-1, 1))
bin_edges = est.bin_edges_ 

箱子边缘为[4.3,5.5,6.7,7.9]。这样写对了吗

  1. 银行标识代码:4.3<;=x<;5.5
  2. 银行标识代码:5.5<;=x<;6.7
  3. 银行标识代码:6.7<;=x<;=7.9

Tags: 代码fromimportltirisdataload银行
1条回答
网友
1楼 · 发布于 2024-03-29 05:54:46

使用np.linspace定义边,但是如果查看source code行303,则使用np.digitize后跟np.clip来控制最右边的存储单元来完成赋值:

for jj in range(Xt.shape[1]):
   rtol = 1.e-5
   atol = 1.e-8
   eps = atol + rtol * np.abs(Xt[:, jj])
   Xt[:, jj] = np.digitize(Xt[:, jj] + eps, bin_edges[jj][1:])
np.clip(Xt, 0, self.n_bins_ - 1, out=Xt)

np.digitize的defaultright=False,因此如果应用于此数据,您的BIN基本上是正确的。您可以检查边界:

test = np.array([4.3,5.5,6.7,7.9]).reshape(-1,1)

est.transform(test)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 1.]])

您应该注意,如果您的值超出了bin边缘,它们将自动指定给边界bin,请参见np.digitize。这意味着,我们尝试使用越界值4.1和8.1:

test = np.array([4.1,4.3,7.9,8.1]).reshape(-1,1)
est.transform(test)

array([[1., 0., 0.],
       [1., 0., 0.],
       [0., 0., 1.],
       [0., 0., 1.]])

它们分别被分配到第一个和最后一个箱子。严格来说,垃圾箱是:

1. bin: x < 5.5,
2. bin: 5.5 <= x < 6.7,
3. bin: 6.7 <= x 

相关问题 更多 >