在 pandas 的累积周级别应用函数
Applying a function at a cumulative week level in pandas
下面是我的数据框 (df),其中我每周都有预期和实际水平的实例
我需要应用下面的 Python 代码函数来计算累计每周级别的 AUC。
from sklearn.metrics import roc_auc_score
def auc_group(df):
y_hat = df.expected
y = df.actual
return (roc_auc_score(y,y_hat))
对于每周,应该对直到该周的所有记录进行评分,因此结果应该如下所示。
虽然每周应用该函数很简单,但考虑到该周之前的所有记录,将其应用于累积分数对我来说是一个挑战。解决此问题的任何帮助表示赞赏。
如果需要累计计数和累计roc_auc_score
使用:
from sklearn.metrics import roc_auc_score
s1 = df['week'].value_counts().sort_index().cumsum()
expected, actual = [],[]
def f(x):
expected.extend(x['expected'].tolist())
actual.extend(x['actual'].tolist())
return roc_auc_score(actual, expected)
s2 = df.groupby('week').apply(f)
df = (pd.concat([s1, s2], axis=1, keys=('count of records','AUC'))
.rename_axis('week')
.reset_index())
print (df)
week count of records AUC
0 10 2 1.000000
1 11 6 0.500000
2 12 10 0.583333
如果需要累积计数并且每组 roc_auc_score(非累积)使用:
from sklearn.metrics import roc_auc_score
s1 = df['week'].value_counts().sort_index().cumsum()
s2 = df.groupby('week').apply(lambda x: roc_auc_score(x.actual,x.expected))
df = (pd.concat([s1, s2], axis=1, keys=('count of records','AUC'))
.rename_axis('week')
.reset_index())
print (df)
week count of records AUC
0 10 2 1.000000
1 11 6 0.166667
2 12 10 0.750000
import pandas as pd
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
data = pd.concat([pd.DataFrame(iris.target, columns = ['target']),
pd.DataFrame(iris.data, columns = iris.feature_names)], axis = 1)
def target_replace(x):
for i in [0,1,2]:
if x == i:
return(iris.target_names[i])
data.target = data.target.apply(target_replace)
下面是我的数据框 (df),其中我每周都有预期和实际水平的实例
我需要应用下面的 Python 代码函数来计算累计每周级别的 AUC。
from sklearn.metrics import roc_auc_score
def auc_group(df):
y_hat = df.expected
y = df.actual
return (roc_auc_score(y,y_hat))
对于每周,应该对直到该周的所有记录进行评分,因此结果应该如下所示。
虽然每周应用该函数很简单,但考虑到该周之前的所有记录,将其应用于累积分数对我来说是一个挑战。解决此问题的任何帮助表示赞赏。
如果需要累计计数和累计roc_auc_score
使用:
from sklearn.metrics import roc_auc_score
s1 = df['week'].value_counts().sort_index().cumsum()
expected, actual = [],[]
def f(x):
expected.extend(x['expected'].tolist())
actual.extend(x['actual'].tolist())
return roc_auc_score(actual, expected)
s2 = df.groupby('week').apply(f)
df = (pd.concat([s1, s2], axis=1, keys=('count of records','AUC'))
.rename_axis('week')
.reset_index())
print (df)
week count of records AUC
0 10 2 1.000000
1 11 6 0.500000
2 12 10 0.583333
如果需要累积计数并且每组 roc_auc_score(非累积)使用:
from sklearn.metrics import roc_auc_score
s1 = df['week'].value_counts().sort_index().cumsum()
s2 = df.groupby('week').apply(lambda x: roc_auc_score(x.actual,x.expected))
df = (pd.concat([s1, s2], axis=1, keys=('count of records','AUC'))
.rename_axis('week')
.reset_index())
print (df)
week count of records AUC
0 10 2 1.000000
1 11 6 0.166667
2 12 10 0.750000
import pandas as pd
import numpy as np
from sklearn import datasets
iris = datasets.load_iris()
data = pd.concat([pd.DataFrame(iris.target, columns = ['target']),
pd.DataFrame(iris.data, columns = iris.feature_names)], axis = 1)
def target_replace(x):
for i in [0,1,2]:
if x == i:
return(iris.target_names[i])
data.target = data.target.apply(target_replace)