【发布时间】:2016-04-22 06:19:15
【问题描述】:
我正在尝试使用 python scikit learn 对多数类进行欠采样。目前我的代码寻找少数类的 N,然后尝试从多数类中对完全相同的 N 进行欠采样。结果,测试和训练数据都具有这种 1:1 的分布。但我真正想要的是仅在训练数据上进行这种 1:1 分布,但在测试数据中的原始分布上进行测试。
我不太确定如何做后者,因为两者之间有一些 dict 矢量化,这让我感到困惑。
# Perform undersampling majority group
minorityN = len(df[df.ethnicity_scan == 1]) # get the total count of low-frequency group
minority_indices = df[df.ethnicity_scan == 1].index
minority_sample = df.loc[minority_indices]
majority_indices = df[df.ethnicity_scan == 0].index
random_indices = np.random.choice(majority_indices, minorityN, replace=False) # use the low-frequency group count to randomly sample from high-frequency group
majority_sample = data.loc[random_indices]
merged_sample = pd.concat([minority_sample, majority_sample], ignore_index=True) # merging all the low-frequency group sample and the new (randomly selected) high-frequency sample together
df = merged_sample
print 'Total N after undersampling:', len(df)
# Declaring variables
X = df.raw_f1.values
X2 = df.f2.values
X3 = df.f3.values
X4 = df.f4.values
y = df.outcome.values
# Codes skipped ....
def feature_noNeighborLoc(locString):
pass
my_dict16 = [{'location': feature_noNeighborLoc(feature_full_name(i))} for i in X4]
# Codes skipped ....
# Dict vectorization
all_dict = []
for i in range(0, len(my_dict)):
temp_dict = dict(
my_dict[i].items() + my_dict2[i].items() + my_dict3[i].items() + my_dict4[i].items()
+ my_dict5[i].items() + my_dict6[i].items() + my_dict7[i].items() + my_dict8[i].items()
+ my_dict9[i].items() + my_dict10[i].items()
+ my_dict11[i].items() + my_dict12[i].items() + my_dict13[i].items() + my_dict14[i].items()
+ my_dict19[i].items()
+ my_dict16[i].items() # location feature
)
all_dict.append(temp_dict)
newX = dv.fit_transform(all_dict)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(newX, y, test_size=testTrainSplit)
# Fitting X and y into model, using training data
classifierUsed2.fit(X_train, y_train)
# Making predictions using trained data
y_train_predictions = classifierUsed2.predict(X_train)
y_test_predictions = classifierUsed2.predict(X_test)
【问题讨论】:
-
看来你的缩进是错误的。 Ad rem:不确定在最后一行使用您想要的测试数据有什么问题。
-
缩进已更正。原始分布是说 20:1 多数:少数类。我的代码使得测试和训练数据都是 1:1 多数:少数。一些 ML 顾问建议我在训练集中应采用 1:1 的比例,但在测试集中保留原来的 20:1 比例。
-
@您只是在重复您在问题中所说的话。是什么阻止您在测试阶段使用 20:1 比率的数据?顺便提一句。你的 for 循环没有任何意义。
-
循环工作得很好,我没有发布我的整个代码,因为它将是 600 行,所以我需要削减一些东西。我展示了这一点来说明 dict 矢量化过程。我不确定我是否理解你的第一个问题。我已经在另一个代码集中进行了 20:1 的测试和训练,但我没有包含欠采样代码。如果您问是什么阻止了我从上述代码中测试 20:1 测试数据,那么我不再拥有它了。 df = merge_sample 是在欠采样之后,以及以下各项:X_train、X_test、y_train、y_test 派生自此 df。
-
我在循环之前添加了一些示例代码来说明这里的逻辑。
标签: python-2.7 machine-learning scikit-learn sampling downsampling