導航:首頁 > 源碼編譯 > id3演算法實現java

id3演算法實現java

發布時間:2023-04-07 16:04:34

Ⅰ ID3演算法中關於java代碼轉化為c#代碼的問題

public string maxClass(Dictionary<string, Int32> classes)
{
string maxC = "";
int max = -1;
foreach (KeyValuePair<string, Int32> tmpClass in classes)
{
if (tmpClass.Value > max)
{
max = tmpClass.Value;
maxC = tmpClass.Key;
}
}

return maxC;
}

Ⅱ 求助 weka 的ID3演算法java源碼

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Id3.java
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
*
*/
package weka.classifiers.trees;
import weka.classifiers.Classifier;
import weka.classifiers.Sourcable;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformationHandler;
import weka.core.Utils;
import weka.core.Capabilities.Capability;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import java.util.Enumeration;
/**
<!-- globalinfo-start -->
* Class for constructing an unpruned decision tree based on the ID3 algorithm. Can only deal with nominal attributes. No missing values allowed. Empty leaves may result in unclassified instances. For more information see: <br/>
* <br/>
* R. Quinlan (1986). Inction of decision trees. Machine Learning. 1(1):81-106.
* <p/>
<!-- globalinfo-end -->
*
<!-- technical-bibtex-start -->
* BibTeX:
* <pre>
* &#64;article{Quinlan1986,
* author = {R. Quinlan},
* journal = {Machine Learning},
* number = {1},
* pages = {81-106},
* title = {Inction of decision trees},
* volume = {1},
* year = {1986}
* }
* </pre>
* <p/>
<!-- technical-bibtex-end -->
*
<!-- options-start -->
* Valid options are: <p/>
*
* <pre> -D
* If set, classifier is run in debug mode and
* may output additional info to the console</pre>
*
<!-- options-end -->
*
* @author Eibe Frank ([email protected])
* @version $Revision: 6404 $
*/
public class Id3
extends Classifier
implements TechnicalInformationHandler, Sourcable {
/** for serialization */
static final long serialVersionUID = -2693678647096322561L;
/** The node's successors. */
private Id3[] m_Successors;
/** Attribute used for splitting. */
private Attribute m_Attribute;
/** Class value if node is leaf. */
private double m_ClassValue;
/** Class distribution if node is leaf. */
private double[] m_Distribution;
/** Class attribute of dataset. */
private Attribute m_ClassAttribute;
/**
* Returns a string describing the classifier.
* @return a description suitable for the GUI.
*/
public String globalInfo() {
return "Class for constructing an unpruned decision tree based on the ID3 "
+ "algorithm. Can only deal with nominal attributes. No missing values "
+ "allowed. Empty leaves may result in unclassified instances. For more "
+ "information see: "
+ getTechnicalInformation().toString();
}
/**
* Returns an instance of a TechnicalInformation object, containing
* detailed information about the technical background of this class,
* e.g., paper reference or book this class is based on.
*
* @return the technical information about this class
*/
public TechnicalInformation getTechnicalInformation() {
TechnicalInformation result;
result = new TechnicalInformation(Type.ARTICLE);
result.setValue(Field.AUTHOR, "R. Quinlan");
result.setValue(Field.YEAR, "1986");
result.setValue(Field.TITLE, "Inction of decision trees");
result.setValue(Field.JOURNAL, "Machine Learning");
result.setValue(Field.VOLUME, "1");
result.setValue(Field.NUMBER, "1");
result.setValue(Field.PAGES, "81-106");
return result;
}
/**
* Returns default capabilities of the classifier.
*
* @return the capabilities of this classifier
*/
public Capabilities getCapabilities() {
Capabilities result = super.getCapabilities();
result.disableAll();
// attributes
result.enable(Capability.NOMINAL_ATTRIBUTES);
// class
result.enable(Capability.NOMINAL_CLASS);
result.enable(Capability.MISSING_CLASS_VALUES);
// instances
result.setMinimumNumberInstances(0);
return result;
}
/**
* Builds Id3 decision tree classifier.
*
* @param data the training data
* @exception Exception if classifier can't be built successfully
*/
public void buildClassifier(Instances data) throws Exception {
// can classifier handle the data?
getCapabilities().testWithFail(data);
// remove instances with missing class
data = new Instances(data);
data.deleteWithMissingClass();
makeTree(data);
}
/**
* Method for building an Id3 tree.
*
* @param data the training data
* @exception Exception if decision tree can't be built successfully
*/
private void makeTree(Instances data) throws Exception {
// Check if no instances have reached this node.
if (data.numInstances() == 0) {
m_Attribute = null;
m_ClassValue = Instance.missingValue();
m_Distribution = new double[data.numClasses()];
return;
}
// Compute attribute with maximum information gain.
double[] infoGains = new double[data.numAttributes()];
Enumeration attEnum = data.enumerateAttributes();
while (attEnum.hasMoreElements()) {
Attribute att = (Attribute) attEnum.nextElement();
infoGains[att.index()] = computeInfoGain(data, att);
}
m_Attribute = data.attribute(Utils.maxIndex(infoGains));
// Make leaf if information gain is zero.
// Otherwise create successors.
if (Utils.eq(infoGains[m_Attribute.index()], 0)) {
m_Attribute = null;
m_Distribution = new double[data.numClasses()];
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
m_Distribution[(int) inst.classValue()]++;
}
Utils.normalize(m_Distribution);
m_ClassValue = Utils.maxIndex(m_Distribution);
m_ClassAttribute = data.classAttribute();
} else {
Instances[] splitData = splitData(data, m_Attribute);
m_Successors = new Id3[m_Attribute.numValues()];
for (int j = 0; j < m_Attribute.numValues(); j++) {
m_Successors[j] = new Id3();
m_Successors[j].makeTree(splitData[j]);
}
}
}
/**
* Classifies a given test instance using the decision tree.
*
* @param instance the instance to be classified
* @return the classification
* @throws if instance has missing values
*/
public double classifyInstance(Instance instance)
throws {
if (instance.hasMissingValue()) {
throw new ("Id3: no missing values, "
+ "please.");
}
if (m_Attribute == null) {
return m_ClassValue;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
classifyInstance(instance);
}
}
/**
* Computes class distribution for instance using decision tree.
*
* @param instance the instance for which distribution is to be computed
* @return the class distribution for the given instance
* @throws if instance has missing values
*/
public double[] distributionForInstance(Instance instance)
throws {
if (instance.hasMissingValue()) {
throw new ("Id3: no missing values, "
+ "please.");
}
if (m_Attribute == null) {
return m_Distribution;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
distributionForInstance(instance);
}
}
/**
* Prints the decision tree using the private toString method from below.
*
* @return a textual description of the classifier
*/
public String toString() {
if ((m_Distribution == null) && (m_Successors == null)) {
return "Id3: No model built yet.";
}
return "Id3 " + toString(0);
}
/**
* Computes information gain for an attribute.
*
* @param data the data for which info gain is to be computed
* @param att the attribute
* @return the information gain for the given attribute and data
* @throws Exception if computation fails
*/
private double computeInfoGain(Instances data, Attribute att)
throws Exception {
double infoGain = computeEntropy(data);
Instances[] splitData = splitData(data, att);
for (int j = 0; j < att.numValues(); j++) {
if (splitData[j].numInstances() > 0) {
infoGain -= ((double) splitData[j].numInstances() /
(double) data.numInstances()) *
computeEntropy(splitData[j]);
}
}
return infoGain;
}
/**
* Computes the entropy of a dataset.
*
* @param data the data for which entropy is to be computed
* @return the entropy of the data's class distribution
* @throws Exception if computation fails
*/
private double computeEntropy(Instances data) throws Exception {
double [] classCounts = new double[data.numClasses()];
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
classCounts[(int) inst.classValue()]++;
}
double entropy = 0;
for (int j = 0; j < data.numClasses(); j++) {
if (classCounts[j] > 0) {
entropy -= classCounts[j] * Utils.log2(classCounts[j]);
}
}
entropy /= (double) data.numInstances();
return entropy + Utils.log2(data.numInstances());
}
/**
* Splits a dataset according to the values of a nominal attribute.
*
* @param data the data which is to be split
* @param att the attribute to be used for splitting
* @return the sets of instances proced by the split
*/
private Instances[] splitData(Instances data, Attribute att) {
Instances[] splitData = new Instances[att.numValues()];
for (int j = 0; j < att.numValues(); j++) {
splitData[j] = new Instances(data, data.numInstances());
}
Enumeration instEnum = data.enumerateInstances();
while (instEnum.hasMoreElements()) {
Instance inst = (Instance) instEnum.nextElement();
splitData[(int) inst.value(att)].add(inst);
}
for (int i = 0; i < splitData.length; i++) {
splitData[i].compactify();
}
return splitData;
}
/**
* Outputs a tree at a certain level.
*
* @param level the level at which the tree is to be printed
* @return the tree as string at the given level
*/
private String toString(int level) {
StringBuffer text = new StringBuffer();
if (m_Attribute == null) {
if (Instance.isMissingValue(m_ClassValue)) {
text.append(": null");
} else {
text.append(": " + m_ClassAttribute.value((int) m_ClassValue));
}
} else {
for (int j = 0; j < m_Attribute.numValues(); j++) {
text.append(" ");
for (int i = 0; i < level; i++) {
text.append("| ");
}
text.append(m_Attribute.name() + " = " + m_Attribute.value(j));
text.append(m_Successors[j].toString(level + 1));
}
}
return text.toString();
}
/**
* Adds this tree recursively to the buffer.
*
* @param id the unqiue id for the method
* @param buffer the buffer to add the source code to
* @return the last ID being used
* @throws Exception if something goes wrong
*/
protected int toSource(int id, StringBuffer buffer) throws Exception {
int result;
int i;
int newID;
StringBuffer[] subBuffers;
buffer.append(" ");
buffer.append(" protected static double node" + id + "(Object[] i) { ");
// leaf?
if (m_Attribute == null) {
result = id;
if (Double.isNaN(m_ClassValue)) {
buffer.append(" return Double.NaN;");
} else {
buffer.append(" return " + m_ClassValue + ";");
}
if (m_ClassAttribute != null) {
buffer.append(" // " + m_ClassAttribute.value((int) m_ClassValue));
}
buffer.append(" ");
buffer.append(" } ");
} else {
buffer.append(" checkMissing(i, " + m_Attribute.index() + "); ");
buffer.append(" // " + m_Attribute.name() + " ");
// subtree calls
subBuffers = new StringBuffer[m_Attribute.numValues()];
newID = id;
for (i = 0; i < m_Attribute.numValues(); i++) {
newID++;
buffer.append(" ");
if (i > 0) {
buffer.append("else ");
}
buffer.append("if (((String) i[" + m_Attribute.index()
+ "]).equals("" + m_Attribute.value(i) + "")) ");
buffer.append(" return node" + newID + "(i); ");
subBuffers[i] = new StringBuffer();
newID = m_Successors[i].toSource(newID, subBuffers[i]);
}
buffer.append(" else ");
buffer.append(" throw new IllegalArgumentException("Value '" + i["
+ m_Attribute.index() + "] + "' is not allowed!"); ");
buffer.append(" } ");
// output subtree code
for (i = 0; i < m_Attribute.numValues(); i++) {
buffer.append(subBuffers[i].toString());
}
subBuffers = null;
result = newID;
}
return result;
}
/**
* Returns a string that describes the classifier as source. The
* classifier will be contained in a class with the given name (there may
* be auxiliary classes),
* and will contain a method with the signature:
* <pre><code>
* public static double classify(Object[] i);
* </code></pre>
* where the array <code>i</code> contains elements that are either
* Double, String, with missing values represented as null. The generated
* code is public domain and comes with no warranty. <br/>
* Note: works only if class attribute is the last attribute in the dataset.
*
* @param className the name that should be given to the source class.
* @return the object source described by a string
* @throws Exception if the source can't be computed
*/
public String toSource(String className) throws Exception {
StringBuffer result;
int id;
result = new StringBuffer();
result.append("class " + className + " { ");
result.append(" private static void checkMissing(Object[] i, int index) { ");
result.append(" if (i[index] == null) ");
result.append(" throw new IllegalArgumentException("Null values "
+ "are not allowed!"); ");
result.append(" } ");
result.append(" public static double classify(Object[] i) { ");
id = 0;
result.append(" return node" + id + "(i); ");
result.append(" } ");
toSource(id, result);
result.append("} ");
return result.toString();
}
/**
* Returns the revision string.
*
* @return the revision
*/
public String getRevision() {
return RevisionUtils.extract("$Revision: 6404 $");
}
/**
* Main method.
*
* @param args the options for the classifier
*/
public static void main(String[] args) {
runClassifier(new Id3(), args);
}
}

