# XAI Experiment 5: Individual Conditional Expectation (ICE) Plot  [lab notebook version]
# Requires: exam: none | lab: matplotlib
# ===========================================================================

data = [
    [1, 2],
    [2, 1],
    [3, 3],
    [2, 2]
]

def model(row):
    return 2*row[0] + row[1]

def ice(data, feature):
    ans = {}
    values = []
    n = len(data)

    for i in range(n):
        values.append(data[i][feature])

    for val in values:
        d1 = [row.copy() for row in data]

        for i in range(n):
            d1[i][feature] = val
        t = []

        for i in range(n):
            t.append(model(d1[i]))

        ans[val] = t

    return ans

col1 = ice(data, 0)
col2 = ice(data, 1)

import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.xlabel("Feature Values")
plt.ylabel("Model Predictions")
plt.title("ICE Plot - Column 1")
for key, value in col1.items():
    plt.plot(value, label=f"Value {key}")
    plt.legend()

import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.xlabel("Feature Values")
plt.ylabel("Model Predictions")
plt.title("ICE Plot - Column 2")
for key, value in col2.items():
    plt.plot(value, label=f"Value {key}")
    plt.legend()
