【问题标题】:sklearn StandardScaler returns all zeros for all inputsklearn StandardScaler 为所有输入返回全零
【发布时间】:2019-11-23 12:29:20
【问题描述】:

我在一个 1 行 8 列的矩阵中设置来自用户的输入,并且特征缩放这些值,以便我可以使用矩阵中的值来预测我得到的错误是

ValueError: 找到昏暗的数组 3. StandardScaler 预期

我试过了

  matrix = q.fit_transform(matrix[:, np.newaxis]) 

但它返回 -Found array with dim 3. StandardScaler expected

 R = int(input("Enter the number of rows:")) 
 C = int(input("Enter the number of columns:")) 


 print("Enter the entries in a single line (separated by space): ") 

 entries = list(map(float, input().split())) 

 matrix = np.array(entries).reshape(R, C) 
 print(matrix)

 matrix = q.fit_transform(matrix)

我期待StandardScale 结果,但它返回

[0.000000000000000000e+00   0.000000000000000000e+00     
0.000000000000000000e+00    0.000000000000000000e+00     
0.000000000000000000e+00    0.000000000000000000e+00     
0.000000000000000000e+00    0.000000000000000000e+00]

【问题讨论】:

    标签: python-3.x numpy scikit-learn


    【解决方案1】:

    我想你误解了StandardScaler的逻辑。每个数据点都减去平均值并用标准偏差进行归一化。因此,如果数据点的数量为 1,则它变为零。

    来自Documentation

    样本x的标准分计算如下:

    z = (x - u) / s

    其中 u 是训练样本的平均值,如果 with_mean=False,s是训练的标准差 样本或如果 with_std=False 则为一个。

    因此,如果您只有一个数据点,那么标准分数将始终为零。

    from sklearn.preprocessing import StandardScaler
    data = [[1, 2, 3, 1, 3, 2, 1]]
    scaler = StandardScaler()
    print(scaler.fit_transform(data))
    
    # [[0. 0. 0. 0. 0. 0. 0.]]
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2016-08-25
      • 2014-11-11
      • 2018-01-04
      • 2017-03-17
      • 2022-01-12
      • 2019-02-17
      • 1970-01-01
      相关资源
      最近更新 更多