Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge 4: Cross-validation | Scikit-learn
Data Science Interview Challenge

book
Challenge 4: Cross-validation

Cross-validation is a pivotal technique in machine learning that aims to assess the generalization performance of a model on unseen data. Given the inherent risk of overfitting a model to a particular dataset cross-validation offers a solution. By partitioning the original dataset into multiple subsets, the model is trained on some of these subsets and tested on the others.

By rotating the testing fold and averaging the results across all iterations, we gain a more robust estimate of the model's performance. This iterative process not only provides insights into the model's potential variability and bias but also aids in mitigating overfitting, ensuring that the model has a balanced performance across different subsets of the data.

Uppgift

Swipe to start coding

Implement a pipeline that combines data preprocessing and model training. After establishing the pipeline, utilize cross-validation to assess the performance of a classifier on the Wine dataset.

  1. Create a pipeline that includes standard scaling and decision tree classifier.
  2. Apply 5-fold cross-validation on the pipeline.
  3. Calculate the average accuracy across all folds.

Lösning

from sklearn.datasets import load_wine
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score

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

# 1. Setup the pipeline
pipe = Pipeline([
('scaler', StandardScaler()),
('classifier', DecisionTreeClassifier(random_state=0))
])

# 2. Cross-validation
scores = cross_val_score(pipe, X, y, cv=5)

# 3. Average accuracy
average_accuracy = scores.mean()
print("Average Accuracy:", average_accuracy)

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 7. Kapitel 4
from sklearn.datasets import load_wine
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score

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

# 1. Setup the pipeline
pipe = Pipeline([
('scaler', ___),
('classifier', ___(random_state=0))
])

# 2. Cross-validation
scores = cross_val_score(___, cv=___)

# 3. Average accuracy
average_accuracy = scores.___
print("Average Accuracy:", average_accuracy)

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt