Sling Academy
Home/Scikit-Learn/Logistic Regression with Cross-Validation in Scikit-Learn

Logistic Regression with Cross-Validation in Scikit-Learn

Last updated: December 17, 2024

In the realm of machine learning, logistic regression is a widely used algorithm for classification tasks. With the help of Scikit-Learn, an adaptable and robust library in Python, implementing logistic regression becomes both straightforward and powerful. Cross-validation further optimizes this model by assessing its performance on unseen data, thus preventing overfitting. This article will guide you through logistic regression implementation with cross-validation using Scikit-Learn.

Understanding Logistic Regression

Logistic regression is a statistical method for analyzing datasets in which there are one or more independent variables that determine an outcome. The outcome is typically binary, meaning there are only two possible classes. For instance, logistic regression can be used to identify whether an email is spam or not, based on features within the email itself.

Introduction to Scikit-Learn

Scikit-Learn is a free software machine learning library for the Python programming language. It features various classification, regression, and clustering algorithms, including logistic regression, SVM, random forests, and others. More importantly, it provides implementation for evaluation techniques like cross-validation. Here is how you can utilize it.

Installing Scikit-Learn

Before proceeding, ensure Scikit-Learn is installed in your Python environment. You can do this easily using pip:

pip install scikit-learn

Implementing Logistic Regression

Let's dive into the code for implementing Logistic Regression using Scikit-Learn. In this example, we'll use a simple dataset and demonstrate both the fitting of the model and the cross-validation evaluation process.

1. Import Libraries

First, import the necessary libraries:

import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris

2. Load Dataset

We'll use the Iris dataset which is available directly in Scikit-Learn:

# Load dataset
iris = load_iris()
X = iris.data
y = iris.target

3. Split the Dataset

It’s crucial to split your data into training and test sets. This ensures an unbiased evaluation of the model:

# Split the dataset 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)

4. Initialize Logistic Regression Model

We’ll create the Logistic Regression model:

# Initialize the Logistic Regression model
model = LogisticRegression(max_iter=200)

5. Perform Cross-Validation

Cross-validation helps in verifying that our model isn't overfitting. Here, we will use a common method called k-fold cross-validation:

# Evaluate using 10-fold cross-validation
evaluations = cross_val_score(model, X_train, y_train, cv=10)
print('Cross-validation scores:', evaluations)
print('Mean cross-validation score:', np.mean(evaluations))

6. Fit and Predict

Finally, we'll fit the model on the training data and make predictions on the test data:

# Fit the model
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)
print('Predicted values:', y_pred)

Conclusion

In conclusion, Logistic Regression remains one of the most vital techniques for classification in machine learning. When enhanced with cross-validation, it becomes a more robust model capable of better generalization. Using Scikit-Learn, both beginners and advanced practitioners can implement these techniques efficiently, benefiting from its high-level operations and consistent API. By incorporating logistic regression and k-folds cross-validation, you maximize the predictive power of your model while ensuring it is adequately evaluated.

Next Article: Elastic Net Regression in Scikit-Learn

Previous Article: Using Scikit-Learn's `RBFSampler` for Kernel Approximation

Series: Scikit-Learn Tutorials

Scikit-Learn

You May Also Like

  • Generating Gaussian Quantiles with Scikit-Learn
  • Spectral Biclustering with Scikit-Learn
  • Scikit-Learn Complete Cheat Sheet
  • ValueError: Estimator Does Not Support Sparse Input in Scikit-Learn
  • Scikit-Learn TypeError: Cannot Broadcast Due to Shape Mismatch
  • AttributeError: 'dict' Object Has No Attribute 'predict' in Scikit-Learn
  • KeyError: Missing 'param_grid' in Scikit-Learn GridSearchCV
  • Scikit-Learn ValueError: 'max_iter' Must Be Positive Integer
  • Fixing Log Function Error with Negative Values in Scikit-Learn
  • RuntimeError: Distributed Computing Backend Not Found in Scikit-Learn
  • Scikit-Learn TypeError: '<' Not Supported Between 'str' and 'int'
  • AttributeError: GridSearchCV Has No Attribute 'fit_transform' in Scikit-Learn
  • Fixing Scikit-Learn Split Error: Number of Splits > Number of Samples
  • Scikit-Learn TypeError: Cannot Concatenate 'str' and 'int'
  • ValueError: Cannot Use 'predict' Before Fitting Model in Scikit-Learn
  • Fixing AttributeError: NoneType Has No Attribute 'predict' in Scikit-Learn
  • Scikit-Learn ValueError: Cannot Reshape Array of Incorrect Size
  • LinAlgError: Matrix is Singular to Machine Precision in Scikit-Learn
  • Fixing TypeError: ndarray Object is Not Callable in Scikit-Learn