Ⅲ 決策樹——ID3演算法應用實例

在ID3決策樹歸納方法中,通常是使用信息增益方法來幫助確定生成每個節點時所應採用的合適屬性。這樣就可以選擇具有最高信息增益(熵減少的程度最大)的屬性最為當前節點的測試屬性,以便對之後劃分的訓練樣本子集進行分類所需要的信息最小,也就是說,利用該屬性進行當前(節點所含)樣本集合劃分,將會使得所產生的樣本子集中的「不同類別的混合程度」降為最低。因此,採用這樣一種資訊理論方法將有效減少對象分來所需要的次數,從而確保所產生的決策樹最為簡單。

一、實驗目的

1、理解分類

2、掌握分類挖掘演算法ID3

3、為改進ID3打下基礎

二、實驗內容

1、選定一個數據集(可以參考教學中使用的數據集)

2、選擇合適的實現環境和工具實現演算法 ID3

3、給出分類規則

三、實驗原理

決策樹是一種最常見的分類演算法,它包含有很多不同的變種,ID3演算法是其中最簡單的一種。ID3演算法中最主要的部分就是信息熵和信息增益的計算。

Ⅳ 5.10 決策樹與ID3演算法

https://blog.csdn.net/dorisi_h_n_q/article/details/82787295

決策樹(decision tree)是一個樹結構(可以是二叉樹或非二叉樹)。決策過程是從根節點開始,測試待分類項中相應的特徵屬性,並按照其值選擇輸出分支,直到到達葉子節點,將葉子節點存放的類別作為決策結果。

