File size: 1,520 Bytes
9b1c753
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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