# Q33. End-to-End Machine Learning Pipeline
# Dataset: Breast Cancer Classification

# Step 1: Import Libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder, LabelEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
    accuracy_score, precision_score, recall_score,
    f1_score, confusion_matrix, classification_report
)

# Step 2: Load Dataset
df = pd.read_csv("breast_cancer.csv")

print("First Five Records:")
print(df.head())

print("\nDataset Shape:", df.shape)
print("\nData Types:")
print(df.dtypes)

print("\nMissing Values:")
print(df.isnull().sum())

print("\nSummary Statistics:")
print(df.describe(include="all"))

# Step 3: Define Target and Features
# Change "diagnosis" if your target column has a different name.
target = "diagnosis"

# Remove common ID columns if present
df = df.drop(columns=["id", "ID", "Unnamed: 32"], errors="ignore")

X = df.drop(columns=[target])
y = df[target]

# Encode target labels if they are text
if not pd.api.types.is_numeric_dtype(y):
    y = LabelEncoder().fit_transform(y)

# Step 4: Identify Numerical and Categorical Features
numeric_features = X.select_dtypes(include="number").columns.tolist()
categorical_features = X.select_dtypes(exclude="number").columns.tolist()

print("\nNumerical Features:", numeric_features)
print("Categorical Features:", categorical_features)

# Step 5: Split Dataset Before Preprocessing
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# Step 6: Preprocessing Pipelines
numeric_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="median")),
    ("scaler", StandardScaler())
])

categorical_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(handle_unknown="ignore"))
])

preprocessor = ColumnTransformer([
    ("num", numeric_pipeline, numeric_features),
    ("cat", categorical_pipeline, categorical_features)
])

# Step 7: Create Complete ML Pipeline
pipeline = Pipeline([
    ("preprocessing", preprocessor),
    ("model", RandomForestClassifier(random_state=42))
])

# Step 8: Hyperparameter Tuning Using Cross-Validation
parameters = {
    "model__n_estimators": [100, 200],
    "model__max_depth": [None, 5, 10],
    "model__min_samples_split": [2, 5]
}

grid_search = GridSearchCV(
    pipeline,
    parameters,
    cv=5,
    scoring="f1_weighted",
    n_jobs=-1
)

grid_search.fit(X_train, y_train)

print("\nBest Parameters:")
print(grid_search.best_params_)

print("Best Cross-Validation F1-Score:", grid_search.best_score_)

# Step 9: Evaluate Final Model on Test Data
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)

print("\nTest Accuracy:", accuracy_score(y_test, y_pred))
print("Test Precision:", precision_score(
    y_test, y_pred, average="weighted", zero_division=0
))
print("Test Recall:", recall_score(
    y_test, y_pred, average="weighted", zero_division=0
))
print("Test F1-Score:", f1_score(
    y_test, y_pred, average="weighted", zero_division=0
))

print("\nClassification Report:")
print(classification_report(y_test, y_pred, zero_division=0))

# Step 10: Confusion Matrix
cm = confusion_matrix(y_test, y_pred)

sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Breast Cancer Classification - Confusion Matrix")
plt.show()

# Step 11: Conclusion
print("\nEnd-to-end machine learning pipeline completed.")














