یادگیری ترکیبی Bagging

یادگیری ترکیبی Bagging

bagging
هوش مصنوعي یادگیری گروهی یادگیری ماشین

یادگیری ترکیبی Bagging

جنگل تصادفی
%MATLAB CODE 
% Load Iris dataset
load fisheriris
X = meas(:, 1:2); % Use first two features: sepal length & sepal width
Y = species;

% Convert species to numeric labels
Y_numeric = grp2idx(Y);

% Train Bagging ensemble
bagModel = fitcensemble(X, Y_numeric, 'Method', 'Bag', ...
    'Learners', templateTree(), 'NumLearningCycles', 10);

% Create grid for decision boundary
[x1Grid, x2Grid] = meshgrid(linspace(min(X(:,1))-1, max(X(:,1))+1, 100), ...
                            linspace(min(X(:,2))-1, max(X(:,2))+1, 100));
XGrid = [x1Grid(:), x2Grid(:)];
predictedLabels = predict(bagModel, XGrid);

% Plot
figure;
gscatter(X(:,1), X(:,2), Y_numeric, 'rgb', 'o', 8);
hold on;
contourf(x1Grid, x2Grid, reshape(predictedLabels, size(x1Grid)), ...
    'LineColor', 'none', 'FaceAlpha', 0.3);
title('Bagging Decision Trees on Iris Dataset');
xlabel('Sepal length');
ylabel('Sepal width');
legend('Setosa','Versicolor','Virginica');
hold off;

# PTHON CODE 
from sklearn.datasets import load_iris
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import numpy as np

# Load Iris dataset
iris = load_iris()
X = iris.data[:, :2]  # Use first two features: sepal length & sepal width
y = iris.target

# Train Bagging classifier
model = BaggingClassifier(
    base_estimator=DecisionTreeClassifier(),
    n_estimators=10,
    random_state=0
)
model.fit(X, y)

# Plot decision boundaries
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.02),
                     np.arange(y_min, y_max, 0.02))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, alpha=0.4, cmap=plt.cm.coolwarm)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolor='k', cmap=plt.cm.coolwarm)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.title('Bagging Decision Trees on Iris Dataset')
plt.show()

فکر خود را اینجا بگذارید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

زمینه‌های نمایش داده شده را انتخاب نمایید. بقیه مخفی خواهند شد. برای تنظیم مجدد ترتیب، بکشید و رها کنید.
  • تصویر
  • شناسۀ محصول
  • امتیاز
  • قيمت
  • موجودی
  • دسترسی
  • افزودن به سبد خرید
  • توضیح
  • محتوا
  • وزن
  • اندازه
  • اطلاعات اضافی
برای مخفی‌کردن نوار مقایسه، بیرون را کلیک نمایید
مقایسه