Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

NSE and R2 return 1.0 when model values are NaN

オープン 初心者向け
#720 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
numpy, pandas, python
領域
analytics

調査の方向性

src/modelskill/metrics.py の nash_sutcliffe_efficiency から始めてください。入力に対して np.sum が適用されている箇所です。r2 も同じ経路を利用しています。Issue にある pandas Series の例で再現し、既に NaN を返す NumPy 配列での呼び出しと比較してください。モデル値が NaN のとき、両方の入力型で NaN が返り、既存のメトリクステストが引き続き通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

nse and r2 return a perfect 1.0 when model values are missing. rmse, bias, mae, kge and cc return NaN for the same input.

import numpy as np, pandas as pd, modelskill as ms

t = pd.date_range("2026-01-01", periods=4, freq="h")
df = pd.DataFrame({"obs": [1.0, 2, 3, 4], "mod": [1.0, 2, np.nan, np.nan]}, index=t)
cmp = ms.from_matched(df, obs_item="obs")
print(cmp.skill(metrics=["nse", "rmse"]).to_dataframe())
#              n  nse  rmse
# observation
# obs          4  1.0   NaN

df["mod"] = np.nan
print(ms.from_matched(df, obs_item="obs").score(metric="nse"))
# {'mod': 1.0}

The same happens calling the metric directly on pandas Series. With NumPy arrays it returns NaN:

obs = pd.Series([1.0, 2, 3, 4])
mod = pd.Series([1.0, 2, np.nan, np.nan])
ms.metrics.nse(obs, mod)                    # 1.0
ms.metrics.nse(obs.values, mod.values)      # nan

Cause

nash_sutcliffe_efficiency uses np.sum on its inputs, which for a pandas Series skips NaN. The numerator then sums only the points where the model has a value, while the denominator sums every observation. The result is not the NSE of the non-missing points either; it overstates the fit, and an all-NaN model scores 1.0. r2 inherits it.

Expected

NaN, as the other metrics return. A model run that failed part-way, and so returns NaN after the failure, should not score as a perfect fit.

Converting the inputs with np.asarray before summing would make it consistent with the other metrics.

modelskill 1.4.0, numpy 2.4.6, pandas 2.3.3. The code is unchanged on main.

主要言語
Python
スター
58
フォーク
9
平均マージ
28分
マージ済み PR(30日)
1

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

DHI/modelskill のほかの issue

DHI/modelskill の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。