np.unique

问题描述 投票:0回答:1

此代码片段描述了我一直遇到的问题。由于某种原因

rounded_data
似乎是圆角的,但是一旦传入
np.unique
np.column_stack
result_array
似乎是不圆角的,同时
rounded_data
仍然是圆角的。

rounded_data = data_with_target_label.round(decimals=2)

unique_values, counts = np.unique(rounded_data, return_counts=True)
result_array = np.column_stack((unique_values, counts))

print(rounded_data)
print(result_array)

结果:

443392    0.01
443393    0.00
443394    0.00
443395    0.00
443396    0.11
          ... 
452237    0.04
452238    0.00
452239    0.00
452240    0.00
452241    0.00
Name: values, Length: 8850, dtype: float32
[[0.00000000e+00 4.80000000e+01]
 [9.99999978e-03 2.10000000e+01]
 [1.99999996e-02 1.10000000e+01]
 ...
 [3.29000015e+01 1.00000000e+00]
 [3.94099998e+01 1.00000000e+00]
python numpy
1个回答
0
投票

这是因为你的数据框是

float32
,而 numpy 中的默认数字格式是
float64
。因此,在 float32 中舍入的数字在 float64 中不会明显舍入,因为数字表示形式有点不同。 解决方案是将输入数组转换为 float32 或将 result_array 转换为 float 32。

rounded_data = data_with_target_label.round(decimals=2)

unique_values, counts = np.unique(rounded_data, return_counts=True)
result_array = np.column_stack((unique_values, counts))

result_array = np.float32(result_array)
© www.soinside.com 2019 - 2024. All rights reserved.