当前位置:网站首页>R 熵权法计算权重及综合得分
R 熵权法计算权重及综合得分
2022-07-05 14:47:00 【第一次看海】
最近工作中需要根据大数据智能计算,预测或者推荐出供应链付款优先/权重来,以往都是人工根据供应商关系等因素做付款的一个优先级,并不能客观的将付款策略分发到其他供应商,现根据大数据客观计算,记录一下
1,获取数据
获取数据从starRocks,
library(RMySQL)
library(tibble)
library(dplyr)
criterions_cite <- function(){
#建立连接,project_name表示项目数据库名称
conn <- dbConnect(MySQL(), dbname = 'ods', username = 'pxxx', password = 'xxx', host = 'xx.xx.xx.xx', port = 9030)
#如果表中有中文出现乱码,可以添加以下代码
#dbSendQuery(conn, 'SET NAMES GBK')
#sheet_name表示需要读取的表格名称
filter_statements <- paste0('SELECT * FROM ods_fkyc_local')
#过滤数据
res <- dbSendQuery(conn, filter_statements)
#提取数据,-1表示全部提取,3表示取三行数据
dat <- dbFetch(res, -1)
#关闭RMySQL中的数据集
dbClearResult(dbListResults(conn)[[1]])
#关闭连接
dbDisconnect(conn)
return(dat)
}
# 从数据库拿到数据集
mydt <- criterions_cite()
tb.mydt <- as_tibble(mydt)
2,指定指标项的正,负向指标
像销售额,负卖金额越大越好的指定为正向指标
像残次库存,滞销库存,欠收金额 这种指标约少越好指定为负向指标
# 1,归一化,正向指标
min_max_norm <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
# 负向指标
max_min_norm <- function(x) {
(max(x) - x) / (max(x) - min(x))
}
# 归一化处理,先处理正项指标
min_max_norm_mydt <- tb.mydt %>% mutate(across(c(9,13,14), min_max_norm))
# 再先处理负项指标
max_min_norm_mydt <- min_max_norm_mydt %>% mutate(across(c(10,11,12), max_min_norm))
3,归一化后计算指标
p_value <- function(x){
x / sum(x)
}
p_mydt <- max_min_norm_mydt %>% mutate(across(c(9:14), p_value))
## 计算熵值
entropy <- function(x){
n <- length(x)
(-1 / log2(n)) * (sum( x * ifelse(log2(x)==-Inf, 0, log2(x)) ))
}
e_mydt <- p_mydt %>% summarise(across(c(9:14), entropy))
e_mydt
d_mydt = 1-e_mydt
d_mydt
w = d_mydt/sum(d_mydt)
w
w及为权重
4,再计算综合得分并排名,之后导出
fscore <- function(x, y){
sum(x*y)
}
old_data <- rename(tb.mydt[,9:14],c(old_ZPO_K001_sum="ZPO_K001_sum",old_zpo_k006_sum="zpo_k006_sum",old_zx0030_sum="zx0030_sum",old_fyjj201to03_sum="fyjj201to03_sum",old_wpfm_sum="wpfm_sum",old_total_amount="total_amount"))
result_data=cbind(max_min_norm_mydt,old_data)
mydt3 <- result_data %>% group_by(1:n()) %>% mutate(score = fscore(c_across(9:14), w)) %>% arrange(-score) %>% ungroup() %>% select("com_code","organization_name","lifnr","name1","dept_no","dept_name","brand_code","brand_name","ZPO_K001_sum","zpo_k006_sum","zx0030_sum","fyjj201to03_sum","wpfm_sum","total_amount","old_ZPO_K001_sum","old_zpo_k006_sum","old_zx0030_sum","old_fyjj201to03_sum","old_wpfm_sum","old_total_amount","score")
write.table(mydt3, file = "/app/bigdata_app/data/fkyc/MyData.csv",quote = FALSE,row.names=FALSE, col.names=FALSE)
将计算完的数据导出csv文件,并streamload进starRocks中
边栏推荐
- Structure - C language
- Select sort and bubble sort
- 有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
- 两个BI开发,3000多张报表?如何做的到?
- Thymeleaf 常用函數
- 选择排序和冒泡排序
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- 启牛证券账户怎么开通,开户安全吗?
- Security analysis of Web Architecture
猜你喜欢
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
Niuke: intercepting missiles
Machine learning notes - gray wolf optimization
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
【jvm】运算指令
Section - left closed right open
MySQL----函数
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
Thymeleaf th:with use of local variables
随机推荐
MySQL----函数
How to solve the problem of garbled code when installing dependency through NPM or yarn
Thymeleaf 使用后台自定义工具类处理文本
easyOCR 字符识别
useMemo,memo,useRef等相关hooks详解
选择排序和冒泡排序
Penetration testing methodology
Install and configure Jenkins
安装配置Jenkins
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Section - left closed right open
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
GPS original coordinates to Baidu map coordinates (pure C code)
启牛证券账户怎么开通,开户安全吗?
Photoshop插件-动作相关概念-非加载执行动作文件中动作-PS插件开发
Structure - C language
在Pytorch中使用Tensorboard可视化训练过程
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Talking about how dataset and dataloader call when loading data__ getitem__ () function
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署