Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Solving Task Using AdaBoost Classifier | Commonly Used Boosting Models
Ensemble Learning

book
Challenge: Solving Task Using AdaBoost Classifier

Opgave

Swipe to start coding

The load_wine dataset is a classic example used for classification tasks. It consists of 178 samples, each representing a different wine cultivar. The dataset comprises 13 numerical attributes that describe various chemical characteristics of the wines, including features like alcohol content, malic acid concentration, and ash content. The target variable consists of three distinct classes representing the three different cultivars.

Your task is to use AdaBoost Classifier to solve the classification problem on the load_wine dataset:

  1. Split data into train and test sets.
  2. Use the AdaBoostClassifier() constructor to create the model with 50 base estimators.

Note

If we don't specify the base model of AdaBoostClassifer, the Decision Tree Classifier will be used by default.

Løsning

from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import f1_score

# Load the Wine dataset
data = load_wine()
X = data.data
y = data.target

# 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 and train the AdaBoost Classifier
classifier = AdaBoostClassifier(n_estimators=50)
classifier.fit(X_train, y_train)

# Make predictions
y_pred = classifier.predict(X_test)

# Calculate F1 score
f1 = f1_score(y_test, y_pred, average='weighted')
print(f'F1 Score: {f1:.4f}')

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 2
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import f1_score

# Load the Wine dataset
data = load_wine()
X = data.data
y = data.target

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = ___(X, y, test_size=0.2, random_state=42)

# Create and train the AdaBoost Classifier
classifier = ___(___=50)
classifier.fit(X_train, y_train)

# Make predictions
y_pred = classifier.predict(X_test)

# Calculate F1 score
f1 = f1_score(y_test, y_pred, average='weighted')
print(f'F1 Score: {f1:.4f}')
toggle bottom row
some-alt