決策樹的關鍵步驟是分裂屬性。就是在某節點處按某一特徵屬性的不同劃分構造不同的分支,目標是讓各個分裂子集盡可能地「純」。即讓一個分裂子集中待分類項屬於同一類別。

簡而言之,決策樹的劃分原則就是:將無序的數據變得更加有序

分裂屬性分為三種不同的情況 :

構造決策樹的關鍵性內容是進行屬性選擇度量,屬性選擇度量(找一種計算方式來衡量怎麼劃分更劃算)是一種選擇分裂准則,它決定了拓撲結構及分裂點split_point的選擇。

屬性選擇度量演算法有很多,一般使用自頂向下遞歸分治法,並採用不回溯的貪心策略。這里介紹常用的ID3演算法。

貪心演算法(又稱貪婪演算法)是指,在對問題求解時,總是做出在當前看來是最好的選擇。也就是說,不從整體最優上加以考慮,所做出的是在某種意義上的局部最優解。

此概念最早起源於物理學,是用來度量一個熱力學系統的無序程度。
而在信息學裡面,熵是對不確定性的度量。
在1948年,香農引入了信息熵,將其定義為離散隨機事件出現的概率,一個系統越是有序,信息熵就越低,反之一個系統越是混亂,它的信息熵就越高。所以信息熵可以被認為是系統有序化程度的一個度量。

