python基础(逻辑回归例题)

一、参数选择

在逻辑回归建模中,"过拟合"是绕不开的坑------当模型在训练数据上表现完美,却在新数据上一塌糊涂时,大概率是模型复杂度超出了数据所能支撑的范围。而惩罚因子(也叫正则化参数),正是我们解决过拟合、平衡模型拟合度与泛化能力的核心工具。

1.通过LogisticRegression(C=i,penalty='l2',solver='lbfgs',max_iter=1000)函数C参数的选择最优惩罚因子。通过K折交叉验证cross_val_score(lr,x_train_w,y_train_w,cv=8,scoring='recall')函数来实现。

python 复制代码
scores=[]  #不同参数下的验证集评分
c_range=[0.01,0.1,1,10,100]
for i in c_range:
    lr=LogisticRegression(C=i,penalty='l2',solver='lbfgs',max_iter=1000)
    score=cross_val_score(lr,x_train_w,y_train_w,cv=8,scoring='recall')
    score_m=sum(score)/len(score)
    scores.append(score_m)
    print(score_m)

best_c=c_range[np.argmax(scores)]
print("最优惩罚因子",best_c)


lr=LogisticRegression(C=best_c,penalty='l2',solver='lbfgs',max_iter=1000)
lr.fit(x_train_w,y_train_w)

from sklearn import metrics
train_predict=lr.predict(x_train_w)
print(metrics.classification_report(y_train_w,train_predict))#获得混淆矩阵的准确值,召回值。
cm_plot(y_train_w,train_predict).show()


test_predict=lr.predict(x_test_w)
print(metrics.classification_report(y_test_w,test_predict,digits=6))#获得混淆矩阵的准确值,召回值。
cm_plot(y_test_w,test_predict).show()

二、下采样

下采样的核心是"削减多数类样本",将不同类别的数量平衡一下,减少多的类别的数量

在案例中使用代码:

x_train_w=train_datatrain_data\['Class'==1]

y_train_w=train_datatrain_data\['Class'==0]

y_train_w=y_train_w.sample(len(x_train_w))

使用sample函数从y_train_w中抽取x_train_w的数量。

python 复制代码
data=pd.read_csv("creditcard.csv")

scaler=StandardScaler()
data['Amount']=scaler.fit_transform(data[['Amount']])
data=data.drop(['Time'],axis=1)#axis=1,表示删除列

x=data.drop('Class',axis=1)
y=data.Class

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

train_data=x_train
train_data['Class']=y_train

x_train_w=train_data[train_data['Class']==1]
y_train_w=train_data[train_data['Class']==0]
y_train_w=y_train_w.sample(len(x_train_w))

data_c=pd.concat([x_train_w,y_train_w])

x_train_w_1=data_c.drop('Class',axis=1)
y_train_w_1=data_c.Class


scores=[]
c_range=[0.01,0.1,1,10,100]
for i in c_range:
    lr=LogisticRegression(C=i,penalty='l2',solver='lbfgs',max_iter=1000)
    score=cross_val_score(lr,x_train_w_1,y_train_w_1,cv=10,scoring='recall')
    score_m=sum(score)/len(score)
    scores.append(score_m)
    print(score_m)

best_c=c_range[np.argmax(scores)]
print("最优因子:",best_c)

lr=LogisticRegression(C=best_c,penalty='l2',solver='lbfgs',max_iter=1000)
lr.fit(x_train_w_1,y_train_w_1)

三、过采样

上采样的核心是"扩充少数类样本"

我们可以使用SMOTE(合成少数类过采样技术)------在少数类样本的特征空间中,找到每个样本的k个近邻,通过插值生成新的少数类样本(如样本A和样本B的近邻,新样本=A+rand(0,1)*(B-A))

python 复制代码
from imblearn.over_sampling import SMOTE
oversampler=SMOTE(random_state=100)#保证数据拟合效果,随机种子
os_x_train,os_y_train=oversampler.fit_resample(x_train,y_train)#人工拟合数据
相关推荐
CoderYanger2 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)
java·开发语言·数据库·spring boot·后端·spring·java-ee
星恒随风6 小时前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
AI创界者7 小时前
Python 进阶实战(七十一章):研发效能度量、自动化质量门禁与工程规范落地实战
人工智能·python·aigc
yingjie1107 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
Felven8 小时前
C. Make Them Equal
c语言·开发语言·前端
tryCbest8 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
风落青芜10 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
Resean022310 小时前
讲透Java(14)泛型的本质:类型擦除与桥接方法
java·开发语言
@陈小鱼10 小时前
基于InceptionTime网络的PPG无创血压估计算法研究
人工智能·python·算法·回归·开源·ppg·血压估计
谙弆悕博士11 小时前
动态自适应多智能体架构的 LangGraph 实现:从元调度器到可运行原型
人工智能·python·神经网络·机器学习·架构·多智能体·智能体