In scikit-learn, fit(), transform(), and fit_transform() are commonly used while preprocessing data. It uses transformers for operations such as scaling, encoding, imputing, feature selection, and dimensionality reduction.
- fit() learns parameter from the given data.
- transform() uses those learned parameters to modify data.
- fit_transform() learns the parameters and transforms the same data in one operation.
fit() method
This method learns the parameters required by a transformer from the input data. It does not perform the final transformation of the input values.
Example:
from sklearn.preprocessing import StandardScaler
data = [[1, 10],
[2, 20],
[3, 30]]
scaler = StandardScaler()
scaler.fit(data)
print(scaler.mean_)
Output:
[ 2. 20.]
Explanation:
- fit() calculates the required statistics from the given data.
- StandardScaler stores the calculated mean and standard deviation.
- Original values are not transformed during this step.
- fit() returns the fitted estimator itself.
transform() Method
It applies the transformation using parameters that were learned during fit().
Example:
from sklearn.preprocessing import StandardScaler
data = [[1, 10],
[2, 20],
[3, 30]]
scaler = StandardScaler()
scaler.fit(data)
transformed_data = scaler.transform(data)
print(transformed_data)
Output:

Explanation:
- fit() first learns the statistics from data.
- transform() then uses those statistics to produce the scaled values.
fit_transform() Method
This method combines fitting and transformation into one operation. Learns the required parameters from the supplied data and immediately transforms that same data.
Example:
from sklearn.preprocessing import StandardScaler
data = [[1, 10],
[2, 20],
[3, 30]]
scaler = StandardScaler()
transformed_data = scaler.fit_transform(data)
print(transformed_data)
Output:

Explanation:
- fit_transform(), learns from the data and applies standard scaling Normalization in the data itself.
- Same input data is used for learning and transformation.
Key Comparison
| Method | Learns Parameters | Transforms Data | Returns |
|---|---|---|---|
| fit() | Yes | No | Fitted estimator |
| transform() | No | Yes | Transformed data |
| fit_transform() | Yes | Yes | Transformed data |