当前位置:网站首页>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中
边栏推荐
- Reconnaissance des caractères easycr
- Detailed explanation of usememo, memo, useref and other relevant hooks
- leetcode:881. lifeboat
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- 裁员下的上海
- Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
- Explain Vue's plan to clean up keepalive cache in time
- webRTC SDP mslabel lable
- 微帧科技荣获全球云计算大会“云鼎奖”!
猜你喜欢

SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain

Topology visual drawing engine

Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

Super wow fast row, you are worth learning!

Under the crisis of enterprise development, is digital transformation the future savior of enterprises

Thymeleaf 使用后台自定义工具类处理文本

CPU design related notes

两个BI开发,3000多张报表?如何做的到?

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
随机推荐
Structure - C language
leetcode:881. lifeboat
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
webRTC SDP mslabel lable
Fonctions communes de thymeleaf
Disjoint Set
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
TS所有dom元素的类型声明
Select sort and bubble sort
Share 20 strange JS expressions and see how many correct answers you can get
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
webRTC SDP mslabel lable
Easyocr character recognition
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
Thymeleaf 使用后台自定义工具类处理文本
Isn't it right to put money into the external market? How can we ensure safety?
Niuke: intercepting missiles