为什么不将我的单列DataFrame缩减为Series?

时间:2019-02-22 20:22:26

标签: python-3.x pandas

我遇到错误

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我不确定为什么。 samples是(3681,58)DataFrame。 predictions最终也是(3681,1)DataFrame,而不是Series。

def sigmoid(weights, sample):
    return 1 / (1 + (np.exp(-sample.dot(weights))))


def predict_sigmoid(weights, sample, th):
    return 0 if sigmoid(weights, sample) < th else 1


def calculate_acc(predictions, targets):
    """Calc. the ACC between the predictions and the targets for the given DataFrame using the given weight vector."""
    return 1 - sum([0 if (predictions.iloc[i] == targets.iloc[i]) else 1 for i in range(len(targets))]) / len(targets)


def calculate_acc_regression(samples, targets, weights, f=predict_sigmoid, th=0.5):
    """Calc. the ACC between the predictions and the targets for the given DataFrame using the given weight vector."""
    predictions = samples.apply(lambda x: f(weights, x, th), axis=1, result_type='reduce')
    return calculate_acc(predictions, targets)

1 个答案:

答案 0 :(得分:0)

由于预测是一个DataFrame:

Comparator

是系列而不是布尔值。

也许您想使用等于?

predictions.iloc[i] == targets.iloc[i]