熵定義為信息的期望值,在明晰這個概念之前,我們必須知道信息的定義。如果待分類的事務可能劃分在多個分類之中,則符號x的信息定義為:

在劃分數據集之前之後信息發生的變化稱為信息增益。
知道如何計算信息增益,就可計算每個特徵值劃分數據集獲得的信息增益,獲得信息增益最高的特徵就是最好的選擇。

條件熵 表示在已知隨機變數的條件下隨機變數的不確定性,隨機變數X給定的條件下隨機變數Y的條
件熵(conditional entropy) ,定義X給定條件下Y的條件概率分布的熵對X的數學期望:

根據上面公式,我們假設將訓練集D按屬性A進行劃分,則A對D劃分的期望信息為

則信息增益為如下兩者的差值

ID3演算法就是在每次需要分裂時,計算每個屬性的增益率,然後選擇增益率最大的屬性進行分裂

步驟:1. 對當前樣本集合,計算所有屬性的信息增益;

是最原始的決策樹分類演算法,基本流程是,從一棵空數出發,不斷的從決策表選取屬性加入數的生長過程中,直到決策樹可以滿足分類要求為止。CLS演算法存在的主要問題是在新增屬性選取時有很大的隨機性。ID3演算法是對CLS演算法的改進,主要是摒棄了屬性選擇的隨機性。

基於ID3演算法的改進,主要包括:使用信息增益比替換了信息增益下降度作為屬性選擇的標准;在決策樹構造的同時進行剪枝操作;避免了樹的過度擬合情況;可以對不完整屬性和連續型數據進行處理;使用k交叉驗證降低了計算復雜度;針對數據構成形式,提升了演算法的普適性。

