Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeClassifier, plot_tree
- from sklearn.metrics import accuracy_score, confusion_matrix
- import matplotlib.pyplot as plt
- 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
- )
- clf = DecisionTreeClassifier(criterion="gini", max_depth=3, random_state=42)
- clf.fit(X_train, y_train)
- y_pred = clf.predict(X_test)
- plt.figure(figsize=(12,8))
- plot_tree(clf, filled=True, feature_names=iris.feature_names, class_names=iris.target_names)
- plt.show()
- accuracy = accuracy_score(y_test, y_pred)
- print("Model Accuracy on Test Data:", accuracy)
- cm = confusion_matrix(y_test, y_pred)
- print("Confusion Matrix:\n", cm)
- new_sample = [[6.0, 2.9, 4.5, 1.5]]
- prediction = clf.predict(new_sample)
- predicted_class = iris.target_names[prediction[0]]
- print("New sample:", new_sample)
- print("Predicted class:", predicted_class)
Advertisement
Add Comment
Please, Sign In to add comment