average_precision_score not working as expected

### Describe the bug

When compute AP with average_precision_score, I get unexpected results. The y_scores (output from the models) are very low for positive samples, so my AP should be very low. Instead I get a perfect 1.0 AP score.

### Steps/Code to Reproduce

```python
from sklearn.metrics import average_precision_score

# labels
y_true = [1, 1, 1, 1, 1]  # 5 positives
# Predicted scores
y_scores = [0.1, 0.3, 0.1, 0.2, 0.1]  # Model's confidence in predictions

# Calculate average precision
average_precision = average_precision_score(y_true, y_scores)
print("AP score:", average_precision)
```

### Expected Results

An AP score close to 0.

### Actual Results

AP score: 1.0

### Versions

```shell
System:
    python: 3.9.19 (main, May  6 2024, 19:43:03)  [GCC 11.2.0]
executable: /bin/python
   machine: Linux-5.15.0-121-generic-x86_64-with-glibc2.31

Python dependencies:
      sklearn: 1.5.2
          pip: 24.2
   setuptools: 75.1.0
        numpy: 1.26.4
        scipy: 1.13.1
       Cython: None
       pandas: 1.4.0
   matplotlib: 3.5.3
       joblib: 1.4.2
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 32
         prefix: libopenblas
       filepath: user/anaconda3/envs/perception39/lib/python3.9/site-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so
        version: 0.3.23.dev
threading_layer: pthreads
   architecture: SkylakeX

       user_api: blas
   internal_api: openblas
    num_threads: 32
         prefix: libopenblas
       filepath: user/anaconda3/envs/perception39/lib/python3.9/site-packages/scipy.libs/libopenblasp-r0-01191904.3.27.so
        version: 0.3.27
threading_layer: pthreads
   architecture: SkylakeX

       user_api: openmp
   internal_api: openmp
    num_threads: 32
         prefix: libgomp
       filepath: user/anaconda3/envs/perception39/lib/python3.9/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
```

COMMENT:
> contributions to this repository should follow its [contributing guidelines](/scikit-learn/scikit-learn/blob/46a7c9a5e4fe88dfdfd371bf36477f03498a3390/CONTRIBUTING.md), [security policy](/scikit-learn/scikit-learn/blob/46a7c9a5e4fe88dfdfd371bf36477f03498a3390/SECURITY.md), and [code of conduct](/scikit-learn/scikit-learn/blob/46a7c9a5e4fe88dfdfd371bf36477f03498a3390/CODE_OF_CONDUCT.md).



> > This would be the score of a DummyClassifier(strategy="constant", constant=1). It means that any predictor providing a score under this threshold do worse that a rule that does not learn any statistical association between the data and the target. So this is reasonable to me.
> 
> Not really because my application is Object Detection, where AP (or mAP) is the main metric used by most papers and benchmarks (e.g. COCO: https://cocodataset.org/#detection-eval). In that case you can't predict all positives (or you would have an infinite number of false positives). That's what I meant by saying that when the model has 0 confidence in my example, it means it missed completely the detection, so it should never be counted as a positive no matter the threshold. Recall can't be above 0.55 in this case.

Solutions: I hope it helps:
Threshold Adjustment: The default classification threshold of 0.5 might not be optimal. If the predicted scores for positive samples are low, lowering the threshold (e.g., to 0.3 or 0.4) could help the model classify more positives, increasing recall and potentially improving your results.
Ranking Confidence: Even if the AP score is high, it could be due to ranking. The model might still be under confident in its positive predictions, so it ranks them higher but with low confidence. To fix this, you could apply calibration methods like Platt Scaling or Isotonic Regression, which adjust the predicted probabilities and make the model more confident about its positive predictions.
Precision-Recall Trade-off: Use a Precision-Recall curve to check how precision and recall vary with different thresholds. You might find that lowering the threshold increases recall but could reduce precision. It’s important to balance these for the best model performance.
Imbalanced Data: If your dataset is imbalanced (more negatives than positives), the model might prioritize negatives. Balancing the dataset (e.g., by oversampling the positives or using class weights) could help the model focus more on predicting positives correctly.

Labels: Bug