Call to estimator.score() in RANSAC even if the produced input data set is too small - fix proposed

### Describe the bug

By sampling radomly from a distribution of x,y values, the RANSAC algorithm tries to find the line which describes best most of the data (while ignoring outliers). For deciding, how well a certain line describes the data, estimator.score() is called in line 526 of _ransac.py.

    525 # score of inlier data set
--> 526 score_subset = estimator.score(
    527     X_inlier_subset,
    528     y_inlier_subset,
    529     **score_params_inlier_subset,
    530 )

However, estimator.score() is called even if the inlier data set contains only a single data point, which then triggers a warning down the road:

...\Lib\site-packages\sklearn\metrics\_regression.py:1283, in r2_score(y_true, y_pred, sample_weight, multioutput, force_finite)
   1281 if _num_samples(y_pred) < 2:
   1282     msg = "R^2 score is not well-defined with less than two samples."
-> 1283     warnings.warn(msg, UndefinedMetricWarning)
   1284     return float("nan")

From my point of view, the code should not call this function if the input data set is not valid.

Unfortunately, I cannot provide a minimum example code as due to the random sampling the warning does only rarely occur. As far as I understand, the situation occurs if in previous iterations there was already a data set with more than 1 inlier data point drawn. Otherwise, line 526 is not reached due to:

510  # less inliers -> skip current random sample
511  if n_inliers_subset < n_inliers_best:
512     self.n_skips_no_inliers_ += 1
513     continue

If propose to modify this line to:

511 if ((n_inliers_subset < n_inliers_best) or (n_inliers_subset < 2)):

At least for me, this does fix the situation that generates the warnings.


### Steps/Code to Reproduce

            # less inliers -> skip current random sample
            if n_inliers_subset < n_inliers_best: # proposed fix: if ((n_inliers_subset < n_inliers_best) or (n_inliers_subset < 2)):
                self.n_skips_no_inliers_ += 1
                continue

            # extract inlier data set
            inlier_idxs_subset = sample_idxs[inlier_mask_subset]
            X_inlier_subset = X[inlier_idxs_subset]
            y_inlier_subset = y[inlier_idxs_subset]

            # cut `fit_params` down to `inlier_idxs_subset`
            score_params_inlier_subset = _check_method_params(
                X, params=routed_params.estimator.score, indices=inlier_idxs_subset
            )

            # score of inlier data set
            score_subset = estimator.score(
                X_inlier_subset,
                y_inlier_subset,
                **score_params_inlier_subset,
            )

### Expected Results

Expected result: RANSAC algorithm should not make call to functions with invalid data that cause warnings.

### Actual Results

...\Lib\site-packages\sklearn\metrics\_regression.py:1283, in r2_score(y_true, y_pred, sample_weight, multioutput, force_finite)
   1281 if _num_samples(y_pred) < 2:
   1282     msg = "R^2 score is not well-defined with less than two samples."
-> 1283     warnings.warn(msg, UndefinedMetricWarning)
   1284     return float("nan")

### Versions

```shell
import sklearn; sklearn.show_versions()

System:
    python: 3.13.5 | packaged by Anaconda, Inc. | (main, Jun 12 2025, 16:37:03) [MSC v.1929 64 bit (AMD64)]
executable: C:\ProgramData\anaconda3\envs\E134\python.exe
   machine: Windows-11-10.0.26200-SP0

Python dependencies:
      sklearn: 1.7.2
          pip: 25.2
   setuptools: 78.1.1
        numpy: 2.3.2
        scipy: 1.16.1
       Cython: None
       pandas: 2.3.2
   matplotlib: 3.10.6
       joblib: 1.5.2
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 12
         prefix: libscipy_openblas
       filepath: C:\ProgramData\anaconda3\envs\E134\Lib\site-packages\numpy.libs\libscipy_openblas64_-860d95b1c38e637ce4509f5fa24fbf2a.dll
        version: 0.3.30
threading_layer: pthreads
   architecture: SkylakeX

       user_api: blas
   internal_api: openblas
    num_threads: 12
         prefix: libscipy_openblas
       filepath: C:\ProgramData\anaconda3\envs\E134\Lib\site-packages\scipy.libs\libscipy_openblas-6b2103f2ae4d8547998b5d188e9801fb.dll
        version: 0.3.28
threading_layer: pthreads
   architecture: SkylakeX

       user_api: openmp
   internal_api: openmp
    num_threads: 12
         prefix: vcomp
       filepath: C:\ProgramData\anaconda3\envs\E134\Lib\site-packages\sklearn\.libs\vcomp140.dll
        version: None
```

