【问题标题】:Standardize dataset containing too large values标准化包含太大值的数据集
【发布时间】:2019-01-15 10:03:13
【问题描述】:

我使用 preprocessing.scale astype('float64') 将特征标准化为 mean=0 和 sd=1。我收到以下警告:

用户警告:数据居中时遇到数值问题 并且可能无法解决。数据集可能包含太大的值。你可以 需要预先调整您的功能。 warnings.warn("数值问题是 遇到“

这是数据集的一个示例:

    col1    col2    col3    col4    col5    col6    col7    col8    col9    col10   col11   col12   col13
0   327 143.04  123.66  101.71  89.36575914 0.668110013 84.13713837 588.103818  633.6584113 525.5505746 132.966095  13.05099964 131.7220566
1   1010    188.98  176.78  137.33  89.36575914 0.620949984 40.52060699 1413.802012 3705.255352 1641.459378 106.3353716 7.69299984  472.4249759
2   1485    166.67  141.72  111.07  98.91169739 0.979290009 100 3580.441388 4327.644518 3242.16829  111.2140427 13.05300045 1164.119187
3   78  54.27   83.01   161.74  95.0061264  0.968744297 100 35644.07894 37765.71684 15667.95157 106.3043671 7.448999882 850.651571
4   591 132.86  121.22  108.13  103.231369  1.039739966 100 9348.743837 10699.19772 7144.242782 101.7313309 8.788999557 1382.113557
5   562 134.98  141.72  141.15  89.36575914 0.968744297 100 3046.147835 3710.575743 2716.801411 106.3353716 18.26099968 1076.131188
6   1030    110.83  79.08   50.87   89.36575914 0.952409983 97.35466766 11348.70932 11928.21847 7637.253514 102.3456802 9.793620323 1164.119187
7   534 109.06  109.14  106.12  89.36575914 0.968744297 100 43007.67453 54008.70819 29971.03064 106.3353716 5.602000237 1164.119187

什么是预分频?我有什么选择这样做?

【问题讨论】:

  • 以熊猫框架的形式提供您的数据样本
  • 我通过添加数据样本来编辑问题。

标签: python scikit-learn normalization scaling


【解决方案1】:

如果你想要这个 PCA,试试这个:

 df=your data frame
 dataForPCA=preprocessing.StandardScaler().fit_transform(df.T)

它会将您的数据格式化为 PCA 所需的格式,方法是将数据格式化为 0-1,并将您的 df 转换为准备使用的 np 数组

如果您不希望 PCA 使用此功能:

 df=your data frame
 dataForPCA=preprocessing.StandardScaler().fit_transform(df)

【讨论】:

    【解决方案2】:

    将数据框转换为浮点类型对我有用:

    df = df.astype(float)
    scaled_df = preprocessing.scale(df)
    

    【讨论】:

    • 这实际上对我有用。警告消失了,因为列dtype 已从float32 转换为float64... 此处采取的步骤不是很有效,而实际上它只压缩UserWarning,因为np.allclose in code。我继续 surpressing the warning if it is float32 而不是进行这种不必要的转换。
    【解决方案3】:

    我使用 StandardScaler 解决了这个问题,并按照here 的建议以以下代码为例:

    from sklearn import preprocessing
    # Get column names first
    names = df.columns
    # Create the Scaler object
    scaler = preprocessing.StandardScaler()
    # Fit your data on the scaler object
    scaled_df = scaler.fit_transform(df)
    scaled_df = pd.DataFrame(scaled_df, columns=names)
    

    【讨论】:

    • 您能否解释一下为什么在使用 StandardScaler 时问题不存在?
    • 你是在 train_test_split 之前,在原始数据帧上做这个,还是应该在每个新的 X,y 集上拆分之后做这个?
    • 我之前猜想,拆分数据后扩展没有意义
    猜你喜欢
    • 2020-09-08
    • 2021-06-18
    • 2014-06-12
    • 2019-10-22
    • 1970-01-01
    • 2023-03-09
    • 2022-01-11
    • 2020-08-26
    • 1970-01-01
    相关资源
    最近更新 更多