当我的特征变量大部分为零时我该怎么办?

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

我有一组商店销售数据,我想利用外部 POI 特征及其人口统计因素来预测其他商店的销售情况。然而,我的特征变量几乎 80% 为零,其余 20% 有不同的范围。导致所有特征都高度倾斜。

我得到了一个较低的 r 平方值,我已经尝试过随机森林、XGBOOST 以及猫增强回归。

python machine-learning random-forest data-cleaning feature-engineering
1个回答
0
投票

您可以尝试分层抽样,这是一种随机抽样方法,将总体划分为各个子组(“层”),并从每个子组中抽取随机样本。

Pandas 有一个函数可以让你将数据分割成任意数量的分位数。例如,如果您想按销售额进行分层,您可以执行以下操作:

from sklearn.model_selection import train_test_split

# Assuming 'df' is your DataFrame and 'sales' is your target variable
# Create a categorical variable to serve as your strata
df['strata'] = pd.qcut(df['sales'], q=10, labels=False)

此行在 DataFrame 中创建一个名为“strata”的新列。

pd.qcut
函数用于将
sales
列分为 10 个分位数。这意味着它会对销售值进行排序并将其分为 10 个大小相等的容器。

然后,sklearn

train_test_split()
函数有一个分层参数,可让您指定要分层的变量:

# Perform stratified sampling
train_set, test_set = train_test_split(df, test_size=0.2, random_state=42, stratify=df['strata'])

之后,您可以删除临时的“strata”列:

# Remove the 'strata' column
train_set = train_set.drop('strata', axis=1)
test_set = test_set.drop('strata', axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.