Challenge: Solving Task Using Regularisation
Завдання
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:
- Specify argument at the
LogisticRegression()
constructor:- specify
penalty
argument equal tol2
; - specify
C
argument equal to1
.
- specify
- Fit regularized model on the training data.
Рішення
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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}")
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 3. Розділ 4
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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}")
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат