C#實現利用熵值法確定權重
阿新 • • 發佈:2018-12-06
這裡使用百度上的一個例子來演示,這個例子在百度上是利用Excel做的,這裡用C#實現。該例子的連結:百度例項連結
程式碼如下:
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class EntropyMethodTool { // 源資料 private DataTable sourceTable; // 目標資料 private DataTable targetTable; // 名稱列表 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); 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.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DataTable dataTable = new DataTable(); dataTable.Columns.Add("車型", typeof(string)); dataTable.Columns.Add("油耗", typeof(double)); dataTable.Columns.Add("功率", typeof(double)); dataTable.Columns.Add("費用", typeof(double)); dataTable.Columns.Add("安全性", typeof(double)); dataTable.Columns.Add("維護性", typeof(double)); dataTable.Columns.Add("操作性", typeof(double)); DataRow row = dataTable.NewRow(); row[0] = "本田"; row[1] = 5; row[2] = 1.4; row[3] = 6; row[4] = 3; row[5] = 5; row[6] = 7; dataTable.Rows.Add(row); row = dataTable.NewRow(); row[0] = "奧迪"; row[1] = 9; row[2] = 2; row[3] = 30; row[4] = 7; row[5] = 5; row[6] = 9; dataTable.Rows.Add(row); row = dataTable.NewRow(); row[0] = "桑塔納"; row[1] = 8; row[2] = 1.8; row[3] = 11; row[4] = 5; row[5] = 7; row[6] = 5; dataTable.Rows.Add(row); row = dataTable.NewRow(); row[0] = "別克"; row[1] = 12; row[2] = 2.5; row[3] = 18; row[4] = 7; row[5] = 5; row[6] = 5; dataTable.Rows.Add(row); EntropyMethodTool tool = new EntropyMethodTool(dataTable); foreach (KeyValuePair<string, double> kvp in tool.CalculateScore()) { Console.WriteLine(kvp.Key + "\t" + kvp.Value); } Console.ReadKey(true); } } }
執行結果如下: