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:
It seems there’s some confusion regarding the results of the average_precision_score when the predicted scores for positive samples are low. The key thing to note here is that Average Precision (AP) is calculated based on the ranking of predictions, not just the predicted scores.
Here's what might be happening:
Ranking of Predictions: AP doesn’t rely solely on whether the prediction score is high or low, but on the order of predictions. In this case, even though the predicted scores for the positive samples are low (e.g., 0.1, 0.3, etc.), as long as these low scores are ranked above the negative samples, they will contribute to a higher AP score.
High AP despite Low Scores: If your positive samples (label = 1) are ranked higher (i.e., in the top positions when sorted by the predicted score), even if the individual predicted scores are low (0.1, 0.2, etc.), the average precision will be higher because the ranking correctly identifies the positive samples earlier in the list of predictions. In this case, you see a high AP score (e.g., 1.0) because the order of positive samples is correct, even if their predicted scores are not very high.
Precision and Recall in Ranking: The ranking is more important in AP than the raw values of the scores themselves. If the predicted positive samples (even with low scores) are ranked at the top, the precision (correct positive predictions) is high, leading to a higher AP. So the AP score of 1.0 that you’re seeing is due to the ranking of your predictions, where the model is correctly identifying positive samples at the top, even if their individual scores are low.

Solution:
Handling Imbalanced Data: If your dataset is imbalanced, the model may focus too much on predicting negatives. You can balance the data by oversampling the positive class, undersampling the negative class, or using class weights during training to prioritize positive samples.
Adjusting the Classification Threshold: The default threshold is 0.5, but lowering it (e.g., to 0.3) can help the model classify more positives, improving recall. A lower threshold can improve ranking of positive samples, even if their predicted scores are low.
Threshold Selection via Precision-Recall Curve: Use the precision-recall curve to balance precision and recall at different thresholds. A lower threshold increases recall but may reduce precision, so ensure you're not introducing too many false positives.
Model Calibration: If the model outputs low probabilities for positives, it may be underconfident. Applying calibration techniques like Platt scaling or isotonic regression can adjust predictions to better reflect the true likelihood of positives.
Hyperparameter Tuning: Perform hyperparameter tuning (e.g., grid search) to optimize model settings, improving its ability to predict positives.

Labels: Bug