生成符合以下要求的代码(1)数据集:Iris dataset (Raschka 2.2,3.2) (2)构建决策树 (3)测试 (4)参考:Raschka 3.6.2
时间: 2024-02-05 14:03:15 浏览: 112
Sure, I can help you create code that meets your requirements! Here is an example of code that meets these criteria:
```
# Import necessary libraries
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the iris dataset
iris = load_iris()
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=42)
# Create a decision tree classifier
clf = DecisionTreeClassifier()
# Fit the classifier to the training data
clf.fit(X_train, y_train)
# Predict the classes of the test set
y_pred = clf.predict(X_test)
# Calculate the accuracy of the classifier on the test set
accuracy = accuracy_score(y_test, y_pred)
# Print the accuracy of the classifier
print("Accuracy: {}".format(accuracy))
```
This code loads the Iris dataset, splits it into training and testing sets, creates a decision tree classifier, fits the classifier to the training data, predicts the classes of the test set, and calculates the accuracy of the classifier on the test set. The output of the code will be the accuracy of the classifier.
阅读全文