信息增益值的大小相對於訓練數據集而言的,並沒有絕對意義,在分類問題困難時,也就是說在訓練數據集經驗熵大的時候,信息增益值會偏大,反之信息增益值會偏小,使用信息增益比可以對這個問題進行校正,這是特徵選擇
的另一個標准。
特徵對訓練數據集的信息增益比定義為其信息增益gR( D,A) 與訓練數據集的經驗熵g(D,A)之比 :

gR(D,A) = g(D,A) / H(D)

sklearn的決策樹模型就是一個CART樹。是一種二分遞歸分割技術,把當前樣本劃分為兩個子樣本,使得生成的每個非葉子節點都有兩個分支,因此,CART演算法生成的決策樹是結構簡潔的二叉樹。
分類回歸樹演算法(Classification and Regression Trees,簡稱CART演算法)是一種基於二分遞歸分割技術的演算法。該演算法是將當前的樣本集,分為兩個樣本子集,這樣做就使得每一個非葉子節點最多隻有兩個分支。因此,使用CART
演算法所建立的決策樹是一棵二叉樹,樹的結構簡單,與其它決策樹演算法相比,由該演算法生成的決策樹模型分類規則較少。

CART分類演算法的基本思想是:對訓練樣本集進行遞歸劃分自變數空間,並依次建立決策樹模型,然後採用驗證數據的方法進行樹枝修剪,從而得到一顆符合要求的決策樹分類模型。

CART分類演算法和C4.5演算法一樣既可以處理離散型數據,也可以處理連續型數據。CART分類演算法是根據基尼(gini)系
數來選擇測試屬性,gini系數的值越小,劃分效果越好。設樣本集合為T,則T的gini系數值可由下式計算:

CART演算法優點:除了具有一般決策樹的高准確性、高效性、模式簡單等特點外,還具有一些自身的特點。
如,CART演算法對目標變數和預測變數在概率分布上沒有要求,這樣就避免了因目標變數與預測變數概率分布的不同造成的結果;CART演算法能夠處理空缺值,這樣就避免了因空缺值造成的偏差;CART演算法能夠處理孤立的葉子結點,這樣可以避免因為數據集中與其它數據集具有不同的屬性的數據對進一步分支產生影響;CART演算法使用的是二元分支,能夠充分地運用數據集中的全部數據,進而發現全部樹的結構;比其它模型更容易理解,從模型中得到的規則能獲得非常直觀的解釋。

CART演算法缺點:CART演算法是一種大容量樣本集挖掘演算法,當樣本集比較小時不夠穩定;要求被選擇的屬性只能產生兩個子結點,當類別過多時,錯誤可能增加得比較快。

sklearn.tree.DecisionTreeClassifier

1.安裝graphviz.msi , 一路next即可

ID3演算法就是在每次需要分裂時,計算每個屬性的增益率,然後選擇增益率最大的屬性進行分裂

按照好友密度劃分的信息增益:

按照是否使用真實頭像H劃分的信息增益

**所以,按先按好友密度劃分的信息增益比按真實頭像劃分的大。應先按好友密度劃分。

Ⅳ 決策樹之ID3演算法及其Python實現

決策樹之ID3演算法及其Python實現

