Skip to content

Conversation

@d-kleine
Copy link
Contributor

@d-kleine d-kleine commented Oct 30, 2024

Description

Update numpy negative infinity constant for version compatibility for the sequential feature selector (SFS)

  • Add version check to handle different numpy versions
  • Use np.NINF for numpy <2.0 and -np.inf for numpy >=2.0
  • Replace direct np.NINF usage with version-aware variable
  • Maintain backward compatibility while supporting newer numpy versions

This change ensures the code works correctly across different numpy versions
while maintaining the same functionality.

Related issues or pull requests

fixes #1100 related issue
#1104 similar but incomplete PR

Pull Request Checklist

  • Added a note about the modification or contribution to the ./docs/sources/CHANGELOG.md file (if applicable)
  • Added appropriate unit test functions in the ./mlxtend/*/tests directories (if applicable)
  • Modify documentation in the corresponding Jupyter Notebook under mlxtend/docs/sources/ (if applicable)
  • Ran PYTHONPATH='.' pytest ./mlxtend -sv and make sure that all unit tests pass (for small modifications, it might be sufficient to only run the specific test file, e.g., PYTHONPATH='.' pytest ./mlxtend/classifier/tests/test_stacking_cv_classifier.py -sv)
  • Checked for style issues by running flake8 ./mlxtend

@d-kleine
Copy link
Contributor Author

Example code for testing:

import numpy as np
from sklearn.linear_model import LogisticRegression
from mlxtend.feature_selection import SequentialFeatureSelector as SFS

# Generate random values
np.random.seed(42)  # For reproducibility
n_samples = 100     # Number of samples
n_features = 10     # Number of features

# Generate random features (X) and binary target (y)
X_train = np.random.rand(n_samples, n_features)
y_train = np.random.randint(0, 2, size=n_samples)

# Now run Sequential Feature Selection
sfs = SFS(LogisticRegression(),
          k_features=5,          # number of features to select
          forward=True,          # forward selection
          floating=False,        # no floating selection
          scoring='accuracy',
          verbose=5,
          cv=5).fit(X_train, y_train)

@d-kleine d-kleine marked this pull request as ready for review October 30, 2024 19:44
@aghents
Copy link

aghents commented Oct 31, 2024

Great contribution

@d-kleine
Copy link
Contributor Author

d-kleine commented Nov 2, 2024

@rasbt ready for review

Copy link
Owner

@rasbt rasbt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for fixing this!

@rasbt rasbt merged commit a78bd0b into rasbt:master Nov 3, 2024
2 checks passed
@d-kleine d-kleine deleted the SFS_numpy2.0 branch November 5, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

np.NINF was removed in the NumPy 2.0 release. Use -np.inf instead

3 participants