Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Solving Task Using Regularisation | Machine Learning Techniques
Data Anomaly Detection

book
Challenge: Solving Task Using Regularisation

Aufgabe

Swipe to start coding

Your task is to create a classification model using L2 regularization on the breast_cancer dataset. It contains features computed from a digitized image of a fine needle aspirate (FNA) of a breast mass. The task associated with this dataset is to classify the breast mass as malignant (cancerous) or benign (non-cancerous) based on the extracted features.

Your task is to:

  1. Specify argument at the LogisticRegression() constructor:
    • specify penalty argument equal to l2;
    • specify C argument equal to 1.
  2. Fit regularized model on the training data.

Lösung

import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
import warnings

# Ignore warnings
warnings.filterwarnings("ignore")

# Load the Breast Cancer dataset as an example of a more complex classification dataset
data = load_breast_cancer()
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 a Logistic Regression model with L2 regularization (ridge)
logistic_reg = LogisticRegression(penalty="l2", C=1, random_state=42)

# Fit the model on the training data
logistic_reg.fit(X_train, y_train)

# Predict on the test data
y_pred = logistic_reg.predict(X_test)

# Calculate F1-score and display the confusion matrix
f1 = f1_score(y_test, y_pred)
print(f"F1-Score: {f1}")

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
import warnings

# Ignore warnings
warnings.filterwarnings("ignore")

# Load the Breast Cancer dataset as an example of a more complex classification dataset
data = load_breast_cancer()
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 a Logistic Regression model with L2 regularization (ridge)
logistic_reg = LogisticRegression(___="l2", C=___, random_state=42)

# Fit the model on the training data
logistic_reg.___(X_train, y_train)

# Predict on the test data
y_pred = logistic_reg.predict(X_test)

# Calculate F1-score and display the confusion matrix
f1 = f1_score(y_test, y_pred)
print(f"F1-Score: {f1}")
toggle bottom row
some-alt