В тестирующей системе данный код валится на времени выполнения. Каким образом можно его улучшить
from numpy import array
def linear_func(theta, x):
return sum(theta*x)
def linear_func_all(theta, X):
size = X.shape[0]
return array([sum(theta*X[i]) for i in range(size)])
def mean_squared_error(theta, X, y):
size = X.shape[0]
return sum((y - linear_func_all(theta, X))**2) / size
def grad_mean_squared_error(theta, X, y):
f = linear_func_all(theta, X)
size = X.shape[0]
n = [sum((f - y) * row) * 2 / size for row in X.T]
return array(n)
X = array([[1,2], [3,4], [4,5]])
theta = array([5, 6])
x = array([1, 2])
y = array([1, 2, 1])
print(linear_func(theta, x))
print(linear_func_all(theta, X))
print(mean_squared_error(theta, X, y))
print(grad_mean_squared_error(theta, X, y))