【问题标题】:Apply the quartile cuts from the train data to the test data将训练数据的四分位数切割应用于测试数据
【发布时间】:2019-05-15 04:13:36
【问题描述】:

是否有任何现有的 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)。

【问题讨论】:

标签: python pandas python-2.7 numpy scikit-learn


【解决方案1】:

当您 pd.qcut 训练集时,您可以使用 retbins 参数。使用 pd.cut 将这些 bin 用于您的测试集,修改下限和上限,以便您可以包含所有内容。

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

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2020-02-17
    • 2021-03-14
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多