Scaling features of a dataset is a common preprocessing step in machine learning.
maxabs_scale()
scales each feature by its maximum absolute value, which is particularly useful for datasets with varying scales and sparse data.
from sklearn.preprocessing import maxabs_scale
import numpy as np
# create a synthetic dataset
data = np.array([[1, -1, 2],
[2, 0, 0],
[0, 1, -1]])
# apply maxabs_scale
scaled_data = maxabs_scale(data)
# display before and after scaling
print("Original data:")
print(data)
print("\nScaled data:")
print(scaled_data)
Running the example gives an output like:
Original data:
[[ 1 -1 2]
[ 2 0 0]
[ 0 1 -1]]
Scaled data:
[[ 0.5 -1. 1. ]
[ 1. 0. 0. ]
[ 0. 1. -0.5]]
The steps are as follows:
- A synthetic dataset is created using
numpy.array()
with mixed positive and negative values. - The
maxabs_scale()
function is applied to the dataset to scale each feature by its maximum absolute value. - The original dataset is printed to show its values before scaling.
- The scaled dataset is printed to show the transformed values after applying
maxabs_scale()
.
This example shows how to preprocess data using maxabs_scale()
in scikit-learn, which is particularly useful for datasets with features of varying scales and sparse datasets.