{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using LogisticRegression in a Pipeline\n", "\n", "This notebook shows how to use `LogisticRegression` in a scikit-learn `Pipeline` with preprocessing, such as `StandardScaler`, to handle binary classification.\n", "\n", "## Setup\n", "We use a synthetic dataset for reproducibility." ] }, { "cell_type": "code", "metadata": { "ExecuteTime": { "end_time": "2025-07-22T14:42:06.197077Z", "start_time": "2025-07-22T14:42:05.118559Z" } }, "source": [ "from sklearn.datasets import make_classification\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.metrics import accuracy_score\n", "from glmpynet import LogisticRegression\n", "\n", "# Generate synthetic dataset\n", "X, y = make_classification(n_samples=200, n_features=20, n_classes=2, random_state=42)\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", "\n", "# Create and fit pipeline\n", "pipeline = Pipeline([\n", " ('scaler', StandardScaler()),\n", " ('logistic_net', LogisticRegression())\n", "])\n", "pipeline.fit(X_train, y_train)\n", "\n", "# Predict and evaluate\n", "y_pred = pipeline.predict(X_test)\n", "accuracy = accuracy_score(y_test, y_pred)\n", "print(f\"Pipeline Accuracy: {accuracy:.2f}\")" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pipeline Accuracy: 0.88\n" ] } ], "execution_count": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explanation\n", "- The pipeline combines `StandardScaler` for feature scaling and `LogisticRegression` for classification.\n", "- The dataset is the same as in the basic example, ensuring consistency.\n", "- Accuracy is similar to the basic example but may improve slightly due to scaling.\n", "- With `glmnet`, expect comparable integration but potentially better performance on high-dimensional data." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 2 }