python - Using Truncated Poisson in Pandas data frame
问题描述
Withe help of @ Welcome to Stack Overflow, I managed to truncate the Poisson distribution using upper limit.When I used the function so called truncated Poisson, which is user defined function, it worked with single value entry, which I have shown in the code below:
import scipy.stats as sct
import pandas as pd
def truncated_Poisson(mu, max_value, size):
temp_size = size
while True:
temp_size *= 2
temp = sct.poisson.rvs(mu, size=temp_size)
truncated = temp[temp <= max_value]
if len(truncated) >= size:
return truncated[:size]
mu = 2.5
max_value = 10
print(truncated_Poisson(mu, max_value, 1))
Unfortunately, I threw me an error when I applied it in the data frame as follow:
data = pd.DataFrame()
data['Name'] = ['A','B','C','D','E']
data ['mu'] = [0.5,1.2,2,2.5,2.8]
max_value = 5
size = 1
data ['Pos'] = truncated_Poisson(data.mu,max_value,size = 1)
The error statement is
ValueError: size does not match the broadcast shape of the parameters.
Can anyone advise me how to use that function in dataframe?
Thanks
Zep.
解决方案
据我了解,您想truncated_Poisson
使用相同的参数和mu
数据中的每一个进行调用。例如,您可以使用以下方法执行此操作.apply
:
data['Pos'] = data.mu.apply(lambda mu: truncated_Poisson(mu, max_value, size=1))
>>> data
Name mu Pos
0 A 0.5 [0]
1 B 1.2 [0]
2 C 2.0 [3]
3 D 2.5 [4]
4 E 2.8 [3]
推荐阅读
- sql - 如何从Oracle中的句子中删除符号?
- node.js - 使用/在 Shibboleth 身份验证后进行的数据库身份验证
- ubuntu - 让我们在 emqtt 上加密 ssl
- signalr - SignalR 只是终止特定客户端中的许多任务中的单个长时间运行的任务
- firebase - 是否需要为每个子集合明确定义安全规则?
- react-native - React Native - 如何在 android 中将图像转换为 Base64 和 Visa,反之亦然
- python-3.x - Python3:如何从 url 获取标题 eng?
- here-api - HERE API:在针对前端域进行保护时使用后端的 app_id 和 app_code
- sql-server - 消息 102,第 15 级
- vue.js - 如何使用 vue.js 按日期对消息进行分组?[Vue 警告]:你可能有一个无限更新循环