C#熵值法确认权重

这个C#程序实现了熵值法来计算数据表中各列的权重。通过读取数据,计算熵值,进而确定每个指标的权重。权重计算后可用于综合打分,以评估不同省份的经济依赖状况。
摘要由CSDN通过智能技术生成

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class EntropyMethodTool
    {
        //源数据
        private DataTable sourceTable;
        
        //目标数据
        private DataTable targetTable;
        //权重
        public double weight;
        // 名称列表
        private Dictionary<string, double> columnNameDictionary;
        // 构造函数
        public EntropyMethodTool(DataTable sourceTable)
        {
            this.sourceTable = sourceTable;
            this.targetTable = new DataTable();
            this.columnNameDictionary = new Dictionary<string, double>();
        }

        // 获取属性列表
        private void GetColumnNameDictionary()
        {
            for (int i = 1; i < sourceTable.Columns.Count; i++)
            {
                columnNameDictionary.Add(sourceTable.Columns[i].ColumnName, 0.0);
                for (int j = 0; j < sourceTable.Rows.Count; j++)
                {
                    double number = Convert.ToDouble(sourceTable.Rows[j][i].ToString());
                    columnNameDictionary[sourceTable.Columns[i].ColumnName] += number;
                }
            }
        }

        // 步骤一:计算权重
        private void CalculateWeight()
        {
            for (int i = 0; i < sourceTable.Columns.Count; i++)
            {
                if (i == 0)
                {
                    targetTable.Columns.Add(sourceTable.Columns[i].ColumnName, typeof(string));
                }
                else
                {
                    targetTable.Columns.Add(sourceTable.Columns[i].ColumnName, typeof(double));
                }
            }
            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                DataRow row = targetTable.NewRow();
                for (int j = 1; j < sourceTable.Columns.Count; j++)
                {
                    string columnName = sourceTable.Columns[j].ColumnName;
                    double number = Convert.ToDouble(sourceTable.Rows[i][j].ToString()) / columnNameDictionary[columnName];
                    row[j] = number * Math.Log(number);
                }
                targetTable.Rows.Add(row);
            }
            double K = -1 / Math.Log(sourceTable.Rows.Count);
            weight = K;
            for (int i = 1; i < targetTable.Columns.Count; i++)
            {
                string columnName = targetTable.Columns[i].ColumnName;
                columnNameDictionary[columnName] = 0.0;

                for (int j = 0; j < targetTable.Rows.Count; j++)
                {
                    double number = Convert.ToDouble(targetTable.Rows[j][i].ToString());
                    columnNameDictionary[columnName] += number;
                }
                columnNameDictionary[columnName] *= K;
                columnNameDictionary[columnName] = 1 - columnNameDictionary[columnName];
            }
            double sum = 0.0;
            foreach (KeyValuePair<string, double> kvp in columnNameDictionary)
            {
                sum += kvp.Value;
            }
            List<string> keys = new List<string>(columnNameDictionary.Keys);
            foreach (string key in keys)
            {
                double number = Math.Round(columnNameDictionary[key] / sum, 2);
                columnNameDictionary[key] = number;
            }
        }

        // 步骤二:综合打分
        public Dictionary<string, double> CalculateScore()
        {
            GetColumnNameDictionary();
            CalculateWeight();

            Dictionary<string, double> dictionary = new Dictionary<string, double>();
            foreach (DataRow row in sourceTable.Rows)
            {
                dictionary.Add(row[0].ToString(), 0.0);
            }

            List<double> score = new List<double>();
            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                double sum = 0.0;
                for (int j = 1; j < sourceTable.Columns.Count; j++)
                {
                    string columnName = sourceTable.Columns[j].ColumnName;
                    sum += Convert.ToDouble(sourceTable.Rows[i][j].ToString()) * columnNameDictionary[columnName];
                }
                score.Add(sum);
            }
            List<string> keys = new List<string>(dictionary.Keys);
            for (int i = 0; i < keys.Count; i++)
            {
                dictionary[keys[i]] = score[i];
            }
            return dictionary;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Readgesheng();
                Readningxia();
                Readquanguo();
                Readgeshengindex();
                Readningxiaindex();
                Readquanguoind

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值