Min-Max Normalization Practice Problem
This data science coding problem helps you practice Feature Scaling & Transformation, min-max normalization, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Feature Scaling & Transformation.
- Problem ID: 121
- Problem key: 121-min-max-normalization
- URL: https://datacrack.app/solve/121-min-max-normalization
- Difficulty: easy
- Topic: Feature Scaling & Transformation
- Module: Data Cleaning
Problem Statement
# Min-Max Normalization
### 🎯 Goal
Normalize numeric columns to a [0, 1] range using Min-Max scaling.
### 💻 Task
Implement `min_max_normalize(data)` that:
1. Converts the input dictionary to a DataFrame
2. Applies the Min-Max formula to each numeric column: `(x - min) / (max - min)`
3. Returns the normalized DataFrame rounded to 2 decimal places
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists of numeric data
### 📤 Output
- A pandas DataFrame with all numeric columns scaled to [0, 1], rounded to 2 decimals
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
def min_max_normalize(data):
"""
Normalize numeric columns to [0, 1] using Min-Max scaling.
Args:
data (dict): Input data as dictionary (from JSON)
Returns:
pd.DataFrame: DataFrame with normalized values rounded to 2 decimals
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Apply (x - min) / (max - min) to each numeric column
# TODO: Round the result to 2 decimal places and return
pass
```
---
### 💡 Examples
**Example 1:** Single column
```python
data = {"A": [1.0, 2.0, 3.0, 4.0, 5.0]}
min_max_normalize(data)
```
```
A
0 0.00
1 0.25
2 0.50
3 0.75
4 1.00
```
**Example 2:** Multiple columns
```python
data = {"X": [10.0, 20.0, 30.0], "Y": [100.0, 200.0, 300.0]}
min_max_normalize(data)
```
```
X Y
0 0.0 0.0
1 0.5 0.5
2 1.0 1.0
```
**Example 3:** Including negative values
```python
data = {"A": [0.0, 50.0, 100.0], "B": [-10.0, 0.0, 10.0]}
min_max_normalize(data)
```
```
A B
0 0.0 0.0
1 0.5 0.5
2 1.0 1.0
```Starter Code
import pandas as pd
import numpy as np
def min_max_normalize(data):
"""
Normalize numeric columns to [0, 1] using Min-Max scaling.
Args:
data (dict): Input data as dictionary (from JSON)
Returns:
pd.DataFrame: DataFrame with normalized values rounded to 2 decimals
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Apply (x - min) / (max - min) to each numeric column
# TODO: Round the result to 2 decimal places and return
pass