1. 決策樹背景知識
??決策樹是數據挖掘中最重要且最常用的方法之一,主要應用於數據挖掘中的分類和預測。決策樹是知識的一種呈現方式,決策樹中從頂點到每個結點的路徑都是一條分類規則。決策樹演算法最先基於資訊理論發展起來,經過幾十年發展,目前常用的演算法有:ID3、C4.5、CART演算法等。
2. 決策樹一般構建過程
??構建決策樹是一個自頂向下的過程。樹的生長過程是一個不斷把數據進行切分細分的過程,每一次切分都會產生一個數據子集對應的節點。從包含所有數據的根節點開始,根據選取分裂屬性的屬性值把訓練集劃分成不同的數據子集,生成由每個訓練數據子集對應新的非葉子節點。對生成的非葉子節點再重復以上過程,直到滿足特定的終止條件,停止對數據子集劃分,生成數據子集對應的葉子節點,即所需類別。測試集在決策樹構建完成後檢驗其性能。如果性能不達標,我們需要對決策樹演算法進行改善,直到達到預期的性能指標。
??註:分裂屬性的選取是決策樹生產過程中的關鍵,它決定了生成的決策樹的性能、結構。分裂屬性選擇的評判標準是決策樹演算法之間的根本區別。
3. ID3演算法分裂屬性的選擇——信息增益
??屬性的選擇是決策樹演算法中的核心。是對決策樹的結構、性能起到決定性的作用。ID3演算法基於信息增益的分裂屬性選擇。基於信息增益的屬性選擇是指以信息熵的下降速度作為選擇屬性的方法。它以的資訊理論為基礎,選擇具有最高信息增益的屬性作為當前節點的分裂屬性。選擇該屬性作為分裂屬性後,使得分裂後的樣本的信息量最大,不確定性最小,即熵最小。
??信息增益的定義為變化前後熵的差值,而熵的定義為信息的期望值,因此在了解熵和信息增益之前,我們需要了解信息的定義。
??信息:分類標簽xi 在樣本集 S 中出現的頻率記為 p(xi),則 xi 的信息定義為:?log2p(xi) 。
??分裂之前樣本集的熵:E(S)=?∑Ni=1p(xi)log2p(xi),其中 N 為分類標簽的個數。
??通過屬性A分裂之後樣本集的熵:EA(S)=?∑mj=1|Sj||S|E(Sj),其中 m 代表原始樣本集通過屬性A的屬性值劃分為 m 個子樣本集,|Sj| 表示第j個子樣本集中樣本數量,|S| 表示分裂之前數據集中樣本總數量。
??通過屬性A分裂之後樣本集的信息增益:InfoGain(S,A)=E(S)?EA(S)
??註:分裂屬性的選擇標准為:分裂前後信息增益越大越好,即分裂後的熵越小越好。
4. ID3演算法
??ID3演算法是一種基於信息增益屬性選擇的決策樹學習方法。核心思想是:通過計算屬性的信息增益來選擇決策樹各級節點上的分裂屬性,使得在每一個非葉子節點進行測試時,獲得關於被測試樣本最大的類別信息。基本方法是:計算所有的屬性,選擇信息增益最大的屬性分裂產生決策樹節點,基於該屬性的不同屬性值建立各分支,再對各分支的子集遞歸調用該方法建立子節點的分支,直到所有子集僅包括同一類別或沒有可分裂的屬性為止。由此得到一棵決策樹,可用來對新樣本數據進行分類。
ID3演算法流程:
(1) 創建一個初始節點。如果該節點中的樣本都在同一類別,則演算法終止,把該節點標記為葉節點,並用該類別標記。
(2) 否則,依據演算法選取信息增益最大的屬性,該屬性作為該節點的分裂屬性。
(3) 對該分裂屬性中的每一個值,延伸相應的一個分支,並依據屬性值劃分樣本。
(4) 使用同樣的過程,自頂向下的遞歸,直到滿足下面三個條件中的一個時就停止遞歸。
??A、待分裂節點的所有樣本同屬於一類。
??B、訓練樣本集中所有樣本均完成分類。
??C、所有屬性均被作為分裂屬性執行一次。若此時,葉子結點中仍有屬於不同類別的樣本時,選取葉子結點中包含樣本最多的類別,作為該葉子結點的分類。
ID3演算法優缺點分析
優點:構建決策樹的速度比較快,演算法實現簡單,生成的規則容易理解。
缺點:在屬性選擇時,傾向於選擇那些擁有多個屬性值的屬性作為分裂屬性,而這些屬性不一定是最佳分裂屬性;不能處理屬性值連續的屬性;無修剪過程,無法對決策樹進行優化,生成的決策樹可能存在過度擬合的情況。

Ⅵ id3演算法是什麼

ID3演算法是一種貪心演算法,用來構造決策樹。ID3演算法起源於概念學習系統(CLS),以信息熵的下降速度為選取測試屬性的標准,即在每個節點選取還尚未被用來劃分的具有最高信息增益的屬性作為劃分標准,然後繼續這個過程,直到生成的決策樹能完美分類訓練樣例。

ID3演算法的背景

ID3演算法最早是由羅斯昆(J. Ross Quinlan)於1975年在悉尼大學提出的一種分類預測演算法,演算法的核心是「信息熵」。ID3演算法通過計算每個屬性的信息增益,認為信息增益高的是好屬性,每次劃分選取信息增益最高的屬性為劃分標准,重復這個過程,直至生成一個能完美分類訓練樣例的決策樹。

Ⅶ 用java編寫獲取多媒體文件id3信息的Android代碼

