GamerBhai02

ML Exp 4

Sep 29th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | Source Code | 0 0
  1. from sklearn.datasets import load_iris
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.tree import DecisionTreeClassifier, plot_tree
  4. from sklearn.metrics import accuracy_score, confusion_matrix
  5. import matplotlib.pyplot as plt
  6.  
  7. iris = load_iris()
  8. X, y = iris.data, iris.target
  9.  
  10. X_train, X_test, y_train, y_test = train_test_split(
  11.     X, y, test_size=0.3, random_state=42
  12. )
  13.  
  14. clf = DecisionTreeClassifier(criterion="gini", max_depth=3, random_state=42)
  15. clf.fit(X_train, y_train)
  16.  
  17. y_pred = clf.predict(X_test)
  18.  
  19. plt.figure(figsize=(12,8))
  20. plot_tree(clf, filled=True, feature_names=iris.feature_names, class_names=iris.target_names)
  21. plt.show()
  22.  
  23. accuracy = accuracy_score(y_test, y_pred)
  24. print("Model Accuracy on Test Data:", accuracy)
  25.  
  26. cm = confusion_matrix(y_test, y_pred)
  27. print("Confusion Matrix:\n", cm)
  28.  
  29. new_sample = [[6.0, 2.9, 4.5, 1.5]]
  30. prediction = clf.predict(new_sample)
  31. predicted_class = iris.target_names[prediction[0]]
  32.  
  33. print("New sample:", new_sample)
  34. print("Predicted class:", predicted_class)
Tags: ML
Advertisement
Add Comment
Please, Sign In to add comment