小编Kei*_*ith的帖子

如何连接因子,而不将它们转换为整数级别?

我很惊讶地发现,当连接向量时,R会将因子强制转换为数字.即使水平相同,也会发生这种情况.例如:

> facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> facs
[1] i       want    to      be      a       factor  not     an      integer
Levels: a an be factor i integer not to want
> c(facs[1 : 3], facs[4 : 5])
[1] 5 9 8 3 1
Run Code Online (Sandbox Code Playgroud)

在R中执行此操作的惯用方法是什么(在我的情况下,这些向量可能非常大)?谢谢.

r concatenation r-factor

39
推荐指数
4
解决办法
2万
查看次数

选择Haskell解析器

Haskell中有许多可供我们使用的开源解析器实现.Parsec似乎是文本解析的标准,而attoparsec似乎是二进制解析的流行选择,但除此之外我不太了解.是否存在选择解析器实现的特定决策树?您是否了解过有关图书馆优势或劣势的任何有趣内容?

parsing haskell

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

自动格式化R代码的工具

有没有可以自动重新格式化R代码的工具(编辑器,脚本,等等......)?它不需要是可自定义的,但它必须能够识别由分号或换行符分隔的语句,因为此代码具有两者.如果它可以将所有语句放在一个单独的行上,一致地缩进代码块并始终如一地放置括号,我将非常高兴.

编辑:总结调查结果

谢谢你的答案.这是我发现的.

  • ESS和StatET都是优秀的R编辑器,并且可以很好地自动缩进代码块.StatET允许您一次选择全部并重新缩进文件中的所有内容.从我可以告诉ESS允许你一次缩进整个函数def而不是整个文件(如果我错过了,请纠正我).这些都不会修复大括号放置或分解多语句行.(例如:i = n*b; a = i + 1)
  • formatR太棒了.除了修复缩进之外,它还将一致地放置大括号并拆分多语句行.

这是我编写的一个小函数,以便我可以转换整个源目录(使用与formatR相同的底层函数,这在动画包中很奇怪).

library("animation")

