我的 dataframe :
from random import random, randint
from pandas import DataFrame
t = DataFrame({"metasearch":["A","B","A","B","A","B","A","B"],
"market":["A","B","A","B","A","B","A","B"],
"bid":[random() for i in range(8)],
"clicks": [randint(0,10) for i in range(8)],
"country_code":["A","A","A","A","A","B","A","B"]})
我想为每个市场都适合LinearRegression,所以我:
1)组df-组= t.groupby(by =“ market”)
2)准备要适合模型的功能-
from sklearn.linear_model import LinearRegression
def group_fitter(group):
lr = LinearRegression()
X = group["bid"].fillna(0).values.reshape(-1,1)
y = group["clicks"].fillna(0)
lr.fit(X, y)
return lr.coef_[0] # THIS IS A SCALAR
3)创建一个新的系列,以市场为指数,以系数为值:
s = groups.transform(group_fitter)
但是第3步失败:KeyError :(“ bid_cpc”,“在出价时出现”)