当前位置:网站首页>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中
边栏推荐
- Share 20 strange JS expressions and see how many correct answers you can get
- Shanghai under layoffs
- Install and configure Jenkins
- Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
- How to paste the contents copied by the computer into mobaxterm? How to copy and paste
- 两个BI开发,3000多张报表?如何做的到?
- Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
- Jmeter性能测试:ServerAgent资源监控
- 漫画:程序员不是修电脑的!
- Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
猜你喜欢

如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴

Pointer operation - C language
![[detailed explanation of Huawei machine test] character statistics and rearrangement](/img/0f/972cde8c749e7b53159c9d9975c9f5.png)
[detailed explanation of Huawei machine test] character statistics and rearrangement

leetcode:881. 救生艇

Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel

Thymeleaf th:with use of local variables

FR练习题目---综合题

How to choose the appropriate certificate brand when applying for code signing certificate?

Topology可视化绘图引擎

【NVMe2.0b 14-9】NVMe SR-IOV
随机推荐
[detailed explanation of Huawei machine test] happy weekend
Type declaration of all DOM elements in TS
Pointer operation - C language
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Section - left closed right open
想进阿里必须啃透的12道MySQL面试题
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
漫画:优秀的程序员具备哪些属性?
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
【招聘岗位】软件工程师(全栈)- 公共安全方向
FR练习题目---综合题
CODING DevSecOps 助力金融企业跑出数字加速度
Matrix chain multiplication dynamic programming example
Structure - C language
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
leetcode:881. lifeboat
Longest common subsequence dynamic programming
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)