agnelvishal / agnelvishal/auto_sklearn2
AttributeError: This 'Pipeline' has no attribute 'predict_proba'
- Langage dominant
- Python
- Étoiles
- 6
- Forks
- 0
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
I tried to use auto_sklearn2 library for oil fingerprint identificvation from GCXGC image in jpeg. However, I got the following attribution errors "AttributeError: 'LinearSVC' object has no attribute 'predict_proba'" and
"AttributeError: This 'Pipeline' has no attribute 'predict_proba'" when I use the function
` p = self.rf.predict_proba(np.array([[d,nok,n1,n2]]))[0]`
This happened after defining self.rf using AutoSklearnClassifier according to the class defnition:
```
class GCxGC():
self.rf = AutoSklearnClassifier(time_limit=30, random_state=42)
```
I tried to restrict probabilistic classifiers that support predict_proba in the following manner:
```
# Restrict to classifiers that support predict_proba
X = np.array([[d, nok, n1, n2]])
p = safe_predict_proba(self.rf, X)[0]
#########################################################################
def safe_predict_proba(clf, X):
# Always return probability-like predictions from a pipeline.
# Works for Auto-Sklearn pipelines even if classifier is LinearSVC or other non-probabilistic.
# Get the last step of the pipeline
try:
# Try to get probabilities from a model in a robust way.
# Falls back to decision_function or predict if needed.
# If clf is a pipeline, get the final step
if isinstance(clf, Pipeline):
model = clf.steps[-1][1]
else:
model = clf
# Case 1: model has predict_proba
if hasattr(model, "predict_proba"):
return model.predict_proba(X)
# Case 2: model has decision_function
elif hasattr(model, "decision_function"):
scores = model.decision_function(X)
if scores.ndim == 1:
from scipy.special import expit
probs = expit(scores)
return np.vstack([1 - probs, probs]).T
else:
from sklearn.utils.extmath import softmax
return softmax(scores)
# Case 3: fallback → use predict() and convert to one-hot
elif hasattr(model, "predict"):
preds = model.predict(X)
if hasattr(model, "classes_"):
classes = model.classes_
else:
classes = np.unique(preds)
one_hot = np.zeros((len(preds), len(classes)))
for i, c in enumerate(preds):
one_hot[i, np.where(classes == c)[0][0]] = 1.0
return one_hot
else:
raise RuntimeError(f"Model {type(model)} has no method to produce probabilities")
except Exception as e:
raise RuntimeError(f"Failed to compute probabilities: {e}")
#######################################################################
probabilistic_classifiers = [
"random_forest",
"gradient_boosting",
"k_nearest_neighbors",
"logistic_regression",
"multinomial_nb",
"qda",
"sgd", # only if loss is 'log' (logistic regression)
"extra_trees",
"mlp"
]
class GCxGC():
self.rf = AutoSklearnClassifier(time_limit=30, include={"classifier": probabilistic_classifiers},random_state=42) # error
```
However, it is not working since it has given the following error:
> TypeError: AutoSklearnClassifier.__init__() got an unexpected keyword argument 'include'
This is critical since I want to restrict probabilistic_classifiers of AutoSklearnClassifier to this choice
```
# Restrict to classifiers that support predict_proba
probabilistic_classifiers = [
"random_forest",
"gradient_boosting",
"k_nearest_neighbors",
"logistic_regression",
"multinomial_nb",
"qda",
"sgd", # only if loss is 'log' (logistic regression)
"extra_trees",
"mlp"
]
```
Any sugggeston would be appreciated
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.