当前位置:网站首页>Calculate weight and comprehensive score by R entropy weight method
Calculate weight and comprehensive score by R entropy weight method
2022-07-05 15:04:00 【See the sea for the first time】
In recent work, we need to calculate intelligently according to big data , Predict or recommend supply chain payment priority / Power comes back , In the past, it was a priority to make payment manually according to factors such as supplier relationship , It cannot objectively distribute payment strategies to other suppliers , Now it is calculated objectively according to big data , Make a note of
1, get data
Get data from starRocks,
library(RMySQL)
library(tibble)
library(dplyr)
criterions_cite <- function(){
# Establishing a connection ,project_name Represents the name of the project database
conn <- dbConnect(MySQL(), dbname = 'ods', username = 'pxxx', password = 'xxx', host = 'xx.xx.xx.xx', port = 9030)
# If there is Chinese garbled in the table , You can add the following code
#dbSendQuery(conn, 'SET NAMES GBK')
#sheet_name Indicates the name of the table to be read
filter_statements <- paste0('SELECT * FROM ods_fkyc_local')
# Filtering data
res <- dbSendQuery(conn, filter_statements)
# Extract the data ,-1 It means extract all ,3 It means taking three rows of data
dat <- dbFetch(res, -1)
# close RMySQL Data set in
dbClearResult(dbListResults(conn)[[1]])
# Close the connection
dbDisconnect(conn)
return(dat)
}
# Get the data set from the database
mydt <- criterions_cite()
tb.mydt <- as_tibble(mydt)
2, Specify the positive , Negative indicators
Like sales , The larger the negative selling amount, the better it can be specified as a positive indicator
Like defective inventory , Unsalable inventory , Amount in arrears This kind of index is about as few as possible, and it is designated as a negative index
# 1, normalization , Positive indicators
min_max_norm <- function(x) {
(x - min(x)) / (max(x) - min(x))
}
# Negative indicators
max_min_norm <- function(x) {
(max(x) - x) / (max(x) - min(x))
}
# normalization , First deal with positive indicators
min_max_norm_mydt <- tb.mydt %>% mutate(across(c(9,13,14), min_max_norm))
# Then deal with the negative indicators first
max_min_norm_mydt <- min_max_norm_mydt %>% mutate(across(c(10,11,12), max_min_norm))
3, Calculate the index after normalization
p_value <- function(x){
x / sum(x)
}
p_mydt <- max_min_norm_mydt %>% mutate(across(c(9:14), p_value))
## Calculate the entropy
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 And is the weight
4, Then calculate the comprehensive score and rank , Then export
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)
Export the calculated data csv file , and streamload Into the starRocks in
边栏推荐
- How to solve the problem of garbled code when installing dependency through NPM or yarn
- Cartoon: what are the attributes of a good programmer?
- 【华为机试真题详解】字符统计及重排
- Leetcode: Shortest Word Distance II
- Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
- PHP high concurrency and large traffic solution (PHP interview theory question)
- [recruitment position] Software Engineer (full stack) - public safety direction
- Leetcode: Shortest Word Distance II
- Using tensorboard to visualize the training process in pytoch
- [detailed explanation of Huawei machine test] character statistics and rearrangement
猜你喜欢
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
实现一个博客系统----使用模板引擎技术
leetcode:881. lifeboat
Crud of MySQL
MySQL之CRUD
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
Ctfshow web entry explosion
两个BI开发,3000多张报表?如何做的到?
爱可可AI前沿推介(7.5)
随机推荐
GPS原始坐标转百度地图坐标(纯C代码)
Magic methods and usage in PHP (PHP interview theory questions)
Interview shock 62: what are the precautions for group by?
Change multiple file names with one click
Using tensorboard to visualize the training process in pytoch
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
漫画:程序员不是修电脑的!
[detailed explanation of Huawei machine test] character statistics and rearrangement
ICML 2022 | 探索语言模型的最佳架构和训练方法
FR练习题目---综合题
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
数据库学习——数据库安全性
Drive brushless DC motor based on Ti drv10970
在Pytorch中使用Tensorboard可视化训练过程
sql server学习笔记
[detailed explanation of Huawei machine test] happy weekend
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
手写promise与async await
Coding devsecops helps financial enterprises run out of digital acceleration