(lhs, rhs, rcond=None, skipna=False)
| 263 | |
| 264 | |
| 265 | def least_squares(lhs, rhs, rcond=None, skipna=False): |
| 266 | if rhs.ndim > 2: |
| 267 | out_shape = rhs.shape |
| 268 | rhs = rhs.reshape(rhs.shape[0], -1) |
| 269 | else: |
| 270 | out_shape = None |
| 271 | |
| 272 | if skipna: |
| 273 | added_dim = rhs.ndim == 1 |
| 274 | if added_dim: |
| 275 | rhs = rhs.reshape(rhs.shape[0], 1) |
| 276 | nan_cols = np.any(np.isnan(rhs), axis=0) |
| 277 | out = np.empty((lhs.shape[1] + 1, rhs.shape[1])) |
| 278 | if np.any(nan_cols): |
| 279 | out[:, nan_cols] = np.apply_along_axis( |
| 280 | _nanpolyfit_1d, 0, rhs[:, nan_cols], lhs |
| 281 | ) |
| 282 | if np.any(~nan_cols): |
| 283 | out[:-1, ~nan_cols], resids, rank, _ = np.linalg.lstsq( |
| 284 | lhs, rhs[:, ~nan_cols], rcond=rcond |
| 285 | ) |
| 286 | out[-1, ~nan_cols] = resids if resids.size > 0 else np.nan |
| 287 | warn_on_deficient_rank(rank, lhs.shape[1]) |
| 288 | coeffs = out[:-1, :] |
| 289 | residuals = out[-1, :] |
| 290 | if added_dim: |
| 291 | coeffs = coeffs.reshape(coeffs.shape[0]) |
| 292 | residuals = residuals.reshape(residuals.shape[0]) |
| 293 | else: |
| 294 | coeffs, residuals, rank, _ = np.linalg.lstsq(lhs, rhs, rcond=rcond) |
| 295 | if residuals.size == 0: |
| 296 | residuals = coeffs[0] * np.nan |
| 297 | warn_on_deficient_rank(rank, lhs.shape[1]) |
| 298 | |
| 299 | if out_shape is not None: |
| 300 | coeffs = coeffs.reshape(-1, *out_shape[1:]) |
| 301 | residuals = residuals.reshape(*out_shape[1:]) |
| 302 | return coeffs, residuals |
| 303 | |
| 304 | |
| 305 | nanmin = _create_method("nanmin") |
nothing calls this directly
no test coverage detected
searching dependent graphs…