是否有任何现有的 python 函数可以从训练数据中获取四分位切割并将其应用于测试数据。
import pandas as pd
import numpy as np
d = {'col1': np.arange(1, 100, 1)}
train = pd.DataFrame(data=d)
d1 = {'col1': np.arange(1, 200, 2)}
test = pd.DataFrame(data = d1)
我在训练和测试中都有大约 1000 列。是否可以使用 pandas qcut 函数使其具有可扩展性,或者是否有任何其他现有的 sklearn 函数?
我希望根据火车上的箱子获得测试数据的四分位数(1、2、3 或 4)。
最佳答案
当您pd.qcut
训练集时,您可以使用retbins
参数。通过 pd.cut
将这些容器用于您的测试集,修改下限和上限,以便您可以包含所有内容。
import numpy as np
import pandas as pd
_, bins = pd.qcut(train.col1, q=4, retbins=True)
bins = np.concatenate(([-np.inf], bins[1:-1], [np.inf]))
# How many elements in each bin for the test set?
test.groupby(pd.cut(test.col1, bins)).size()
#col1
#(-inf, 25.5] 13
#(25.5, 50.0] 12
#(50.0, 74.5] 12
#(74.5, inf] 63
#dtype: int64
关于python - 将训练数据的四分位数切割应用于测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53770433/