Course Content
Foundations of Machine Learning Track Overview
Foundations of Machine Learning Track Overview
Ensemble Learning
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_breast_cancer from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Load the Breast Cancer dataset data = load_breast_cancer() X, y = data.data[:, :2], data.target # We use the first two features for visualization # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create Random Forest Classifier rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42) # Train the classifier rf_classifier.fit(X_train, y_train) # Make predictions predictions = rf_classifier.predict(X_test) # Calculate accuracy accuracy = accuracy_score(y_test, predictions) print("Accuracy:", accuracy) # Visualization x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) Z = rf_classifier.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', marker='o', s=70, linewidth=1, cmap=plt.cm.Paired) plt.xlabel('Feature 1') plt.ylabel('Feature 2') plt.title('Random Forest Classifier Decision Boundaries (Breast Cancer Dataset)') plt.show()
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 6