$this->error="Nosuchfile"; if($exitonerror)$this->exitonerror(); } } functionexitonerror(){ echo($this->error); exit; } functionset_id3($title="",$author="",$album="",$year="",$comment="",$genre_id=0){ $this->error=false; $this->wfh=fopen($this->file,"a"); fseek($this->wfh,-128,SEEK_END); fwrite($this->wfh,pack("a3a30a30a30a4a30C1","TAG",$title,$author,$album,$year,$comment,$genre_id),128); fclose($this->wfh); } functionget_id3(){ $this->id3_parsed=true; fseek($this->fh,-128,SEEK_END); $line=fread($this->fh,10000); if(preg_match("/^TAG/弊滾御",$line)){ $this->id3=unpack("a3tag/a30title/a30author/a30album/a4year/a30comment/C1genre_id",$line); $this->id3["genre"]=$this->id3_genres_array[$this->id3]["genre_id"]]; return(true); }else{ $this->error="noidv3tagfound"; return(false); } } //get_info()helpermethods functioncalculate_length($id3v2_tagsize=0){ $length=floor(($this->info["filesize"]-$id3v2_tagsize)/$this->info["bitrate"]*0.008); $min=floor($length/60); $min=strlen($min)==1?"0$min":$min; $sec=$length`; $sec=strlen($sec)==1?"0$sec":$sec; return("$min:$sec"); } functionget_info(){ // $this->get_id3v2header(); $second=$this->synchronize(); // echo("2ndbyte=$second<b>".decbin($second)."</b><br>"); $third=ord(fread($this->fh,1)); $fourth=ord(fread($this->fh,1)); $this->info["version_id"]=($second&16)>0?(($second&8)>0?1:2):(($second&8)>0?0:2.5); $this->info["version"]=$this->info_versions[$this->info]["version_id"]]; $this->租岩info["layer_id"]=($second&4)>0?(($second&2)>0?1:2):(($second&2)>0?3:0); ; $this->info["layer"]=$this->info_layers[$this->info]["layer_id"]]; $this->info["備鏈protection"]=($second&1)>0?"noCRC":"CRC"; $this->info["bitrate"]=$this->info_bitrates[$this->info]["version_id"]][$this->info]["layer_id"]][($third&240)]; $this->info["sampling_rate"]=$this->info_sampling_rates[$this->info]["version_id"]][($third&12)];

Ⅷ 什麼是ID3演算法

ID3演算法是由Quinlan首先提出的。該演算法是以資訊理論為基礎,以信息熵和信息增益度為衡量標准,從而實現對數據的歸納分類。以下是一些資訊理論的基本概念:
定義1:若存在n個相同概率的消息,則每個消息的概率p是1/n,一個消息傳遞的信息量為-Log2(1/n)
定義2:若有n個消息,其給定概率分布為P=(p1,p2…pn),則由該分布傳遞的信息量稱為P的熵,記為

