You are on page 1of 2

4/16/24, 10:48 PM ML Lab5.

ipynb - Colab

import matplotlib.pyplot as plt


from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_predict, train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import plot_tree
from sklearn.metrics import accuracy_score, classification_report

breast = load_breast_cancer()
X = breast.data
y = breast.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

rf = RandomForestClassifier(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)

▾ RandomForestClassifier
RandomForestClassifier(random_state=42)

y_pred = rf.predict(X_test)

print(classification_report(y_test, y_pred))

print(accuracy_score(y_test, y_pred))

precision recall f1-score support

0 0.98 0.93 0.95 43


1 0.96 0.99 0.97 71

accuracy 0.96 114


macro avg 0.97 0.96 0.96 114
weighted avg 0.97 0.96 0.96 114

0.9649122807017544

y_pred_c = cross_val_predict(rf, X, y, cv=5)

print(classification_report(y, y_pred_c))
print(accuracy_score(y, y_pred_c))

precision recall f1-score support

0 0.95 0.93 0.94 212


1 0.96 0.97 0.97 357

accuracy 0.96 569


macro avg 0.96 0.95 0.95 569
weighted avg 0.96 0.96 0.96 569

0.9560632688927944

plt.figure(figsize=(50,50))
for i in range(3):
plt.subplot(3, 1, i+1)
plot_tree(rf.estimators_[i], filled=True, feature_names=breast.feature_names, class_names=['Disease', "No Disease"])
plt.show()

https://colab.research.google.com/drive/1vPVh3WYBZOIvQJrxmedAf1cnVcxXl8Jh#scrollTo=sKVVlZ5JjRnn&printMode=true 1/2
4/16/24, 10:48 PM ML Lab5.ipynb - Colab

https://colab.research.google.com/drive/1vPVh3WYBZOIvQJrxmedAf1cnVcxXl8Jh#scrollTo=sKVVlZ5JjRnn&printMode=true 2/2

You might also like