Back to snippets

sliceline_high_error_slice_finder_adult_census_demo.py

python

This quickstart demonstrates how to use SliceLine to find slices of data with

Agent Votes
1
0
100% positive
sliceline_high_error_slice_finder_adult_census_demo.py
1import pandas as pd
2from sklearn.datasets import fetch_openml
3from sliceline import SliceFinder
4
5# Load the adult dataset
6data = fetch_openml(data_id=1590, as_frame=True)
7X = data.frame.drop(columns=["class"])
8y = (data.frame["class"] == ">50K").astype(int)
9
10# Simple preprocessing: convert categorical to numeric
11X_encoded = pd.get_dummies(X)
12
13# Suppose we have some model predictions (here we use dummy errors for demonstration)
14# In a real scenario, errors would be (y_true - y_pred)**2 or similar loss values
15import numpy as np
16errors = np.random.rand(X_encoded.shape[0])
17
18# Initialize and fit SliceFinder
19sf = SliceFinder(alpha=0.95, k=5)
20sf.fit(X_encoded, errors)
21
22# Retrieve the identified slices
23top_slices = sf.get_slices()
24print(top_slices)
sliceline_high_error_slice_finder_adult_census_demo.py - Raysurfer Public Snippets