⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个用C#编写的ID3分类算法
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.btn_Distinct.Click += new System.EventHandler(this.btn_Distinct_Click);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(24, 256);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(192, 16);
			this.label6.TabIndex = 14;
			this.label6.Text = "结果";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(576, 389);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.groupBox1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form1";
			this.Text = "分类算法_决策树";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		

		public   void printNode(DecisionTreeID3.TreeNode root,System.Windows.Forms.TreeNode rootNode)
		{
			
			System.Windows.Forms.TreeNode childNote=new System.Windows.Forms.TreeNode();
			if (flag==true)
			{   
				rootNode.Text="("+root.attribute.AttributeName+")";
				treeView1.Nodes.Add(rootNode);
				flag=false;
			}
			else
			{
				childNote=rootNode.Nodes.Add("(" + root.attribute.ToString() + ")");
				 
			}
			if (root.attribute.values != null)
			{
				
				for (int i = 0; i < root.attribute.values.Length; i++)
				{
					DecisionTreeID3.TreeNode child = root.getChildByBranchName(root.attribute.values[i]);	
					childNote=rootNode.Nodes.Add(root.attribute.values[i].ToString()+"(" + child.attribute.AttributeName.ToString() + ")");			
					printNode(child,childNote);
				}
			}
		}

		

		static DataTable getDataTable()
		{
			DataTable result = new DataTable("samples");
			DataColumn column = result.Columns.Add("地区");
			column.DataType = typeof(string);
			
			column = result.Columns.Add("温度");
			column.DataType = typeof(string);

			column = result.Columns.Add("湿度");
			column.DataType = typeof(string);

			column = result.Columns.Add("云量");
			column.DataType = typeof(string);

			column = result.Columns.Add("result");
			column.DataType = typeof(bool);

			result.Rows.Add(new object[] {"热带", "高", "潮湿", "多", true}); 
			result.Rows.Add(new object[] {"热带", "中", "潮湿", "少", true}); 
			result.Rows.Add(new object[] {"温带", "中", "潮湿", "多", true}); 
			result.Rows.Add(new object[] {"寒带", "高", "潮湿", "多", false}); 
			result.Rows.Add(new object[] {"寒带", "低", "干燥", "多", false}); 
			result.Rows.Add(new object[] {"寒带", "高", "干燥", "少", false}); 
			result.Rows.Add(new object[] {"温带", "低", "干燥", "少", false}); 
			result.Rows.Add(new object[] {"亚热带", "中", "潮湿", "多", true}); 
			result.Rows.Add(new object[] {"热带", "低", "正常", "多", true}); 
			result.Rows.Add(new object[] {"寒带", "中", "干燥", "多", true}); 
			result.Rows.Add(new object[] {"热带", "中", "干燥", "正常", false}); 
			result.Rows.Add(new object[] {"亚热带", "高", "干燥", "少", false}); 
			result.Rows.Add(new object[] {"温带", "高", "干燥", "正常", false}); 
			result.Rows.Add(new object[] {"寒带", "中", "潮湿", "少", false}); 
			result.Rows.Add(new object[] {"寒带", "中", "正常", "少", false});
			result.Rows.Add(new object[] {"亚热带", "中", "正常", "少", false});
			result.Rows.Add(new object[] {"亚热带", "中", "潮湿", "少", true});
			result.Rows.Add(new object[] {"寒带", "高", "干燥", "少", false});
			return result;
			
		}

		public string trim_str(string str)
		{   string temp_str=str.TrimStart('(');
			return temp_str.TrimEnd(')');
		}

		public int search_str(string str1,string str2)
		{
			return str1.IndexOf(str2);

		}

		
		private void PrintRecursive(System.Windows.Forms.TreeNode treeNode)
		{
			//MessageBox.Show(treeNode.Text);
			
			label6.Text="结果:"+treeNode.Text;
			if(string.Compare(trim_str(treeNode.Text.Substring(search_str(treeNode.Text,"("))),"地区")==0)
			{
				foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
				{
					if(string.Compare(trim_str(tn.Text.Substring(0,search_str(tn.Text,"("))), cbo_zone.SelectedItem.ToString ())==0)
					PrintRecursive(tn);
				}
			}

			else if(string.Compare(trim_str(treeNode.Text.Substring(search_str(treeNode.Text,"("))),"云量")==0)
			{
				foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
				{
					if(string.Compare(trim_str(tn.Text.Substring(0,search_str(tn.Text,"("))), cbo_cloud.SelectedItem.ToString ())==0)
						PrintRecursive(tn);
				}
			}
			else if(string.Compare(trim_str(treeNode.Text.Substring(search_str(treeNode.Text,"("))),"温度")==0)
			{
				foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
				{
					if(string.Compare(trim_str(tn.Text.Substring(0,search_str(tn.Text,"("))), cbo_temperature.SelectedItem.ToString ())==0)
						PrintRecursive(tn);
				}
			}
			else if(string.Compare(trim_str(treeNode.Text.Substring(search_str(treeNode.Text,"("))),"湿度")==0)
			{
				foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
				{
					if(string.Compare(trim_str(tn.Text.Substring(0,search_str(tn.Text,"("))), cbo_humidity.SelectedItem.ToString ())==0)
						PrintRecursive(tn);
				}

			}

			else 
				foreach (System.Windows.Forms.TreeNode tn in treeNode.Nodes)
				{
					
						PrintRecursive(tn);
				}
			
               
			
		}


		private void btn_Distinct_Click(object sender, System.EventArgs e)
		{
			
			
				PrintRecursive(treeView1.TopNode);
		}


		private void Form1_Load(object sender, System.EventArgs e)
		{

			
			flag=true;
			DecisionTreeID3.Attribute zone = new DecisionTreeID3.Attribute("地区", new string[] {"热带", "温带", "寒带","亚热带"});
			DecisionTreeID3.Attribute temperature = new DecisionTreeID3.Attribute("温度", new string[] {"高", "低", "中"});
			DecisionTreeID3.Attribute humidity = new DecisionTreeID3.Attribute("湿度", new string[] {"潮湿", "干燥","正常"});
			DecisionTreeID3.Attribute cloud = new DecisionTreeID3.Attribute("云量", new string[] {"少","正常","多"});
			DecisionTreeID3.Attribute[] attributes = new DecisionTreeID3.Attribute[] {zone, temperature, humidity, cloud};
			
			//添加comboBox选择项
			for(int i=0;i < zone.values.Length;i++)
				cbo_zone.Items.Add(zone.values[i].ToString());
			cbo_zone.SelectedIndex=0;

			for(int i=0;i < temperature.values.Length;i++)
				cbo_temperature.Items.Add(temperature.values[i].ToString());
			cbo_temperature.SelectedIndex=0;

			for(int i=0;i < humidity.values.Length;i++)
				cbo_humidity.Items.Add(humidity.values[i].ToString());
			cbo_humidity.SelectedIndex=0;

			for(int i=0;i < cloud.values.Length;i++)
				cbo_cloud.Items.Add(cloud.values[i].ToString());
			cbo_cloud.SelectedIndex=0;

			DataTable samples = getDataTable();			
			DecisionTreeID3.DecisionTreeID3 id3 = new DecisionTreeID3.DecisionTreeID3();
			DecisionTreeID3.TreeNode root = id3.mountTree(samples, "result", attributes);
			System.Windows.Forms.TreeNode rootNode=new System.Windows.Forms.TreeNode();
			printNode(root,rootNode);

		}

		private void btn_Exit_Click(object sender, System.EventArgs e)
		{
		    //label6.Text =trim_str(treeNode.Text.Substring(search_str(treeNode.Text,"(")));
			Application.Exit();
			
		}
		

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -