如何使用 python scikit-learn 执行欠采样(正确的方法)?
How to perform undersampling (the right way) with python scikit-learn?
我正在尝试使用 python scikit learn 对大多数 class 进行欠采样。目前我的代码寻找少数 class 的 N,然后尝试从多数 class 中抽取完全相同的 N。结果,测试数据和训练数据都具有 1:1 分布。但我真正想要的是仅在训练数据上执行此 1:1 分布,但在测试数据的原始分布上对其进行测试。
我不太确定如何做后者,因为两者之间有一些字典矢量化,这让我很困惑。
# 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)
您想要对其中一个类别的训练样本进行子采样,因为您想要一个对所有标签都一视同仁的分类器。
如果你想这样做而不是子采样,你可以将分类器的 'class_weight' 参数的值更改为 'balanced'(或 'auto' 对于某些分类器),它会执行你想做的工作。
您可以阅读 LogisticRegression 分类器的文档作为示例。注意 'class_weight' 参数的描述 here.
通过将该参数更改为 'balanced',您将不再需要进行子采样。
我正在尝试使用 python scikit learn 对大多数 class 进行欠采样。目前我的代码寻找少数 class 的 N,然后尝试从多数 class 中抽取完全相同的 N。结果,测试数据和训练数据都具有 1:1 分布。但我真正想要的是仅在训练数据上执行此 1:1 分布,但在测试数据的原始分布上对其进行测试。
我不太确定如何做后者,因为两者之间有一些字典矢量化,这让我很困惑。
# 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)
您想要对其中一个类别的训练样本进行子采样,因为您想要一个对所有标签都一视同仁的分类器。
如果你想这样做而不是子采样,你可以将分类器的 'class_weight' 参数的值更改为 'balanced'(或 'auto' 对于某些分类器),它会执行你想做的工作。
您可以阅读 LogisticRegression 分类器的文档作为示例。注意 'class_weight' 参数的描述 here.
通过将该参数更改为 'balanced',您将不再需要进行子采样。