Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sklearn.kbins所有箱子的离散化器返回0_Python_Scikit Learn - Fatal编程技术网

Python sklearn.kbins所有箱子的离散化器返回0

Python sklearn.kbins所有箱子的离散化器返回0,python,scikit-learn,Python,Scikit Learn,我有一个纳瑞形状(1188)。尝试使用KBinsDiscretizer创建箱子,但它只返回带零注释的箱子 # transform the dataset with KBinsDiscretizer enc = KBinsDiscretizer(n_bins=18, encode='ordinal' ,strategy='uniform')#strategy='uniorm' test =(np.asarray(list_event_MES).astype(np.float))

我有一个纳瑞形状(1188)。尝试使用KBinsDiscretizer创建箱子,但它只返回带零注释的箱子

 # transform the dataset with KBinsDiscretizer
    enc = KBinsDiscretizer(n_bins=18, encode='ordinal' ,strategy='uniform')#strategy='uniorm'

    test =(np.asarray(list_event_MES).astype(np.float)).reshape(1, -1)
    print(test)
    print(test.shape)

    enc.fit(test)
    test2 = enc.transform(test)
    print(test2.tolist())
将为所有箱子返回零

矩阵输入: [[0.13614053 0.14069501 0.08270327 0.26015096 0.15958708 0.16834299 0.14913976 0.11897561 0.23232807 0.0892398 0.1637264 0.17120459 0.19350733 0.18131615 0.20117186 0.1586006 0.19068352 0.24293008 . .... 0.2112216 0.21829195 0.28169516 0.27585681 0.27317305 0.1849694 0.23402622 0.24994829 0.20873297 0.25534803 0.15556027 0.27226802 0 0.14180543 0.24001428]]

形状: (1188)

188列的警告: /miniconda3/lib/python3.7/site packages/sklearn/preprocessing/_离散化。py:159:UserWarning:Feature 0是常量,将替换为0。 “替换为0。”%jj) /miniconda3/lib/python3.7/site packages/sklearn/preprocessing/_离散化。py:159:UserWarning:Feature 1是常量,将替换为0。 “替换为0。”%jj) /miniconda3/lib/python3.7/site packages/sklearn/preprocessing/_离散化。py:159:UserWarning:Feature 2是常量,将替换为0。 “替换为0。”%jj)


从数组的形状
(1188)
,我们可以推断只有
1
样本和
188
特征。根据
KBinsDiscretizer
的说明,它用于将连续数据分为多个区间,并发生在特征级别,即对于每个特征(或数据的每列),
KBinsDiscretizer
计算bin区间,然后将数据分为多个区间,示例如下所示:

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)  

Xt = est.transform(X)
Xt  

array([[ 0., 0., 0., 0.],
      ​[ 1., 1., 1., 0.],
      ​[ 2., 2., 2., 1.],
      ​[ 2., 2., 2., 2.] 
enc = KBinsDiscretizer(n_bins=18, encode='ordinal' ,strategy='uniform')
list_event_MES = np.random.normal(0,2,188).reshape(-1,1)
test =(np.asarray(list_event_MES))
print(test.shape)
(188,1)

enc.fit(test)
test2 = enc.transform(test)

print(test2[0:5])

array([[12.],
   [12.],
   [ 7.],
   [ 9.],
   [ 3.]])
在这里,对于每一列,离散化程序计算存储单元间隔并存储它们。在您的例子中,每个特性只有一个数据点,因此计算垃圾箱没有任何意义。相反,如果您的数据的形状为
(188,1)
,即带有
188
示例和
1
功能,则其工作状态完美,如下所示:

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)  

Xt = est.transform(X)
Xt  

array([[ 0., 0., 0., 0.],
      ​[ 1., 1., 1., 0.],
      ​[ 2., 2., 2., 1.],
      ​[ 2., 2., 2., 2.] 
enc = KBinsDiscretizer(n_bins=18, encode='ordinal' ,strategy='uniform')
list_event_MES = np.random.normal(0,2,188).reshape(-1,1)
test =(np.asarray(list_event_MES))
print(test.shape)
(188,1)

enc.fit(test)
test2 = enc.transform(test)

print(test2[0:5])

array([[12.],
   [12.],
   [ 7.],
   [ 9.],
   [ 3.]])

希望这有帮助

您好@ZheFrench,如果您觉得下面的答案是合适的,请将其标记为已验证的答案,以帮助将来访问此网站的其他人。