# Breast Cancer Classification using Logistic Regression and Random Forest
from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, precision_score, recall_score, f1_score,
roc_auc_score, confusion_matrix, classification_report
# Load dataset
data = load_breast_cancer()
X, y = [Link], [Link]
# Split data into training and testing sets (80-20)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42,
stratify=y)
# Feature scaling (for Logistic Regression)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)
# Initialize models
log_reg = LogisticRegression(max_iter=10000, random_state=42)
rf = RandomForestClassifier(n_estimators=100, random_state=42)
# Train models
log_reg.fit(X_train_scaled, y_train)
[Link](X_train, y_train)
# Make predictions
y_pred_log = log_reg.predict(X_test_scaled)
y_pred_rf = [Link](X_test)
# Evaluate models
def evaluate_model(y_true, y_pred, model_name):
acc = accuracy_score(y_true, y_pred)
prec = precision_score(y_true, y_pred)
rec = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
auc = roc_auc_score(y_true, y_pred)
cm = confusion_matrix(y_true, y_pred)
print(f"\n{model_name} Performance:")
print(f"Accuracy: {acc:.4f}")
print(f"Precision: {prec:.4f}")
print(f"Recall: {rec:.4f}")
print(f"F1-score: {f1:.4f}")
print(f"ROC-AUC: {auc:.4f}")
print("Confusion Matrix:")
print(cm)
print("\nClassification Report:")
print(classification_report(y_true, y_pred))
# Print performance
evaluate_model(y_test, y_pred_log, "Logistic Regression")
evaluate_model(y_test, y_pred_rf, "Random Forest")