我想创建数据框架,可能是稀疏的,它测量用户之间的相关性。user_1在这里,我对和之间相关性的定义是它们在同一天user_2执行相同操作的次数。action
我将尝试用一个例子更好地解释自己。假设我有以下数据框:
date action user
6 2019-05-05 b user_3
9 2019-05-05 b user_2
1 2019-05-06 b user_2
5 2019-05-06 a user_1
0 2019-05-07 b user_3
7 2019-05-07 a user_2
8 2019-05-07 a user_1
2 2019-05-08 c user_2
4 2019-05-08 c user_1
3 2019-05-09 c user_3
Run Code Online (Sandbox Code Playgroud)
可以使用以下代码片段生成:
import numpy as np
import pandas as pd
np.random.seed(12)
users = np.random.choice(['user_1', 'user_2', 'user_3'], size=10)
actions = np.random.choice(['a', 'b', 'c'], size=10)
date = np.random.choice(pd.date_range(start='2019-05-05', end='2019-05-10', freq='D'), size=10)
df = …Run Code Online (Sandbox Code Playgroud) 我需要更改给__call__定对象的方法的行为。天真的方法是这样的:
class A(object):
def __call__(self):
return 1
def new_call():
return 42
a = A()
a.__call__ = new_call
Run Code Online (Sandbox Code Playgroud)
为什么输出不是a()42?我可以利用一种解决方法来达到相同的效果吗?(不使用该类)
===========================编辑===================== ===========
根据记录,简短的回答是否定的。Python 直接在类上而不是在实例上调用“特殊方法” __call_,因此如果需要更改方法,则需要在类本身上进行更改。