COMMENT:
Here is a minimum example that will trigger the issue:

`import warnings`
`warnings.filterwarnings("error")`
`from sklearn import linear_model`
`import numpy as np`

`import matplotlib.pyplot as plt`

`def ransac(x: np.ndarray,y: np.ndarray, stop_probability: float = 0.99, min_samples: int = None, random_state: int = None):`
`    res = linear_model.RANSACRegressor(stop_probability=stop_probability, min_samples=min_samples, random_state=random_state)`
`    res.fit(x.reshape(-1, 1), y)`
`    slope = res.estimator_.coef_`
`    offset = res.estimator_.intercept_`
`    mask = res.inlier_mask_`
`    return np.squeeze(slope), offset, mask`


`x = np.loadtxt('x.txt')`
`y = np.loadtxt('y.txt')`
`plt.figure()`
`plt.scatter(x,y)`

`cnt = 3997`
`while(True):`
`    slope, offset, mask = ransac(x, y, stop_probability=0.85, min_samples=5, random_state=cnt)`
`    cnt += 1`
`    if ((cnt % 1) == 0):`
`        print(cnt)`

x.txt:

`2.354089453125000000e+04
2.354190764508928623e+04
2.353714536830357247e+04
2.353221847098214130e+04
2.349483956473214130e+04
2.343760156250000000e+04
2.339758733258928623e+04
2.334923493303571377e+04
2.339448409598214130e+04
2.337885267857142753e+04
2.335331166294642753e+04
2.338325223214285870e+04
2.338662862723214130e+04
2.339962834821428623e+04
2.337248493303571377e+04
2.337703125000000000e+04
2.335175279017857247e+04
2.333880859375000000e+04
2.333363699776785870e+04
2.334816517857142753e+04
2.334201088169642753e+04
2.332751757812500000e+04
2.329640569196428623e+04
2.331853738839285870e+04
2.331441155133928623e+04
2.330571037946428623e+04
2.336414174107142753e+04
2.352394056919642753e+04
2.357136467633928623e+04
2.345948325892857247e+04
2.346412220982142753e+04
2.352992940848214130e+04
2.353771372767857247e+04
2.354593247767857247e+04
2.358410239955357247e+04
2.359560323660714130e+04
2.360009654017857247e+04
2.364286914062500000e+04
2.366714704241071377e+04
2.364426004464285870e+04
2.370781640625000000e+04
2.371560965401785870e+04
2.377968526785714130e+04
2.443840178571428623e+04
2.434347488839285870e+04
2.433881752232142753e+04
2.433426311383928623e+04
2.427608035714285870e+04
2.430333091517857247e+04
2.429239983258928623e+04
2.423840039062500000e+04
2.423808593750000000e+04
2.422915931919642753e+04
2.416740318080357247e+04
2.414949581473214130e+04
2.415604157366071377e+04
2.413267271205357247e+04
2.413538030133928623e+04
2.411324469866071377e+04
2.411510016741071377e+04
2.408434095982142753e+04
2.408112946428571377e+04
2.406505831473214130e+04
2.407048660714285870e+04
2.401976283482142753e+04
2.403424051339285870e+04
2.402245898437500000e+04
2.402745703125000000e+04
2.401284570312500000e+04
2.400151897321428623e+04
2.397562472098214130e+04
2.396642466517857247e+04
2.396590206473214130e+04
2.395050781250000000e+04
2.394383314732142753e+04
2.393089843750000000e+04
2.392278180803571377e+04
2.393490457589285870e+04
2.395881445312500000e+04
2.395584654017857247e+04
2.394173604910714130e+04
2.394343443080357247e+04
2.394648716517857247e+04
2.397626283482142753e+04
2.397459681919642753e+04
2.397639174107142753e+04
2.399163392857142753e+04
2.399876171875000000e+04
2.400032533482142753e+04
2.405376004464285870e+04
2.412974079241071377e+04
2.417889341517857247e+04
2.417490513392857247e+04
2.417541071428571377e+04
2.421409737723214130e+04
2.422623130580357247e+04
2.423053152901785870e+04
2.428429408482142753e+04
2.423184626116071377e+04
2.421227315848214130e+04
2.420852622767857247e+04
2.412087053571428623e+04
2.396933175223214130e+04
2.397105050223214130e+04
2.393382896205357247e+04
2.392703152901785870e+04
2.391187806919642753e+04
2.392253934151785870e+04
2.390548493303571377e+04
2.393171986607142753e+04
2.389341127232142753e+04
2.390545898437500000e+04
2.389538504464285870e+04
2.389791601562500000e+04
2.389031780133928623e+04
2.390086467633928623e+04
2.389445424107142753e+04
2.383993554687500000e+04
2.385353515625000000e+04
2.386406333705357247e+04
2.385337639508928623e+04
2.303180524553571377e+04
2.303268359375000000e+04
2.302369084821428623e+04
2.305043666294642753e+04
2.304599079241071377e+04`

