小编Pet*_*tig的帖子

在data.table上应用自定义函数而不是使用plyr和ddply

我正在处理一个名为orderFlow的data.table,并计算potentialWelfare.tmp作为输出.到目前为止,以下基于plyr的方法一直是我的解决方案,但由于输入orderFlow有数百万行,我更喜欢利用R中data.table的性能的解决方案.

    # solution so far, poor performance on huge orderFlow input data.table
    require(plyr)
    potentialWelfare.tmp = ddply(orderFlow, 
                       .variables = c("simulationrun_id", "db"), 
                       .fun = calcPotentialWelfare, 
                       .progress = "text", 
                       .parallel=TRUE)
Run Code Online (Sandbox Code Playgroud)

编辑1:简而言之,自定义功能检查df中是否有更多出价或要求,并对按报价(按估值)出价的NbAsks的估值求和.这样做是为了选择最有价值的出价并总结其估值.代码是遗留的,可能效率不高,但它与plyr和普通的data.frames结合使用.

    calcPotentialWelfare <- function(df){
       NbAsks = dim(df[df$type=="ask",])[1]
    #   print(NbAsks)
      Bids = df[df$type == "bid",]
    #         dd[with(dd, order(-z, b)), ]
      Bids = Bids[with(Bids,order(valuation,decreasing = TRUE)),]
      NbBids = dim(df[df$type == "bid",])[1]
    #   print(Bids)
      if (NbAsks > 0){
        Bids = Bids[1:min(NbAsks,NbBids),]
        potentialWelfare = sum(Bids$valuation)
        return(potentialWelfare)
      } …
Run Code Online (Sandbox Code Playgroud)

r plyr data.table

9
推荐指数
1
解决办法
7301
查看次数

用户定义的cmp函数的python排序函数

我想使用各种比较器功能在字典中订购项目.请参阅下面的示例代码.这是使用cmpRatio函数和sorted()的最后一部分,它不起作用.我不确定我做错了什么.提前感谢任何想法!

mydict = { 'a1': (1,6),
          'a2': (10,2),
          'a3': (5,3),
          'a4': (1,2),
          'a5': (3,9),
          'a6': (9,7) }

# sort by first element of the value tuple: WORKS
print sorted(mydict.iteritems(), key=lambda (k,v): v[0])

# sort by second element of the value tuple: WORKS
print sorted(mydict.iteritems(), key=lambda (k,v): v[1])

# THIS is what I can't get working:
def cmpRatio(x,y):
   sx = float(x[0])/x[1]
   sy = float(y[0])/y[1]
   return sx < sy

# sort by sum of the elements in the value tuple: DOES NOT …
Run Code Online (Sandbox Code Playgroud)

python sorted

8
推荐指数
1
解决办法
2万
查看次数

Gurobi:获取优化运行时

我想访问在 python 中运行 gurobi 数学优化问题时找到模型最优解所需的时间。m

到目前为止我使用

runtime = m.Runtime
print("The run time is %f" % runtime)
Run Code Online (Sandbox Code Playgroud)

不幸的是,返回的运行时间始终为 0.0,与求解模型所花费的时间无关,并且在达到任何时间限制之前。

    m.setParam("TimeLimit", timeLimit)
Run Code Online (Sandbox Code Playgroud)

如何通过 gurobipy 在 gurobi 中访问实际运行时?我已阅读Gurobi 参考手册,但没有成功。

python mathematical-optimization gurobi

4
推荐指数
1
解决办法
9109
查看次数

使用 ggplot2 在一个组合图中绘制密度和累积密度函数

我想得到一个结合了观测密度和累积分布函数的图。

通常的问题是两者的规模相差甚远。如何解决这个问题,即使用两个尺度,或者重新调整其中一个数据系列(最好在 ggplot 内,因为我想将数据的计算和显示分开)。

这是到目前为止的代码:

>dput(tmp) 产量

structure(list(drivenkm = c(8, 11, 21, 4, 594, 179, 19, 7, 10, 36)), .Names = "drivenkm", class = c("data.table", "data.frame" ), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x223cb78>)

那我就做

p = ggplot(data = tmp, aes(x = drivenkm)) + geom_histogram(aes(y = ..density..), alpha = 0.2, binwidth = 3) + stat_ecdf(aes(x = drivenkm)); print(p)

我得到的是以下内容:

在此输入图像描述

显然,天平相差甚远。如何解决这个问题,以便可以以合理的方式解释直方图和 cdf?

谢谢!

r ggplot2 cdf ecdf density-plot

2
推荐指数
1
解决办法
2037
查看次数

traefik:使用docker-compose时如何使用基本身份验证?

我正在尝试使用带有docker compose的traefik.

根据我在他们网站上看到的帖子,你可以使用这样的东西:

mytest-steph: image: myimage ports: - "45001:45001" labels: - "traefik.backend=test_steph" - "traefik.frontend.rule=Host:test.mydomain.com;PathPrefix:/myprefix" - "traefik.backend.port=8080" - "traefik.frontend.auth.basic=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"

但是当我启动docker-compose时,我得到: WARNING: The apr1 variable is not set. Defaulting to a blank string. WARNING: The H6uskkkW variable is not set. Defaulting to a blank string. WARNING: The IgXLP6ewTrSuBkTrqE8wj variable is not set. Defaulting to a blank string.

有没有人实现使用这样的基本身份验证?

traefik

2
推荐指数
2
解决办法
3191
查看次数