Z-Score Standardization Practice Problem
This data science coding problem helps you practice Feature Scaling & Transformation, z-score standardization, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Feature Scaling & Transformation.
- Problem ID: 122
- Problem key: 122-z-score-standardization
- URL: https://datacrack.app/solve/122-z-score-standardization
- Difficulty: easy
- Topic: Feature Scaling & Transformation
- Module: Data Cleaning
Problem Statement
# Z-Score Standardization
### 🎯 Goal
Standardize numeric columns so they have zero mean and unit variance using Z-Score scaling.
### 💻 Task
Implement `z_score_standardize(data)` that:
1. Converts the input dictionary to a DataFrame
2. Applies the Z-Score formula to each numeric column: `(x - mean) / std`
3. Uses pandas default standard deviation (ddof=1, sample std)
4. Returns the standardized 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 standardized (mean ≈ 0, std ≈ 1), rounded to 2 decimals
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
def z_score_standardize(data):
"""
Standardize numeric columns using Z-Score (mean=0, std=1).
Args:
data (dict): Input data as dictionary (from JSON)
Returns:
pd.DataFrame: DataFrame with standardized values rounded to 2 decimals
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Apply (x - mean) / std to each numeric column
# TODO: Round the result to 2 decimal places and return
pass
```
---
### 💡 Examples
**Example 1:** Single column with 5 values
```python
data = {"A": [1.0, 2.0, 3.0, 4.0, 5.0]}
z_score_standardize(data)
```
```
A
0 -1.26
1 -0.63
2 0.00
3 0.63
4 1.26
```
**Example 2:** Evenly spaced values
```python
data = {"X": [10.0, 20.0, 30.0]}
z_score_standardize(data)
```
```
X
0 -1.0
1 0.0
2 1.0
```
**Example 3:** Scaled version of Example 1
```python
data = {"A": [100.0, 200.0, 300.0, 400.0, 500.0]}
z_score_standardize(data)
```
```
A
0 -1.26
1 -0.63
2 0.00
3 0.63
4 1.26
```Starter Code
import pandas as pd
import numpy as np
def z_score_standardize(data):
"""
Standardize numeric columns using Z-Score (mean=0, std=1).
Args:
data (dict): Input data as dictionary (from JSON)
Returns:
pd.DataFrame: DataFrame with standardized values rounded to 2 decimals
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Apply (x - mean) / std to each numeric column
# TODO: Round the result to 2 decimal places and return
pass