import java.util.*;
public class Main{
public static void main(String[]args){
//数据初始化,用下面两个模拟两列属性值
ArrayList<Double> values=new ArrayList<>();
values.add(100.0);
values.add(99.0);
values.add(98.0);
values.add(101.0);
ArrayList<Double> values_2=new ArrayList<>();
values_2.add(77.0);
values_2.add(36.0);
values_2.add(48.0);
values_2.add(27.0);
System.out.println("=====调用熵权法");
entropyHelper(values,values_2);
System.out.println("=====调用变异系数法");
CVPF(values,values_2);
System.out.println("=====调用离差法");
MDPF(values,values_2);
}
public static void MDPF(ArrayList<Double> values,ArrayList<Double> values_2){
double fenmu1=MDPFHelper(values);
double fenmu2=MDPFHelper(values_2);
double fenzi=fenmu1+fenmu2;
double w1=fenmu1/fenzi;
double w2=fenmu2/fenzi;
System.out.println("属性1的权重为:"+w1+",属性2的权重为:"+w2);
}
//离差法
public static double MDPFHelper(ArrayList<Double> values){
//先数据归一化
double max= Collections.max(values);
double min=Collections.min(values);
ArrayList<Double> xij= new ArrayList<>();
for(double num: values){
double temp=(num-min)/(max-min);
xij.add(temp);
}
//计算分母
double fenmu1=0.0;
for(double num:xij){
for(double num2:xij)
fenmu1+=Math.abs(num-num2);
}
return fenmu1;
}
public static void CVPF(ArrayList<Double> values,ArrayList<Double> values_2){
double V1=CVPFHelper(values);
double V2=CVPFHelper(values_2);
double w1=V1/(V1+V2);
double w2=V2/(V1+V2);
System.out.println("属性1的权重为:"+w1+",属性2的权重为:"+w2);
}
//变异系数法
public static double CVPFHelper(ArrayList<Double> values){
//计算平均值
double X1=0.0;
double sum=0.0;
for(double num: values){
sum+=num;
}
X1=sum/4;
//计算Sj
double S1=0.0;
sum=0.0;
for(double num:values){
sum+=Math.pow((num-X1),2);
}
sum=0.25*sum;
S1=Math.sqrt(sum);
double V1=S1/X1;
return V1;
}
//调用熵权法的函数
public static void entropyHelper(ArrayList<Double> values, ArrayList<Double> values_2){
double e1=entropyMethod(values);
double e2=entropyMethod(values_2);
double w1=(1-e1)/(2-(e1+e2));
double w2=(1-e2)/(2-(e1+e2));
System.out.println("属性1的权重为:"+w1+",属性2的权重为:"+w2);
}
//计算熵权,返回ej
public static double entropyMethod(ArrayList<Double> values){
//先数据归一化
double max= Collections.max(values);
double min=Collections.min(values);
//利用熵权法计算熵权
//(1)
double sum=0;
ArrayList<Double> xij= new ArrayList<>();
for(double num: values){
double temp=(num-min)/(max-min);
xij.add(temp);
sum+=temp;
}
//(2)
ArrayList<Double> pij=new ArrayList<>();
for(double num: xij){
double temp=(num)/(sum);
pij.add(temp);
}
//(3)
double res=0;
double ej=0;
for(double num:pij){
if(num==0)
res+=0;
else
res+=num*Math.log(num);
}
ej=(-1)*(1/Math.log(4))*(res);
//System.out.println(ej);
return ej;
}
}
原文:https://www.cnblogs.com/dongdongdong8001/p/14490597.html