Repository navigation
Description
Warning
This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.
Introduce yourself
No response
Describe the bug and give evidence about its user-facing impact
There do not appear to be rules about how things work when the dtypes (e.g. float32/float64) passed to estimators with methods like partial_fit or argument warm_start are supposed to work, and different estimators show different behavior.
For instance:
SGDRegressorwill error out if the dtypes of attributes do not match with the dtype of the new 'X':
import numpy as np
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(20,3))
y = rng.standard_normal(size=X.shape[0])
from sklearn.linear_model import SGDRegressor
model = SGDRegressor().fit(X[:10].astype(np.float32), y[:10].astype(np.float32))
model.partial_fit(X[10:], y[10:])TypeError: Argument 'dataset' has incorrect type (expected sklearn.utils._seq_dataset.SequentialDataset32, got sklearn.utils._seq_dataset.ArrayDataset64)
-
LogisticRegression(warm_start=True)will keep the dtype of the last 'X' passed to.fit()(I think this one is an unambiguously correct behavior regardless of the rest). -
IncrementalPCAwill upcast the dtype whenever it sees a float64:
import numpy as np
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(20,3))
from sklearn.decomposition import IncrementalPCA
model1 = IncrementalPCA().fit(X[:10].astype(np.float32))
model1.partial_fit(X[10:])
print(model1.singular_values_.dtype)
model2 = IncrementalPCA().fit(X[:10])
model2.partial_fit(X[10:].astype(np.float32))
print(model2.singular_values_.dtype)float64
float64
- Most estimators will upcast dtypes when the 'X' passed to methods like
.predict()or.transform()differs from the attributes, butMaxAbsScalerfollows the dtype of the input instead:
import numpy as np
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(20,3))
from sklearn.preprocessing import MaxAbsScaler
scaler = MaxAbsScaler().fit(X)
print(scaler.scale_.dtype)
print(scaler.transform(X[:5].astype(np.float32)).dtype)float64
float32
-
In the same line, LogisticRegression follows this upcast-on-predict behavior with NumPy namespaces, but under array API, it errors out if the dtypes do not match, and will follow a different logic after Store all fitted attributes as Numpy arrays #35053 where the dtype of the prediction will follow 'X' under array API but will still upcast under NumPy.
-
IncrementalPCAwill use float64 for some methods regardless of the dtype the attributes have:
import numpy as np
rng = np.random.default_rng(seed=123)
X = rng.standard_normal(size=(20,3))
from sklearn.decomposition import IncrementalPCA
model = IncrementalPCA().fit(X[:10].astype(np.float32))
print(model.singular_values_.dtype)
print(model.get_covariance().dtype)float32
float64
Steps/Code to Reproduce
Examples given above.
Expected Results
Code examples given above should be consistent across estimators.
Actual Results
There's differences across estimators.
Versions
Not relevant.Interest in fixing the bug
No.
Activity
Kaynup commentedon Nov 27, 2026
Hello @david-cortes-intel
I ran an empirical audit with antigravity across all 24 non-meta estimators with
partial_fit, the 5 meta-estimators withpartial_fit, sparse CSR paths, and all 33 estimators withwarm_start=Trueinmainto map out their behaviours.i.
partial_fitacross all 24 non-meta estimators (float32tofloat64)SGDClassifier,SGDRegressor,SGDOneClassSVM,PassiveAggressiveClassifier,PassiveAggressiveRegressor,Perceptron,MiniBatchKMeansTypeError(SequentialDataset32vsArrayDataset64).MiniBatchKMeansraisesValueError: Buffer dtype mismatchin_mini_batch_step(both dense & sparse CSR).Birch,LatentDirichletAllocation,MiniBatchDictionaryLearning,MiniBatchNMF,MLPClassifier,MLPRegressorsubcluster_centers_,components_,coefs_) stayfloat32onf32 -> f64andfloat64onf64 -> f32. (InMLPRegressor,best_loss_staysfloat32whileloss_switches tofloat64.)float64is seenIncrementalPCA,MinMaxScaler,MaxAbsScalerfloat32onf32only, promote tofloat64onf32 -> f64, and remainfloat64onf64 -> f32.GaussianNBtheta_,var_,class_count_,class_prior_keep the 1st call's dtype, butepsilon_flips to match the latest batch (self.var_smoothing * xp.max(xp.var(X, axis=0))).float64internallyStandardScaler,BernoulliNB,MultinomialNB,ComplementNB,CategoricalNB,BernoulliRBMfloat64even when fitted solely onfloat32(under NumPy).(Meta-estimators
OneVsRestClassifier,OneVsOneClassifier,MultiOutputClassifier,MultiOutputRegressor, andSelectFromModelinherit whatever their wrapped estimator does, e.g. all crash when wrappingSGD*onf32 -> f64, whileOneVsRestClassifier(GaussianNB)succeeds.HashingVectorizeris stateless with no float attributes.)ii.$\to$
warm_start=Trueacross all 33 estimators (fit(f32)fit(f64)and vice versa)Even within
warm_start=True(which you notedLogisticRegressionhandles cleanly), estimators diverge sharply:fit()dtype cleanly:LogisticRegression,Lasso,ElasticNet,TweedieRegressor,PoissonRegressor,GammaRegressor, andSGDClassifier/PassiveAggressiveClassifier/Perceptron/SGDOneClassSVM(though the latter four keepintercept_/offset_infloat64).SGDRegressor(warm_start=True)&PassiveAggressiveRegressor(warm_start=True): raiseTypeError(SequentialDataset32vsArrayDataset64) onf32 -> f64, unlikeSGDClassifier(warm_start=True)which succeeds!MultiTaskLasso(warm_start=True)&MultiTaskElasticNet(warm_start=True): raiseValueError: Buffer dtype mismatchon bothf32 -> f64andf64 -> f32, unlike single-taskLasso/ElasticNet!fit()dtype:MLPClassifier,MLPRegressor(coefs_/intercepts_stay in 1st dtype).fit(f64)fit(f32):GaussianMixture(warm_start=True):weights_,means_,lower_bound_stayfloat64, whilecovariances_,precisions_cholesky_,precisions_flip tofloat32.BayesianGaussianMixture(warm_start=True):mean_prior_flips tofloat32whilemeans_,covariances_, etc. stayfloat64.ii. Reproducers for a few Edge Cases
MiniBatchKMeanscrashes both onpartial_fitAND onpredictwith mixed dtypes (whiletransformsilently upcasts):GaussianNBsplitstheta_/var_andepsilon_across dtypes;GaussianMixture(warm_start=True)splitsmeans_andcovariances_:SGD*/PassiveAggressive*/Perceptronallocatecoef_infloat32butintercept_infloat64, so evenfit(f32).predict(f32)returnsfloat64(andSGDRegressor(warm_start=True)crashes whileSGDClassifier(warm_start=True)does not):Mixed
fitvstransform/predictbehavior splits across estimators:fit(f64).transform(f32)float32:StandardScaler,MinMaxScaler,MaxAbsScaler,MiniBatchDictionaryLearning,MiniBatchNMF.fit(f64).transform/predict_proba(f32)float64:IncrementalPCA,Birch,MiniBatchKMeans.transform,GaussianNB,MLPClassifier,MLPRegressor,BernoulliRBM,*NB.fit(f32).transform(f64)float32(follows attribute dtype, notX):LatentDirichletAllocation.I hope this helps.
I have missed some auditing locally for these two however:
I have attached my audit script for reference:
audit_partial_fit_and_warm_start_dtypes.py
audit_output.log