To solve the problem of analyzing hidden quality control (QC) data for anomalies, we need to identify specific patterns in the dataset. The goal is to detect products that fall outside acceptable weight specifications, sudden weight changes between consecutive products, and products with placeholder weights. Below is a step-by-step solution:
Approach
- Sort Data by Timestamp: Ensure chronological processing to detect consecutive product changes accurately.
- Identify Out-of-Spec Products: Check each product's weight to see if it is below 100g or above 200g.
- Detect Sudden Weight Changes: Compare each product's weight with the previous one; if the absolute difference exceeds 50g, flag it.
- Find Placeholder Weights: Identify products with exactly 150g weights, which may indicate placeholder values.
- Compile Results: Collect all flagged products into separate lists for each anomaly type.
Solution Code
def analyze_qc_data(data):
# Sort data by timestamp
sorted_data = sorted(data, key=lambda x: x[2])
out_of_spec = []
sudden_changes = []
placeholder_weights = []
for i, (product_id, weight, timestamp) in enumerate(sorted_data):
# Check for out-of-spec weights
if weight < 100 or weight > 200:
out_of_spec.append((product_id, weight, timestamp))
# Check for placeholder weights
if weight == 150.0:
placeholder_weights.append((product_id, weight, timestamp))
# Check for sudden changes (skip the first product)
if i > 0:
prev_weight = sorted_data[i-1][1]
weight_diff = abs(weight - prev_weight)
if weight_diff > 50:
prev_product_id = sorted_data[i-1][0]
sudden_changes.append((prev_product_id, product_id, weight_diff, timestamp))
return out_of_spec, sudden_changes, placeholder_weights
data = [
("P001", 120.5, "2023-01-01 10:00:00"),
("P002", 95.0, "2023-01-01 10:01:00"),
("P003", 150.0, "2023-01-01 10:02:00"),
("P004", 180.0, "2023-01-01 10:03:00"),
("P005", 210.0, "2023-01-01 10:04:00"),
("P006", 50.0, "2023-01-01 10:05:00"),
("P007", 160.0, "2023-01-01 10:06:00"),
("P008", 150.0, "2023-01-01 10:07:00"),
("P009", 100.0, "2023-01-01 10:08:00"),
("P010", 200.0, "2023-01-01 10:09:00"),
("P011", 250.0, "2023-01-01 10:10:00"),
("P012", 120.0, "2023-01-01 10:11:00"),
("P013", 170.0, "2023-01-01 10:12:00"),
("P014", 150.0, "2023-01-01 10:13:00"),
("P015", 100.0, "2023-01-01 10:14:00"),
("P016", 200.0, "2023-01-01 10:15:00"),
("P017", 180.0, "2023-01-01 10:16:00"),
("P018", 90.0, "2023-01-01 10:17:00"),
("P019", 160.0, "2023-01-01 10:18:00"),
("P020", 150.0, "2023-01-01 10:19:00")
]
out_of_spec, sudden_changes, placeholder_weights = analyze_qc_data(data)
print("Out-of-Spec Products:")
for item in out_of_spec:
print(f"Product ID: {item[0]}, Weight: {item[1]}g, Timestamp: {item[2]}")
print("\nSudden Weight Changes:")
for item in sudden_changes:
print(f"Products: {item[0]} -> {item[1]}, Weight Difference: {item[2]}g, Timestamp: {item[3]}")
print("\nPlaceholder Weights (150g):")
for item in placeholder_weights:
print(f"Product ID: {item[0]}, Weight: {item[1]}g, Timestamp: {item[2]}")
Explanation
- Sorting Data: The data is sorted by timestamp to ensure chronological processing, which is crucial for detecting consecutive product changes.
- Out-of-Spec Detection: Each product's weight is checked against the acceptable range (100g to 200g). Products outside this range are added to the
out_of_speclist. - Sudden Change Detection: For each product (except the first), the weight difference with the previous product is calculated. If this difference exceeds 50g, it is recorded in the
sudden_changeslist along with the product IDs and timestamp. - Placeholder Detection: Products with exactly 150g weights are flagged as potential placeholders and added to the
placeholder_weightslist. - Output: The results are printed in a structured format, showing each anomaly type with relevant details.
This approach efficiently processes the QC data to identify critical anomalies, ensuring quality control standards are met.
Request an On-site Audit / Inquiry