pandas 如何在DataFrame上循环和应用lambda函数?

c7rzv4ha  于 2023-02-02  发布在  其他
关注(0)|答案(2)|浏览(148)

我正在建立一个ML模型,我想运行几次预测位,然后计算准确性得分的平均值。
我的代码如下所示:

predictions = test_df[['histor', 'philosoph', 'cook', 'roman', 'bibl']].apply(lambda x: baseline.predict(*x), axis=1)

y_true = test_df["label"].values

print("Accuracy: ", accuracy_score(y_true, predictions))

是否有办法循环预测?预期结果将是:假设n = 10。预测运行10次,我会打印出每次运行的所有准确度,最后还会打印出所有准确度的平均值。
希望这是有道理的。

vpfxa7rd

vpfxa7rd1#

我会使用sklearns的cross_瓦尔_score来实现这个目标:

from sklearn.model_selection import cross_val_score
X = test_df[['histor', 'philosoph', 'cook', 'roman', 'bibl']]
y = test_df["label"].values
cross_val_score(baseline, X, y, cv=10)
63lcw9qa

63lcw9qa2#

您可以将准确度得分存储在列表中,然后使用该列表计算最后的平均准确度

import numpy as np
 n = 10
accuracies = np.zeros(n)
for i in range(n):
    predictions = test_df[['histor', 'philosoph', 'cook', 'roman', 'bibl']].apply(lambda x: baseline.predict(*x), axis=1)
    accuracy = accuracy_score(y_true, predictions)
    accuracies[i] = accuracy
    print("Run ", i+1, " Accuracy: ", accuracy)

mean_accuracy = np.mean(accuracies)
print("Mean Accuracy: ", mean_accuracy)

n = 10
accuracies = []
for i in range(n):
    predictions = test_df[['histor', 'philosoph', 'cook', 'roman', 'bibl']].apply(lambda x: baseline.predict(*x), axis=1)
    accuracy = accuracy_score(y_true, predictions)
    accuracies.append(accuracy)
    print("Run ", i+1, " Accuracy: ", accuracy)

mean_accuracy = sum(accuracies) / n
print("Mean Accuracy: ", mean_accuracy)

相关问题