非平衡数据会影响最后的评判效果,严重的会带来过拟合的效果,即模型总是把样本划分到样本量较多的那一种。为了让模型的评判更准确,我们需要对非平衡数据进行一定的处理,主要有以下几种方式:
在开始介绍不同的处理方式之前,我们先引入一组非平衡数据。
#导入一些相关库
del_selection import train_test_split
from sklearn.linear_model import LogisticRegression
ics import classification_report
ics import roc_curve, auc
from sklearn.preprocessing import scale
#导入数据
ad_excel(r"C:UserszhangjunhongDesktopUnbanlanced-data.xlsx").fillna(0)
看一下正负样本的具体数据量情况。
x=df.iloc[:,1:-1]
y=df["label"]
print(y.value_counts())
print("-------------------------")
print(y.value_counts(normalize=True))
该数据量的正负样本比例接近7:3,我们看一下不做任何处理的情况下,模型的预测效果如何。
#将模型进行封装,方便调用
def get_result_data(x,y):
x_=scale(x,with_mean=True,with_std=True)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.4,random_state=0)
model=LogisticRegression()
clf=model.fit(x_train,y_train)
print("LR模型测试成绩:{:.2f}".format(clf.score(x_test,y_test)))
y_p
本文发布于:2024-01-28 19:30:06,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064414079742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |