# XAI Experiment 8: LIME (Weighted Least Squares surrogate)  [lab notebook version]
# Requires: exam: none (math) | lab: numpy
# ===========================================================================

import numpy as np
import math

x = np.array([1, 1])

Z = np.array([
    [1, 1],
    [1, 0],
    [0, 1],
    [0, 0]
])

y = np.array([0.90, 0.70, 0.60, 0.20])
sigma = 1

def dist(x, z):
    d = 0
    for i in range(len(x)):
        d += (x[i]-z[i])**2

    return d**0.5


distances = []
for i in range(len(Z)):
    distances.append(dist(x, Z[i]))

distances

weights = []
for d in distances:
    weights.append(np.exp(-d**2 / sigma**2))

weights

X = np.column_stack((np.ones(len(Z)), Z))
W = np.diag(weights)
Y = y.reshape(-1, 1)

X

W

Y

A = X.T@W@X
B = X.T@W@Y

beta = np.linalg.solve(A, B)

beta