y.txt

`9.911940427466330306e-01
9.866958850020894900e-01
9.875961949667857454e-01
9.875055813609107114e-01
9.907457464020061888e-01
9.863727550727469096e-01
9.811931695440736512e-01
9.876478741689477259e-01
9.882716880566916329e-01
9.831566273337865036e-01
9.872962206971640509e-01
9.902945245482689396e-01
9.857471838361253624e-01
9.870131297279840510e-01
9.878516088368557435e-01
9.877325510779544349e-01
9.874504557797080251e-01
9.841143189484785614e-01
9.867977155158825786e-01
9.849624221909996091e-01
9.893468906941939034e-01
9.864039083516480400e-01
9.854392380939522855e-01
9.728602713686699266e-01
9.893614135425836675e-01
9.852133978663125680e-01
9.863605453729722417e-01
9.879487327362642679e-01
9.854795057234500000e-01
9.846331812539265149e-01
9.881291955906768010e-01
9.889651604012378305e-01
9.893814200375223855e-01
9.867874354152974536e-01
9.877912691783664600e-01
9.871398772720846315e-01
9.896569047019155851e-01
9.896225598933215961e-01
9.886767968912076876e-01
9.886195132272517228e-01
9.893355543928239459e-01
9.902379493004785926e-01
9.908719793170618351e-01
9.958305873753400261e-01
9.965277985062945820e-01
9.947820155623234850e-01
9.949523708861491844e-01
9.933702829062354134e-01
1.005197396142061272e+00
9.924987664724146397e-01
9.949701353930223702e-01
9.922635628187238543e-01
9.919742558694860657e-01
9.938089889172160296e-01
1.003345982114923984e+00
9.929625301715683250e-01
9.939562528942915698e-01
9.930592982495255461e-01
1.003205192435743909e+00
9.939306964960783475e-01
9.924345720790366254e-01
9.931700700215164712e-01
9.892363880517691666e-01
9.919527222885397011e-01
9.915491327863689142e-01
9.915031659935225061e-01
9.904421568573573698e-01
9.929443486392161011e-01
9.962363600189177992e-01
9.956236777010575301e-01
9.909991370134141331e-01
9.930267240942991513e-01
9.902007071925760240e-01
9.915107449719483546e-01
9.874719198704383638e-01
9.944841724692834450e-01
9.920693639998098279e-01
9.912254191253513369e-01
1.018409657102366861e+00
9.945644542350319250e-01
9.918270561574651367e-01
9.927845679100995024e-01
9.872030274380240167e-01
9.950902043141174325e-01
9.926399785878228244e-01
9.909140171397071706e-01
9.923285674924691158e-01
9.907790848265294326e-01
9.928608308767812307e-01
9.974898747710033708e-01
1.004097942561977375e+00
9.954476107931979900e-01
9.929765814190184559e-01
9.927339060739439613e-01
9.953326740017417462e-01
9.936247541248719450e-01
9.938620130145667808e-01
9.945206568315547724e-01
9.934128831391779713e-01
9.942985993612444284e-01
9.939502258280988878e-01
9.907932780682602303e-01
9.931426535406523426e-01
9.913017681245003265e-01
9.907210375467023766e-01
9.915538231058085294e-01
9.916650964664350809e-01
9.934774933766156169e-01
9.893342903597259763e-01
9.938277974224778433e-01
9.939421685814324725e-01
9.916855380763870409e-01
9.892394118584579843e-01
9.900057394516791343e-01
9.947926846377536103e-01
9.910273883445890331e-01
9.910755728437796730e-01
9.947038007496498135e-01
9.913071141088929394e-01
9.922702756096926091e-01
9.912313887163096693e-01
9.804850468306531841e-01
9.811454682925286885e-01
9.881656940870331418e-01
9.832404521255758700e-01
9.703085068726251539e-01`

Merry Christmas everyone!

Labels: Bug, Needs Reproducible Code, Needs Triage