Real-World Rescaling Cases
1. Financial Data Analysis
Stock Price Normalization
import numpy as np
import pandas as pd
def normalize_stock_prices(prices):
return (prices - prices.min()) / (prices.max() - prices.min())
stock_prices = np.array([50, 55, 60, 52, 58])
normalized_prices = normalize_stock_prices(stock_prices)
2. Machine Learning Feature Preparation
Preparing Features for Neural Networks
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
def prepare_ml_features(X):
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)
return X_scaled, scaler
## Example dataset preparation
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
X_train_scaled, scaler = prepare_ml_features(X_train)
X_test_scaled = scaler.transform(X_test)
3. Image Processing
Color Channel Normalization
import numpy as np
def normalize_image_channels(image):
return (image - image.min()) / (image.max() - image.min())
## RGB image normalization
rgb_image = np.random.randint(0, 256, (100, 100, 3))
normalized_image = np.apply_along_axis(normalize_image_channels, 2, rgb_image)
4. Sensor Data Processing
IoT Sensor Reading Calibration
def calibrate_sensor_readings(readings, min_val, max_val):
return [(reading - min_val) / (max_val - min_val) * 100
for reading in readings]
temperature_readings = [18.5, 20.3, 22.1, 19.7]
calibrated_readings = calibrate_sensor_readings(
temperature_readings,
min(temperature_readings),
max(temperature_readings)
)
Scaling Methods Comparison
Use Case |
Scaling Method |
Key Benefit |
Neural Networks |
MinMax Scaling |
Bounded Input |
Linear Regression |
Standard Scaling |
Zero Mean |
Anomaly Detection |
Robust Scaling |
Outlier Resistance |
5. Time Series Normalization
Preparing Time Series for Forecasting
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
def prepare_time_series(series):
scaler = MinMaxScaler()
scaled_series = scaler.fit_transform(series.values.reshape(-1, 1))
return scaled_series, scaler
## Example time series scaling
time_series_data = pd.Series([100, 120, 110, 130, 125])
scaled_series, scaler = prepare_time_series(time_series_data)
Scaling Workflow Visualization
graph TD
A[Raw Data] --> B[Identify Scaling Needs]
B --> C{Select Scaling Method}
C -->|Neural Network| D[MinMax Scaling]
C -->|Statistical Analysis| E[Standard Scaling]
C -->|Outlier-rich Data| F[Robust Scaling]
D --> G[Scaled Data Ready for Processing]
E --> G
F --> G
Best Practices for Real-World Scaling
- Always understand your data's characteristics
- Choose scaling method based on specific use case
- Maintain consistent scaling across training and testing datasets
- Preserve original data relationships
- Handle potential edge cases and outliers
By mastering these real-world rescaling techniques with LabEx, you'll be equipped to handle diverse data preprocessing challenges across multiple domains.