假设我有一个大矩阵:
M <- matrix(rnorm(1e7),nrow=20)
Run Code Online (Sandbox Code Playgroud)
进一步假设每列代表样品.说我想申请t.test()每一栏,有没有办法做到这比使用快得多apply()?
apply(M, 2, t.test)
Run Code Online (Sandbox Code Playgroud)
在我的计算机上运行分析只需不到2分钟:
> system.time(invisible( apply(M, 2, t.test)))
user system elapsed
113.513 0.663 113.519
Run Code Online (Sandbox Code Playgroud) 之前我问过如何在没有引号的情况下接受文本/字符参数.在我在该问题中提供的场景中,参数的数量是固定的,因此我在函数定义中使用的eval(substitute())的数量对应于我拥有的参数的数量.
现在我有一个场景,我有一个参数,例如factors(见下文),用户可以指定多个列名而不使用它们周围的引号 - 即,他们将使用factor1而不是"factor1".我想评估用户提供的每个列名.
foo<-function(data.frame, factors){
}
Run Code Online (Sandbox Code Playgroud)
问题1:我想知道当表达式的数量可以变化时,是否有办法将eval(substitute())应用于多个表达式.
正如所指出的,eval(substitute())可能具有潜在的危险性,并且在某些情况下可能会失败.
问题2:除了使用引用的列名之外,还有更优雅的方式来处理问题,如下所示:
foo<-function(data.frame, factors){
output<-data.frame[, factors]
output
}
foo(data.frame=dataset, factors=c("factor1", "factor2"))
Run Code Online (Sandbox Code Playgroud) 下面是我写的C代码的一部分.该函数foo将在R中调用.代码不断导致R崩溃,我将问题缩小到此outer()函数,该函数用于计算外部和或差.请注意注释掉的部分:如果我没有注释掉,如果每个数组包含超过1000个数据点,该函数将导致R崩溃.如果我将其评论出来,我可以计算出明显更长的数组的外部和/差异而没有问题(例如,每个数组超过100000个数据点).我想知道问题是什么......谢谢!
#include <R.h>
#include <Rmath.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void outer(double *x1, double *x2, int *n, int operation, double *output){
int i, j;
if(operation==1){
for(i=0; i<*n; i++){
for(j=0; j<*n; j++){
output[(*n)*i+j]=x1[j]+x2[i];
}
}
} else if(operation==2){
for(i=0; i<*n; i++){
for(j=0; j<*n; j++){
output[(*n)*i+j]=x1[j]-x2[i];
//Rprintf("%d ", (*n)*i+j); //<-----------HERE
}
}
}
}
void foo(double *x, double *y, int *npred, int *nsamp){
int oper=2;
double xouter[*nsamp], youter[*nsamp];
double outer_temp_x[(*nsamp)*(*nsamp)], outer_temp_y[(*nsamp)*(*nsamp)];
outer(x, x, nsamp, oper, …Run Code Online (Sandbox Code Playgroud) 我有两个 numpy 数组:
数组 1:500,000 行 x 100 列
数组 2:160,000 行 x 100 列
我想在 Array 1和Array 2 中的每一行之间找到最大的余弦相似度。换句话说,我计算数组 1 中第一行与数组 2 中所有行之间的余弦相似度,并找到最大余弦相似度,然后计算数组 1 中第二行与数组 2 中所有行之间的余弦相似度数组2,求最大余弦相似度;并对数组 1 的其余部分执行此操作。
我目前使用sklearn的cosine_similarity()功能并执行以下操作,但速度非常慢。我想知道是否有一种更快的方法不涉及多处理/多线程来完成我想做的事情。此外,我拥有的数组并不稀疏。
from sklearn.metrics.pairwise import cosine_similarity as cosine
results = []
for i in range(Array1.shape[0]):
results.append(numpy.max(cosine(Array1[None,i,:], Array2)))
Run Code Online (Sandbox Code Playgroud) 假设我使用了两种嵌套随机效应模型,anova()结果如下:
new.model: new
current.model: new
Df AIC BIC logLik Chisq Chi Df Pr(>Chisq)
new.model 8 299196 299259 -149590
current.model 9 299083 299154 -149533 115.19 1 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Run Code Online (Sandbox Code Playgroud)
我想只使用表格部分(见下文):
Df AIC BIC logLik Chisq Chi Df Pr(>Chisq)
new.model 8 299196 299259 -149590
current.model 9 299083 299154 -149533 115.19 1 < 2.2e-16 ***
Run Code Online (Sandbox Code Playgroud)
我知道我能够通过使用属性(anova.object)$ heading = NULL将标题设置为null来摆脱标题部分(请参阅打击),但我不知道如何摆脱底部:Signif .代码:.....
new.model: new
current.model: new
Run Code Online (Sandbox Code Playgroud)
我至关重要的是不想使用data.frame(见下文),因为它将空白单元格更改为NA
data.frame(anova(new.model, …Run Code Online (Sandbox Code Playgroud) 我正在尝试提出一种不涉及使用其他软件包(例如ggplot)的解决方案。虽然绘制多条线非常简单,但我还没有找到将不同参数值(例如,不同颜色)应用于不同线的方法。下面的代码(带有生成的图)是我的尝试,显然没有达到我想要的效果。我也不想使用循环,因为我试图使我的脚本尽可能简单。
df = cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10)))
plot(0, xlim = c(1,10), ylim=range(df), type="n")
apply(df, 2, lines, type="b", col = c("red", "blue", "black"))
Run Code Online (Sandbox Code Playgroud)
我真正想要的是如下图:
plot(0, xlim = c(1,10), ylim=range(df), type="n")
color = c("red","blue","black")
for(i in 1:3){
lines(1:10, df[,i], type = "b", col=color[i])
}
Run Code Online (Sandbox Code Playgroud)
先感谢您!
假设我有一个如下所示的数据集:
set.seed(1)
dataset <- data.frame(x = sort(rnorm(100)),
y = sort(rlnorm(100))+1:4,
group=rep(letters[1:4], 25))
Run Code Online (Sandbox Code Playgroud)
我想创建一个情节使用ggplot2.我没有手动选择颜色,而是使用预定义的颜色集Paired:
ggplot(dataset, aes(x = x, colour = group)) +
geom_line(aes(y=y)) +
scale_colour_brewer(palette="Paired")
Run Code Online (Sandbox Code Playgroud)
我得到的图如下所示:组的数据点a和b蓝色的两种颜色,而组的数据点c和d绿色的两种阴影.

假设现在,我想只绘制相应组的数据c和d,我想用绿色的两个色调.如果我只是做以下事情:
ggplot(dataset[dataset$group %in% c("c", "d"),], aes(x = x, colour = group)) +
geom_line(aes(y=y)) +
scale_colour_brewer(palette="Paired")
Run Code Online (Sandbox Code Playgroud)
该功能将自动选择两种蓝色阴影(见下文),因为它们首先出现在Paired调色板中.

ggplot2对不起,如果问题不清楚.随意改变它.
所以基本上我试图找到一种方法,以便函数的文本/字符串参数不需要引号.
foo = function(x, y, data){
n1 = length(data[,x])
n2 = length(data[,y])
cat(n1, n1)
}
Run Code Online (Sandbox Code Playgroud)
如果我使用以下代码
data(survey)
foo(Sex, Fold, survey)
Run Code Online (Sandbox Code Playgroud)
我会收到一条错误消息.但是,如果我使用以下内容:
foo("Sex", "Fold", survey)
Run Code Online (Sandbox Code Playgroud)
要么
foo(1, 5, survey)
Run Code Online (Sandbox Code Playgroud)
这个功能会给我我想要的东西.所以我想知道是否有任何方法来构造函数,以便我不需要在列名称周围使用引号.谢谢!
[社区编辑提供可重复的示例:]
def main():
e = None
print(locals())
while not e:
try:
raise Exception
except Exception as e:
pass
main()
Run Code Online (Sandbox Code Playgroud)
产生
~/coding$ python3.3 quiz2.py
{'e': None}
Traceback (most recent call last):
File "quiz2.py", line 11, in <module>
main()
File "quiz2.py", line 5, in main
while not e:
UnboundLocalError: local variable 'e' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
[已编辑]包含可重现的代码
我正在尝试运行while循环,我使用的条件是循环在变量时继续e==None.相关代码如下:
print("\nThe current score list contains the following people's scores: ")
score_list = open("score_list.dat", "rb")
score_name = []
e = None
while not e: …Run Code Online (Sandbox Code Playgroud) 假设我有一个具有多个返回值的函数(如下所示)。但是,此输出并不提供信息,因为用户除非查找函数定义,否则不知道每个值代表什么。所以我想用println()适当的名称将结果打印到屏幕上,同时抑制实际返回的值打印在屏幕上。在 R 中,函数invisible()可以做到这一点,但是如何在 Julia 中做同样的事情呢?
function trimci(x::Array; tr=0.2, alpha=0.05, nullvalue=0)
se=sqrt(winvar(x,tr=tr))./((1-2.*tr)*sqrt(length(x)))
ci=cell(2)
df=length(x)-2.*floor(tr.*length(x))-1
ci=[tmean(x, tr=tr)-qt(1-alpha./2, df).*se, tmean(x, tr=tr)+qt(1-alpha./2, df).*se]
test=(tmean(x,tr=tr)-nullvalue)./se
sig=2.*(1-pt(abs(test),df))
return ci, tmean(x, tr=tr), test, se, sig
end
Run Code Online (Sandbox Code Playgroud) r ×7
apply ×2
matrix ×2
python ×2
anova ×1
attributes ×1
c ×1
character ×1
colors ×1
crash ×1
dataframe ×1
eval ×1
exception ×1
function ×1
ggplot2 ×1
julia ×1
line ×1
numpy ×1
plot ×1
python-3.x ×1
quotes ×1
scikit-learn ×1
substitution ×1
text ×1
while-loop ×1