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

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

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

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

ensemble voting

1. رأی‌گیری سخت (Hard Voting)

hard voting example

2. رأی‌گیری نرم (Soft Voting)

soft voting example

کد MATLAB

% 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);

% Define base models
t1 = templateTree('MaxNumSplits', 10); % Decision Tree
t2 = templateKNN('NumNeighbors', 5); % KNN
t3 = templateLinear('Learner', 'logistic'); % Logistic Regression

% Train Voting ensemble
votingModel = fitcensemble(X, Y_numeric, 'Method', 'Bag', ...
    'Learners', {t1, t2, t3}, 'NumLearningCycles', 3); % 3 base models

% 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(votingModel, 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('Voting Ensemble on Iris Dataset');
xlabel('Sepal length');
ylabel('Sepal width');
legend('Setosa', 'Versicolor', 'Virginica');
hold off;

کد Python

from sklearn.datasets import load_iris
from sklearn.ensemble import VotingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
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

# Define base models
clf1 = DecisionTreeClassifier(max_depth=10)
clf2 = KNeighborsClassifier(n_neighbors=5)
clf3 = LogisticRegression(max_iter=1000)

# Train Voting classifier (Hard Voting)
model = VotingClassifier(
    estimators=[('dt', clf1), ('knn', clf2), ('lr', clf3)],
    voting='hard'
)
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('Voting Ensemble on Iris Dataset')
plt.show()

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

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

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