tidy.all <- function(inDir = NULL, outDir = NULL, ...) {
    if (is.null(inDir) || is.na(outDir)) 
        stop("inDir can't be null or NA")
    if (!file.info(inDir)$isdir) 
        stop("inDir must be a directory")

    if (is.null(outDir) || is.na(outDir)) 
        stop("outDir can't be null or NA")
    if (!file.exists(outDir)) 
        dir.create(outDir)
    if (!file.info(outDir)$isdir) 
        stop("outDir must be a directory")

    for (f in dir(inDir)) {
        currFile <- file.path(inDir, f)
        if (length(grep(".*\\.R$", currFile, perl = T))) {
            outFile <- file.path(outDir, …
Run Code Online (Sandbox Code Playgroud)

formatting r autoformatting

17
推荐指数
3
解决办法
8796
查看次数

tensorflow索引如何工作

我无法理解张量流的基本概念.索引如何用于张量读/写操作?为了使这一点具体化,如何将以下numpy示例转换为tensorflow(使用张量来分配数组,索引和值):

x = np.zeros((3, 4))
row_indices = np.array([1, 1, 2])
col_indices = np.array([0, 2, 3])
x[row_indices, col_indices] = 2
x
Run Code Online (Sandbox Code Playgroud)

输出:

array([[ 0.,  0.,  0.,  0.],
       [ 2.,  0.,  2.,  0.],
       [ 0.,  0.,  0.,  2.]])
Run Code Online (Sandbox Code Playgroud)

......而且......

x[row_indices, col_indices] = np.array([5, 4, 3])
x
Run Code Online (Sandbox Code Playgroud)

输出:

array([[ 0.,  0.,  0.,  0.],
       [ 5.,  0.,  4.,  0.],
       [ 0.,  0.,  0.,  3.]])
Run Code Online (Sandbox Code Playgroud)

......最后......

y = x[row_indices, col_indices]
y
Run Code Online (Sandbox Code Playgroud)

输出:

array([ 5.,  4.,  3.])
Run Code Online (Sandbox Code Playgroud)

tensorflow

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

首次事件通知时GWT removeHandler

我想在第一次收到事件时删除GWT事件处理程序.我还想避免使用非必要的跟踪注册对象来污染我的类.我目前将其编码为:

final HandlerRegistration[] registrationRef = new HandlerRegistration[1];
registrationRef[0] = dialog.addFooHandler(new FooHandler()
{
    public void onFoo(FooEvent event)
    {
        HandlerRegistration removeMe = registrationRef[0];
        if(removeMe != null)
        {
            removeMe.removeHandler();
        }

        // do stuff here
    }
});

但使用registrationRef会降低代码的可读性.有没有更好的方法来做到这一点而不向我的班级添加变量?

java events gwt event-handling

12
推荐指数
1
解决办法
9645
查看次数

用DB后端清理芹菜任务

我正在努力了解如何以及何时清理芹菜中的任务.从查看任务文档,我看到:

将根据CELERY_TASK_RESULT_EXPIRES设置自动清除旧结果.默认情况下,它设置为在1天后过期:如果您有一个非常繁忙的群集,则应该降低此值.

但这个引用来自RabbitMQ结果后端部分,我没有在数据库后端部分看到任何类似的文本.所以我的问题是:是否有一种后端不可知的方法我可以用芹菜进行旧任务清理,如果没有,我应该采取DB后端特定的方法吗?如果它有任何区别我正在使用django-celery.谢谢.

celery django-celery

10
推荐指数
1
解决办法
7985
查看次数

F#:在异步返回之前的SwitchToThreadPool的目的

Async.SwitchToNewThread的MS文档中,给出的示例之一是:

let asyncMethod f = 
    async {  
        do! Async.SwitchToNewThread() 
        let result = f() 
        do! Async.SwitchToThreadPool() 
        return result
    } 
Run Code Online (Sandbox Code Playgroud)

在return语句之前立即切换到线程池的目的是什么?我理解为什么你可能想要在异步块有更多工作要做的时候从专用线程切换到线程池,但事实并非如此.

这不是主要问题的一部分,但我也很想知道为什么SwitchToNewThread和SwitchToThreadPool返回Async.有没有一个用例,你不想立即"做!" 这些任务?谢谢

concurrency f# multithreading asynchronous

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

cecil:Instruction.Operand类型对应于Instruction.OpCode.Code值

是否有任何文档或是否有一部分cecil源代码我可以参考,以全面了解Operandcecil将用于给定Code值的类型?例如:我可以搜集来自MethodBodyRocksLdloc接受一个Operand类型的VariableDefinition,但我一直无法追查下来用于其他一些指令代码.

c# mono il cil mono.cecil

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

F#:类型和函数之间的相互递归可能吗?

我可以使用and关键字来设置相互递归的函数定义.我也可以and用于相互递归的类型,但是如果类型和函数之间存在相互递归的关系呢?我唯一的选择是使该功能成为该类型的成员,还是我可以使用类似的东西and

编辑:添加一个简化的伪示例,我希望说明我正在尝试做什么

// A machine instruction type
type Instruction = Add | CallMethod int (* method ID *) | ...

// A class representing a method definition
type MethodDef (fileName : string) =
    member x.Params with get () = ...
    member x.Body with get() =
        let insts = readInstructions fileName
        Array.map toAbstractInst insts

// a more abstract view of the instructions
and AbstractInstruction = AbstAdd | AbstCallMethod MethodDef | ...

// a function that …
Run Code Online (Sandbox Code Playgroud)

f# mutual-recursion

6
推荐指数
1
解决办法
1455
查看次数

循环习语危险吗?

以"R简介"为例

xc <- split(x, ind)
yc <- split(y, ind)
for (i in 1:length(yc)) {
    plot(xc[[i]], yc[[i]])
    abline(lsfit(xc[[i]], yc[[i]]))
}
Run Code Online (Sandbox Code Playgroud)

for(i in 1:length(yc)) { ...在您需要当前索引的句柄的情况下,似乎是迭代列表或向量的习惯用法.然而,这在空列表的情况下会中断,因为1:0它不是空向量.当你不能保证非空列表时,我应该用什么成语来迭代列表/向量索引?我在想if(length(yc)) for(i in 1:length(yc)) { ...但是有更好的方法吗?

r

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

了解python中的数据库连接池

我不确定我是否了解python中数据库连接池的用例(例如:psycopg2.pool和mysql.connector.pooling)。在我看来,由于GIL,并行性通常是在python中使用多进程而不是多线程方法实现的,并且在多进程情况下,这些池不是很有用,因为每个进程都会初始化自己的池并且一次只能运行一个线程。它是否正确?使用多个进程时,是否有任何共享数据库连接池的策略?如果不是,则池的有用性仅限于多线程python应用程序,或者是否存在使用它们的其他方案?

python database multithreading fork connection-pooling

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