大学做网站,新手做网站视频,怎么做二级网站域名,顺义成都网站建设补充#xff1a;关于“加权模型” 
“加权模型”指的是在训练机器学习模型时#xff0c;对不同类别或样本赋予不同的权重#xff0c;让模型在学习过程中更加关注某些类别或样本。 
常见场景#xff1a; 
当数据集类别不平衡#xff08;比如正类很少#xff0c;负类很多关于“加权模型” 
“加权模型”指的是在训练机器学习模型时对不同类别或样本赋予不同的权重让模型在学习过程中更加关注某些类别或样本。 
常见场景 
当数据集类别不平衡比如正类很少负类很多时模型容易忽略少数类。通过给少数类更高的权重模型在训练时会“更重视”少数类提高其被正确识别的概率。 
举例 在随机森林、逻辑回归等模型中可以通过参数 class_weightbalanced 自动为少数类分配更高权重。 
代码实现 
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from sklearn.metrics import recall_score# 训练集类别统计
import numpy as np
unique, counts  np.unique(y_train, return_countsTrue)
print(训练集中各类别数量:, counts)
minority_class  unique[np.argmin(counts)]
majority_class  unique[np.argmax(counts)]
print(f少数类标签: {minority_class}, 多数类标签: {majority_class})# 带权重的随机森林
rf_weighted  RandomForestClassifier(class_weightbalanced, random_state42)# 交叉验证
print(开始进行 5 折交叉验证...)
cv_scores  cross_val_score(rf_weighted, X_train, y_train, cv5, scoringaccuracy)
print(f\n带权重随机森林 交叉验证平均性能 (基于训练集划分)\n  平均 accuracy: {cv_scores.mean():.4f} (/- {cv_scores.std():.4f}))# 在测试集上评估少数类召回率
rf_weighted.fit(X_train, y_train)
y_pred_weighted  rf_weighted.predict(X_test)
recall_weighted  recall_score(y_test, y_pred_weighted, pos_labelminority_class)
print(f带权重模型: {recall_weighted:.4f}) 
代码注解 
np.unique(y_train, return_countsTrue)统计y_train中每个类别的标签及其出现次数。unique保存所有不同的类别标签如[0, 1]。 
【.unique() 是 Pandas 和 NumPy 中常用的方法用于返回某个数组或序列中所有不重复的元素。 在 Pandas 中 data[列名].unique() 返回该列中所有不同的取值结果是一个数组。  在 NumPy 中 np.unique(array) 返回数组中所有唯一值并默认排序。用途常用于查看某一列有多少种类别、去重、统计类别分布等。】 counts保存每个类别对应的样本数量如[4328, 1672]。np.argmin(counts)找到counts中最小值的索引即样本最少的类别。minority_class保存少数类的标签。np.argmax(counts)找到counts中最大值的索引即样本最多的类别。majority_class保存多数类的标签。class_weightbalanced自动根据各类别样本数量分配权重样本少的类别权重高样本多的类别权重低帮助模型更关注少数类缓解类别不平衡问题。cross_val_score 是 sklearn 的交叉验证评分函数。X_train, y_train 是训练数据和标签。cv_scores 保存了每一折的准确率分数共5个。recall_score 是 sklearn 的召回率计算函数。pos_labelminority_class 指定“正类”为少数类标签计算模型对少数类的识别能力。 
手写笔记复习“随机过采样”部分 今日复习到这里明日接着复习SMOTE。继续加油浙大疏锦行  文章转载自: http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.mfct.cn.gov.cn.mfct.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.frnjm.cn.gov.cn.frnjm.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.rrhfy.cn.gov.cn.rrhfy.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.qbgff.cn.gov.cn.qbgff.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.wnqbf.cn.gov.cn.wnqbf.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.ghphp.cn.gov.cn.ghphp.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.dwdjj.cn.gov.cn.dwdjj.cn http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.lgtcg.cn.gov.cn.lgtcg.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn