Lightning-AI / Lightning-AI/torchmetrics

Add weights for the pearson, spearman, and r2_score

Open
#1,235 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement good first issue
Dominant language
Python
Stars
2.5k
Forks
526
Avg merge
6d 11h
Merged PRs (30d)
5

Description

## 🚀 Feature

We can provide a weight Tensor to the regression coefficients, such as pearson, spearman, and r2_score

### Motivation

It should be relatively simple to add weights to these computations. And it can be useful in many contexts, including masking by providing 0-weights, or adding more weights to the relevant sample/target pairs.

### Pitch

Adding `weights` parameter in `pearson`, `spearman`, and `r2_score`. The parameter `weights` should be either `None`, 1D ,or 2D.

### Alternatives

None

### Additional context

See [weighted pearsonr](https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#Weighted_correlation_coefficient). For the spearmanr, it should be identical, since spearman is the correlation of the rank.

For the r2_score, there exist some implementations for example in [sklearn](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html), but it would be better to provide either a 1D or 2D matrix, and it would be broadcasted to the same shape as preds / target. instead of forcing `sample_weight` to be 1D.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the pearson, spearman, and r2_score metric entry points and their existing tests. Review the weighted Pearson definition and the linked sklearn r2_score behavior, then define coverage for weights=None and 1D or 2D weights broadcast to preds/target; done means all three metrics support these cases with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.