某公司的网站建设的资金预算书,wordpress 改为根目录,免费空间 上传网站,网络营销的三大基础1、概述 逻辑回归本身是一种分类算法#xff0c;它并不涉及下采样或过采样操作。然而#xff0c;在处理不平衡数据集时#xff0c;这些技术经常被用来改善模型的性能。下采样和过采样是两种常用的处理不平衡数据集的方法。
2、下采样 1、概念 下采样是通过减少数量较多的类…1、概述 逻辑回归本身是一种分类算法它并不涉及下采样或过采样操作。然而在处理不平衡数据集时这些技术经常被用来改善模型的性能。下采样和过采样是两种常用的处理不平衡数据集的方法。
2、下采样 1、概念 下采样是通过减少数量较多的类别多数类的样本数量使其与数量较少的类别少数类的样本数量相匹配或接近。这样可以使模型在训练时不会偏向于多数类。 2、原理 随机选择一些多数类的样本并从数据集中移除只保留与少数类样本数量相等的样本。可以导致数据集的信息丢失特别是当多数类样本被大量移除时。
3、案例 从0中找到和1的数目相同的数据 代码
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
datapd.read_csv(creditcard.csv)
#StandardScaler用于数据标准化
from sklearn.preprocessing import StandardScaler
scalerStandardScaler()
# 对Amount列进行标准化处理
data[Amount]scaler.fit_transform(data[[Amount]])
datadata.drop([Time],axis1)#从完整数据集中找到和n_eg数目相同的p_eg进行lianj
p_egdata[data[Class]0]
n_egdata[data[Class]1]
np.random.seed(seed4)
p_egp_eg.sample(len(n_eg))
data_cpd.concat([p_eg,n_eg])from sklearn.model_selection import train_test_split
xdata.drop(Class,axis1)
ydata[Class]
# 随机分割训练集和测试集
x_train,x_test,y_train,y_testtrain_test_split(x,y,test_size0.3,random_state0)
#小数据集的训练集特征与标签测试集特征与标签
mdata_c.drop(Class,axis1)
ndata_c[Class]
m_train,m_test,n_train,n_testtrain_test_split(m,n,test_size0.2,random_state0)#交叉验证小数据集
from sklearn.model_selection import cross_val_score
scores[]
c_param_range[0.01,0.1,1,10,100]
for i in c_param_range:lrLogisticRegression(Ci,penaltyl2,solverlbfgs,max_iter1000)scorecross_val_score(lr,m_train,n_train,cv8,scoringrecall)score_meansum(score)/len(score)scores.append(score_mean)
#选择最合适的C重新训练
best_cc_param_range[np.argmax(scores)]
lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000)
lr.fit(m_train,n_train)from sklearn import metrics
#小数据集的训练集
train_predictedlr.predict(m_train)
print(metrics.classification_report(n_train,train_predicted))
#小数据集的测试集
test_predictedlr.predict(m_test)
print(metrics.classification_report(n_test,test_predicted))#完整数据集的训练集
data_x_train_predictedlr.predict(x_train)
print(metrics.classification_report(y_train,data_x_train_predicted))
#完整数据集的测试集
data_x_test_predictedlr.predict(x_test)
print(metrics.classification_report(y_test,data_x_test_predicted))thresh[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
recalls[]
for i in thresh:y_predict_probalr.predict_proba(x_test)y_predict_probapd.DataFrame(y_predict_proba).drop([0],axis1)y_predict_proba[y_predict_proba[[1]]i]1y_predict_proba[y_predict_proba[[1]]i]0recallmetrics.recall_score(y_test,y_predict_proba[1])recalls.append(recall)print(i,recall)
4、过采样 1、概念 过采样是通过增加数量较少的类别少数类的样本数量使其与数量较多的类别多数类的样本数量相匹配或超过。这可以通过复制现有样本或生成新的合成样本来实现。 2、原理 复制简单地复制少数类的样本直到其数量与多数类相等。 合成样本使用算法如SMOTESynthetic Minority Over-sampling Technique生成新的合成样本而不是简单地复制现有样本。SMOTE通过在特征空间中插值来创建新的少数类样本。
5、案例 将原数据分成训练集和测试集训练集进行过采样获得两倍大小的新的训练集 代码
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
datapd.read_csv(creditcard.csv)#StandardScaler用于数据标准化
from sklearn.preprocessing import StandardScaler
scalerStandardScaler()
# 对Amount列进行标准化处理
data[Amount]scaler.fit_transform(data[[Amount]])
datadata.drop([Time],axis1)#随机抽取
# 准备数据集分割特征和标签
from sklearn.model_selection import train_test_split
xdata.drop(Class,axis1)
ydata[Class]
# 随机分割训练集和测试集
x_train,x_test,y_train,y_testtrain_test_split(x,y,test_size0.3,random_state0)#对训练集进行过采样
from imblearn.over_sampling import SMOTE
oversamplesSMOTE(random_state0)
os_x_train,os_y_trainoversamples.fit_resample(x_train,y_train)
# 随机分割训练集和测试集
os_x_train_w,os_x_test_w,os_y_train_w,os_y_test_wtrain_test_split(os_x_train,os_y_train,test_size0.3,random_state0)#交叉验证
from sklearn.model_selection import cross_val_score
scores[]
c_param_range[0.01,0.1,1,10,100]
for i in c_param_range:lrLogisticRegression(Ci,penaltyl2,solverlbfgs,max_iter1000)scorecross_val_score(lr,os_x_train_w,os_y_train_w,cv8,scoringrecall)score_meansum(score)/len(score)scores.append(score_mean)
# 选择平均召回率最高的C值
best_cc_param_range[np.argmax(scores)]
lrLogisticRegression(Cbest_c,penaltyl2,max_iter1000)
lr.fit(os_x_train_w,os_y_train_w)
from sklearn import metrics# 打印分类报告
os_train_predictedlr.predict(os_x_train_w)
print(metrics.classification_report(os_y_train_w,os_train_predicted))os_test_predictedlr.predict(os_x_test_w)
print(metrics.classification_report(os_y_test_w,os_test_predicted))train_predictedlr.predict(x_train)
print(metrics.classification_report(y_train,train_predicted))test_predictedlr.predict(x_test)
print(metrics.classification_report(y_test,test_predicted))thresh[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
recalls[]
for i in thresh:y_predict_probalr.predict_proba(x_test)y_predict_probapd.DataFrame(y_predict_proba).drop([0],axis1)y_predict_proba[y_predict_proba[[1]]i]1y_predict_proba[y_predict_proba[[1]]i]0recallmetrics.recall_score(y_test,y_predict_proba[1])recalls.append(recall)print(i,recall)
文章转载自: http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.bpmfl.cn.gov.cn.bpmfl.cn http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn http://www.morning.hmfxl.cn.gov.cn.hmfxl.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.qklff.cn.gov.cn.qklff.cn http://www.morning.zkdbx.cn.gov.cn.zkdbx.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.fywqr.cn.gov.cn.fywqr.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.trjr.cn.gov.cn.trjr.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.fdjwl.cn.gov.cn.fdjwl.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.khdw.cn.gov.cn.khdw.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.dsmwy.cn.gov.cn.dsmwy.cn