Python TypeError:ufunc的循环不支持没有可调用日志方法的ArrayBox类型的参数0
我正在使用python的auto grad库计算逻辑回归的成本函数梯度 以下是LR的拟合方法代码:Python TypeError:ufunc的循环不支持没有可调用日志方法的ArrayBox类型的参数0,python,gradient,logistic-regression,autograd,Python,Gradient,Logistic Regression,Autograd,我正在使用python的auto grad库计算逻辑回归的成本函数梯度 以下是LR的拟合方法代码: import numpy as np import pandas as pd class LogisticRegression: def __init__(self) -> None: self.num_of_iterations = None self.thetas = None self.tolerance = None
import numpy as np
import pandas as pd
class LogisticRegression:
def __init__(self) -> None:
self.num_of_iterations = None
self.thetas = None
self.tolerance = None
self.thetas_history = []
self.cost_func_history = []
def fit_autograd(self, X, y, tol, n_iter=100, lr=0.01, fit_intercept=True):
from autograd import grad
from autograd import elementwise_grad as egrad
import autograd.numpy as npa
from math import e
def training_loss(weights):
# Training loss is the negative log-likelihood of the training labels.
preds = (1/(1 + e**( np.dot(X_il, weights) )) - 0.00001)
label_probabilities = preds * y_il + (1 - preds) * (1 - y_il)
return -np.sum(np.log(label_probabilities))
# handling fit intercept param
if (fit_intercept == True):
self.num_of_thetas = len(list(X.columns))+1
thetas = pd.Series(np.random.randn(self.num_of_thetas))
bias = pd.DataFrame(pd.Series([1.0 for i in range(len(X))]))
X = pd.concat([bias,X],axis=1)
else:
self.num_of_thetas = len(list(X.columns))
self.thetas = pd.Series(np.random.randn(self.num_of_thetas))
self.num_of_samples = len(X)
self.num_of_iterations = n_iter
self.learning_rate = lr
self.tolerance = tol
self.fit_intercept = fit_intercept
mygrad = grad(training_loss)
X_il,y_il = X.to_numpy(),y.to_numpy()
theta_il = thetas.to_numpy()
for it in range(self.num_of_iterations):
temp_grad = mygrad(theta_il)
print(temp_grad)
print("thetas:",thetas)
thetas -= (lr/self.num_of_samples) * temp_grad
self.thetas_history.append(thetas)
self.cost_func_history.append(temp_grad)
return thetas
它抛出以下错误
TypeError: loop of ufunc does not support argument 0 of type ArrayBox which has no callable log method
我已经检查了以下关于np.exp()使用的问题,我能够解决这些问题。我不知道如何为np.log()执行此操作
谢谢