2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The decision tree algorithm is a very popular machine learning algorithm that can be used for classification and regression tasks. The following is a detailed introduction to the decision tree algorithm, including its principles and case implementation, as well as the corresponding Python code.
A decision tree is a tree-like structure used to classify or regress data. It consists of nodes and edges, where each internal node represents a test of a feature, each branch represents the result of the test, and each leaf node represents a category or regression value.
The process of building a decision tree usually includes the following steps:
The following is a decision tree classification case implemented using Python and the scikit-learn library. We will use the famous Iris dataset, which contains the features and categories of three types of irises (Setosa, Versicolour, Virginica).
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
-
- # 加载数据集
- iris = load_iris()
- X, y = iris.data, iris.target
-
- # 拆分数据集为训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
- from sklearn.tree import DecisionTreeClassifier
-
- # 初始化决策树分类器
- clf = DecisionTreeClassifier()
-
- # 训练模型
- clf.fit(X_train, y_train)
- from sklearn.metrics import accuracy_score
-
- # 预测测试集
- y_pred = clf.predict(X_test)
-
- # 计算准确率
- accuracy = accuracy_score(y_test, y_pred)
- print(f"Accuracy: {accuracy:.2f}")
- import matplotlib.pyplot as plt
- from sklearn.tree import plot_tree
-
- # 可视化决策树
- plt.figure(figsize=(12, 12))
- plot_tree(clf, filled=True, feature_names=iris.feature_names, class_names=iris.target_names)
- plt.show()
The above code shows how to use the scikit-learn library to load the Iris dataset, train a decision tree classifier, evaluate the model performance, and visualize the decision tree. Through this case, you can see how the decision tree works and how to use it in practical applications.