我正在运行一个多标签预测模型。作为一项性能衡量标准,我正在检查模型中最重要的
N
预测是否包含 y=1
的真实情况。
例如,如果我的模型对某个数据点的最高预测是黄色(90%)、绿色(80%)、红色(75%),而现实是绿色和红色,我将其视为“正确”预测,而诸如(精确)准确性之类的衡量标准将被视为不正确。
下面是我的实现,其中有一个大型 X 和 y 矩阵(具有许多列)的实际示例。我需要找到一个运行速度更快的实现(或完全不同的解决方案)。
可重现的示例(运行速度太慢,约 2 分钟)如下:
from scipy.sparse import random
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import time
np.random.seed(14)
## Generate sparse X, and y
X = random(100_000, 1000, density=0.01, format='csr')
y = pd.DataFrame(np.random.choice([0, 1], size=(100_000, 10)))
# Define no change as 0 in all rows
y['no_change'] = np.where(y.sum(axis=1) == 0, 1, 0)
dt = DecisionTreeClassifier(max_depth=15)
dt.fit(X, y)
# Print precise accuracy -- truth must precisely match prediction
print(f"Accuracy score (precise): {accuracy_score(y_true=y, y_pred=dt.predict(X=X)):.1%}")
# Get top n predictions based on probability (in case of equality keep all)
def top_n_preds(row, n_top):
topcols = row[row > 0].nlargest(n=n_top, keep='all')
top_colnames = topcols.index.tolist()
return top_colnames
start = time.time()
# Retrieve probabilities of predictions
pred_probs = np.asarray(dt.predict_proba(X=X))
pred_probs = pd.DataFrame(pred_probs[:, :, 1].T, columns=y.columns)
# Find top 5 predictions
pred_probs['top_preds'] = pred_probs.apply(top_n_preds, axis=1, n_top=5)
# List all real changes in y
pred_probs['real_changes'] = y.apply(lambda row: row[row == 1].index.tolist(), axis=1)
# Check if real changes are contained in top 5 predictions
pred_probs['preds_cover_reality'] = pred_probs.apply(lambda row: set(row['real_changes']).issubset(set(row['top_preds'])), axis=1)
print(f"Accuracy present in top n_top predictions: {pred_probs['preds_cover_reality'].sum() / y.shape[0]:.1%}")
print(f"Time elapsed: {(time.time()-start)/60:.1f} minutes")
apply()
没那么快,因为它会遍历 DataFrame
的行。尝试将 apply()
替换为 numpy
操作:
...
# Generate sparse X, and y using NumPy and appending 'no_change' column
y = np.random.choice([0, 1], size=(100_000, 10))
y_no_change = np.where(y.sum(axis=1) == 0, 1, 0)
y = np.c_[y, y_no_change]
...
# Retrieving probabilities using NumPy
pred_probs_list = dt.predict_proba(X)
pred_probs = np.array([probs[:, 1] for probs in pred_probs_list]).T
# Find top 5 predictions using NumPy's argpartition
top_5_idx = np.argpartition(-pred_probs, 5, axis=1)[:, :5]
# Prepare the ground truth using NumPy's argwhere
true_labels_idx = np.argwhere(y == 1)
# Check if real changes are contained in top 5 predictions using NumPy
counts = 0
for i in range(y.shape[0]):
truth_set = set(true_labels_idx[true_labels_idx[:, 0] == i][:, 1])
if truth_set.issubset(top_5_idx[i]):
counts += 1
...
print(f"Accuracy present in top n_top predictions: {counts / y.shape[0]:.1%}")