![]() | 1 | initial version |
Using numpy
arrays, I wrote a function for you (I used Google Gemini for TSS and RSS):
def coeff_of_det(R,coeffs):
import numpy as np
# Extract x and y values
x_values = np.array([point[0] for point in R])
y_true = np.array([point[1] for point in R])
# Predict y values
y_pred = [model.subs(coeffs)(x_val) for x_val in x_values]
# R**2
y_mean = n(sum((y_true)) / len(y_true))
TSS = sum((y_true - y_mean)**2)
RSS = sum((y_true - y_pred)**2)
r_squared = 1 - (RSS / TSS)
return r_squared
R = [[1, 2], [3.45, 4], [6, 5], [4, 3]]
var('a, b')
model(x) = a * x + b
coeffs = find_fit(R, model)
r_sq=coeff_of_det(R,coeffs)
print(r_sq)