当前位置:网站首页>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中
边栏推荐
- PHP - fatal error: allowed memory size of 314572800 bytes exhausted
- Machine learning notes - gray wolf optimization
- Fonctions communes de thymeleaf
- 【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
- GPS original coordinates to Baidu map coordinates (pure C code)
- Penetration testing methodology
- Explain Vue's plan to clean up keepalive cache in time
- 如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
- CPU design related notes
- 机器学习框架简述
猜你喜欢
Coding devsecops helps financial enterprises run out of digital acceleration
选择排序和冒泡排序
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
MongDB学习笔记
leetcode:881. 救生艇
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
Talking about how dataset and dataloader call when loading data__ getitem__ () function
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
随机推荐
Topology visual drawing engine
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Section - left closed right open
Coding devsecops helps financial enterprises run out of digital acceleration
Easyocr character recognition
MySQL----函数
基于TI DRV10970驱动直流无刷电机
Handwriting promise and async await
Isn't it right to put money into the external market? How can we ensure safety?
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Stm32+bh1750 photosensitive sensor obtains light intensity
【招聘岗位】基础设施软件开发人员
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Drive brushless DC motor based on Ti drv10970
【华为机试真题详解】欢乐的周末
easyOCR 字符識別
Implement a blog system -- using template engine technology
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
Machine learning notes - gray wolf optimization