当前位置:网站首页>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

原网站

版权声明
本文为[See the sea for the first time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051446123131.html