定義3:若一個記錄集合T根據類別屬性的值被分成互相獨立的類C1C2..Ck,則識別T的一個元素所屬哪個類所需要的信息量為Info(T)=I(p),其中P為C1C2…Ck的概率分布,即P=(|C1|/|T|,…..|Ck|/|T|)
定義4:若我們先根據非類別屬性X的值將T分成集合T1,T2…Tn,則確定T中一個元素類的信息量可通過確定Ti的加權平均值來得到,即Info(Ti)的加權平均值為:
Info(X, T)=(i=1 to n 求和)((|Ti|/|T|)Info(Ti))
定義5:信息增益度是兩個信息量之間的差值,其中一個信息量是需確定T的一個元素的信息量,另一個信息量是在已得到的屬性X的值後需確定的T一個元素的信息量,信息增益度公式為:
Gain(X, T)=Info(T)-Info(X, T)
ID3演算法計算每個屬性的信息增益,並選取具有最高增益的屬性作為給定集合的測試屬性。對被選取的測試屬性創建一個節點,並以該節點的屬性標記,對該屬性的每個值創建一個分支據此劃分樣本.
數據描述
所使用的樣本數據有一定的要求,ID3是:
描述-屬性-值相同的屬性必須描述每個例子和有固定數量的價值觀。
預定義類-實例的屬性必須已經定義的,也就是說,他們不是學習的ID3。
離散類-類必須是尖銳的鮮明。連續類分解成模糊范疇(如金屬被「努力,很困難的,靈活的,溫柔的,很軟」都是不可信的。
足夠的例子——因為歸納概括用於(即不可查明)必須選擇足夠多的測試用例來區分有效模式並消除特殊巧合因素的影響。
屬性選擇
ID3決定哪些屬性如何是最好的。一個統計特性,被稱為信息增益,使用熵得到給定屬性衡量培訓例子帶入目標類分開。信息增益最高的信息(信息是最有益的分類)被選擇。為了明確增益,我們首先從資訊理論借用一個定義,叫做熵。每個屬性都有一個熵。

Ⅸ 簡述ID3演算法基本原理和步驟

1.基本原理:
以信息增益/信息熵為度量,用於決策樹結點的屬性選擇的標准,每次優先選取信息量最多(信息增益最大)的屬性,即信息熵值最小的屬性,以構造一顆熵值下降最快的決策樹,到葉子節點處的熵值為0。(信息熵 無條件熵 條件熵 信息增益 請查找其他資料理解)
決策樹將停止生長條件及葉子結點的類別取值:
①數據子集的每一條數據均已經歸類到每一類,此時,葉子結點取當前樣本類別值。
②數據子集類別仍有混亂,但已經找不到新的屬性進行結點分解,此時,葉子結點按當前樣本中少數服從多數的原則進行類別取值。
③數據子集為空,則按整個樣本中少數服從多數的原則進行類別取值。

步驟:
理解了上述停止增長條件以及信息熵,步驟就很簡單

Ⅹ 用java編寫獲取多媒體文件id3信息的Android代碼

當前位置: > php如何調用JAVA 類庫
PHP如何調用JAVA 類庫
JAVA是個非常強大悔瞎段的編程利器,它的擴展庫也是非常的有用,這篇教程,主要講述怎樣使用PHP調用功能強大的JAVA 類庫(classes)。為了方便你的學習,這篇教程將包括JAVA的安裝及一些基本的例子。 Windows下的安裝 第一步:安裝JDK,這是非常容易的,你只需一路回車的安裝好。然後做好以下步驟。在 Win9x 下加入 :「PATH=%PATH%;C:\jdk1.2.2\bin」 到AUTOEXEC.BAT文件中 在 NT 下加入 「;C:\jdk1.2.2\bin」到環境變數中。 這一步是非常重要的,這樣PHP才能正確的找到需調用的JAVA類。 第二步:修改你碧譽的PHP.INI文件。 [java] extension=php_java.dll java.library.path=c:\web\php4\extensions\ java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses" 在PHP.INI中加入extension=php_java.dll,並在[java]中,設定好java.class.path,讓它指向php_java.jar,如果你使用新的JAVA類,你也應該存入這個路徑,在這篇例子中,我們使用c:\myclasses這個目錄。 第三步:測試環境,創建如下PHP文件: <?php $system = new Java("java.lang.System"); print "Java version=".$system->getProperty("java.version")." <br>\n"; print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n"; print "OS=".$system->getProperty("os.name")." ". $system->getProperty("os.version")." on ". $system->getProperty("os.arch")." <br>\n"; $formatter = new Java("java.text.SimpleDateFormat","EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java("java.util.Date"))."\n"; ?> 如果你正確安裝了,你將會看到以下信息: Java version=1.2.2 Java vendor=Sun Microsystems Inc. OS=Windows 95 4.10 on x86 Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time 這樣,我們就已經成功的建立起了可以使用JAVA類的PHP運行環境,我們可以開始我們接下去的課程了。 例子1:創建和使用你自己的JAVA類 創建你自己的JAVA類非常容易。新建一個phptest.java文件,將它放置在你的java.class.path目錄下,文神廳件內容如下: public class phptest return str; } /** * whatisfoo() simply returns the value of the variable foo. */ public String whatisfoo() /** * This is called if phptest is run from the command line with * something like * java phptes

閱讀全文

與id3演算法實現java相關的資料

熱點內容
單片機的功能模塊 瀏覽:771
安卓手機如何錄制視頻長時間 瀏覽:285
安全問題app哪個好 瀏覽:445
壓縮水會變冰嗎 瀏覽:526
小說配音app哪個靠譜 瀏覽:820
編譯iso 瀏覽:944
照片生成pdf格式 瀏覽:194
病歷轉pdf 瀏覽:835
雲伺服器配硬體 瀏覽:978
伺服器10k什麼意思 瀏覽:21
pdfeditor漢化 瀏覽:884
新科學pdf 瀏覽:746
現在還有c語言編譯嗎 瀏覽:675
哪裡買到單片機 瀏覽:480
linux文件打開數量 瀏覽:510
編譯原理中什麼是l屬性文法 瀏覽:372
硬碟加密時出現的問題 瀏覽:61
如何退域命令 瀏覽:108
看書的app哪裡看 瀏覽:291
伺服器怎麼調大 瀏覽:4