Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

NSE and R2 return 1.0 when model values are NaN

Đang mở Phù hợp với người mới
#720 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

  • #721 của @syahra712 — đã đóng, không merge

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
numpy, pandas, python
Lĩnh vực
analytics

Hướng nghiên cứu

Bắt đầu từ src/modelskill/metrics.py, trong nash_sutcliffe_efficiency, nơi np.sum được áp dụng cho các đầu vào; r2 dựa trên cùng đường đi đó. Tái hiện lỗi bằng ví dụ pandas Series trong issue và so sánh với lệnh gọi bằng mảng NumPy, vốn đã trả về NaN. Hoàn thành khi cả hai kiểu đầu vào đều trả về NaN với giá trị NaN của mô hình và các bài kiểm thử metric hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
58
Fork
10
Merge trung bình
28 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của DHI/modelskill

Tất cả issue của DHI/modelskill

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.