【发布时间】:2020-04-15 08:33:56
【问题描述】:
我正在尝试将基线模型应用于我的数据集。但是数据集是不平衡的,只有 11% 的数据属于正类。我在没有采样的情况下拆分数据,正面记录的召回率非常低。我想在不平衡测试数据的情况下平衡训练数据(0.5 负 0.5 正)。有谁知道这是怎么做到的吗?
#splitting train and test data
train,test = train_test_split(coupon,test_size = 0.3,random_state = 100)
##separating dependent and independent variables
cols = [i for i in coupon.columns if i not in target_col]
train_X = train[cols]
train_Y = train[target_col]
test_X = test[cols]
test_Y = test[target_col]
#Function attributes
#dataframe - processed dataframe
#Algorithm - Algorithm used
#training_x - predictor variables dataframe(training)
#testing_x - predictor variables dataframe(testing)
#training_y - target variable(training)
#training_y - target variable(testing)
#cf - ["coefficients","features"](cooefficients for logistic
#regression,features for tree based models)
#threshold_plot - if True returns threshold plot for model
def coupon_use_prediction(algorithm,training_x,testing_x,
training_y,testing_y,cols,cf,threshold_plot) :
#model
algorithm.fit(training_x,training_y)
predictions = algorithm.predict(testing_x)
probabilities = algorithm.predict_proba(testing_x)
#coeffs
if cf == "coefficients" :
coefficients = pd.DataFrame(algorithm.coef_.ravel())
elif cf == "features" :
coefficients = pd.DataFrame(algorithm.feature_importances_)
column_df = pd.DataFrame(cols)
coef_sumry = (pd.merge(coefficients,column_df,left_index= True,
right_index= True, how = "left"))
coef_sumry.columns = ["coefficients","features"]
coef_sumry = coef_sumry.sort_values(by = "coefficients",ascending = False)
print (algorithm)
print ("\n Classification report : \n",classification_report(testing_y,predictions))
print ("Accuracy Score : ",accuracy_score(testing_y,predictions))
【问题讨论】:
-
没有。你正在解决一个 XY 问题。你有不平衡的数据,现在你试图平衡训练步骤的数据,并在测试步骤保持不平衡,以试图“解决”问题。不要只是盲目地更改数据,您要阅读有关如何正确处理不平衡数据的信息。 (其中一些实际上会稍微调整训练数据与测试数据的比率,但还没有达到 50-50 的程度。还有很多其他的事情你可以也应该尝试,包括改变你的指标,如果算法支持,则添加权重,等等)。
-
参考:XY Problem。你的 X 是:如何在给定不平衡数据的情况下训练一个好的模型。你的 Y 听起来像:我如何简单地使数据在训练时间上成比例,同时保持测试数据不变。
-
我明白了。你说的对。我想处理不平衡的数据。我将搜索有关此的更多信息。谢谢!
标签: python machine-learning baseline