| # NMF Compatibility Fix | |
| ## Problem | |
| The `NMFRiskDiscovery` class initialization failed with: | |
| ``` | |
| TypeError: NMF.__init__() got an unexpected keyword argument 'alpha' | |
| ``` | |
| ## Root Cause | |
| The scikit-learn `NMF` class has different parameter names across versions: | |
| - **scikit-learn < 0.19**: No regularization parameters | |
| - **scikit-learn 0.19-0.24**: Uses `alpha` and `l1_ratio` | |
| - **scikit-learn >= 1.0**: Uses `alpha_W`, `alpha_H`, and `l1_ratio` | |
| The code was using the old `alpha` parameter which doesn't exist in newer versions. | |
| ## Solution | |
| Implemented version detection to use the correct parameters: | |
| ```python | |
| import sklearn | |
| sklearn_version = tuple(map(int, sklearn.__version__.split('.')[:2])) | |
| nmf_params = { | |
| 'n_components': n_components, | |
| 'random_state': random_state, | |
| 'init': 'nndsvda', | |
| 'max_iter': 500 | |
| } | |
| # Add regularization params if supported | |
| if sklearn_version >= (1, 0): | |
| # scikit-learn >= 1.0 | |
| nmf_params['alpha_W'] = 0.1 | |
| nmf_params['alpha_H'] = 0.1 | |
| nmf_params['l1_ratio'] = 0.5 | |
| elif sklearn_version >= (0, 19): | |
| # scikit-learn 0.19 to 0.24 | |
| nmf_params['alpha'] = 0.1 | |
| nmf_params['l1_ratio'] = 0.5 | |
| # else: very old version, use basic params only | |
| self.nmf_model = NMF(**nmf_params) | |
| ``` | |
| ## Testing | |
| Run the comparison script again: | |
| ```bash | |
| python3 compare_risk_discovery.py --advanced | |
| ``` | |
| All 9 methods should now work correctly across different scikit-learn versions. | |
| ## Files Modified | |
| - `risk_discovery_alternatives.py`: Fixed `NMFRiskDiscovery.__init__()` method | |