给定以下NumPy数组,
> a = array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5],[1, 2, 3, 4, 5]])
Run Code Online (Sandbox Code Playgroud)
它很简单,可以改变一行,
> shuffle(a[0])
> a
array([[4, 2, 1, 3, 5],[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]])
Run Code Online (Sandbox Code Playgroud)
是否可以使用索引表示法独立地对每个行进行洗牌?或者你必须迭代数组.我有类似的想法,
> numpy.shuffle(a[:])
> a
array([[4, 2, 3, 5, 1],[3, 1, 4, 5, 2],[4, 2, 1, 3, 5]]) # Not the real output
Run Code Online (Sandbox Code Playgroud)
虽然这显然不起作用.
我有以下脚本模拟我拥有的数据结构类型和我想要做的分析,
library(ggplot2)
library(reshape2)
n <- 10
df <- data.frame(t=seq(n)*0.1, a =sort(rnorm(n)), b =sort(rnorm(n)),
a.1=sort(rnorm(n)), b.1=sort(rnorm(n)),
a.2=sort(rnorm(n)), b.2=sort(rnorm(n)))
head(df)
mdf <- melt(df, id=c('t'))
## head(mdf)
levels(mdf$variable) <- rep(c('a','b'),3)
g <- ggplot(mdf,aes(t,value,group=variable,colour=variable))
g +
stat_smooth(method='lm', formula = y ~ ns(x,3)) +
geom_point() +
facet_wrap(~variable) +
opts()
Run Code Online (Sandbox Code Playgroud)
除此之外,我想做的是绘制平滑函数的一阶导数以t
反对因子c('a','b')
.任何建议如何去做这件事将不胜感激.
我有一个数据框,其中列最初标记为任意.稍后,我想将这些级别更改为数值.以下脚本说明了该问题.
library(ggplot2)
library(reshape2)
m <- 10
n <- 6
nam <- list(c(),letters[1:n])
var <- as.data.frame(matrix(sort(rnorm(m*n)),m,n,F,nam))
dtf <- data.frame(t=seq(m)*0.1, var)
mdf <- melt(dtf, id=c('t'))
xs <- c(0.25,0.5,1.0,2.0,4.0,8.0)
levels(mdf$variable) <- xs
g <- ggplot(mdf,aes(variable,value,group=variable,colour=t))
g +
geom_point() +
#scale_x_continuous() +
opts()
Run Code Online (Sandbox Code Playgroud)
这个图是产生的.
'变量'量在图上均匀分布,即使在数字上这不是真的.如何才能使x轴上的间距正确?
通过大量试验和错误,我发现了以下几行python代码,
for N in range(2**1,2**3):
print [(2**n % (3*2**(2*N - n))) % (2**N-1) for n in range(2*N+1)]
Run Code Online (Sandbox Code Playgroud)
产生以下输出,
[1, 2, 1, 2, 1]
[1, 2, 4, 1, 4, 2, 1]
[1, 2, 4, 8, 1, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 1, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 1, 32, 16, 8, 4, 2, 1]
[1, 2, 4, 8, 16, 32, 64, 1, 64, 32, 16, 8, 4, 2, 1]
Run Code Online (Sandbox Code Playgroud)
即2 2**(N-1) …
两个随机变量x和y之和的概率分布由各个分布的卷积给出.我在数字上做这个有点麻烦.在以下示例中,x和y均匀分布,其各自的分布近似为直方图.我的推理说直方图应该是复杂的,给出x + y的分布.
from numpy.random import uniform
from numpy import ceil,convolve,histogram,sqrt
from pylab import hist,plot,show
n = 10**2
x,y = uniform(-0.5,0.5,n),uniform(-0.5,0.5,n)
bins = ceil(sqrt(n))
pdf_x = histogram(x,bins=bins,normed=True)
pdf_y = histogram(y,bins=bins,normed=True)
s = convolve(pdf_x[0],pdf_y[0])
plot(s)
show()
Run Code Online (Sandbox Code Playgroud)
给出以下,
换句话说,如预期的那样是三角形分布.但是,我不知道如何找到x值.如果有人能在这里纠正我,我将不胜感激.
ggplot2 ×2
numpy ×2
python ×2
r ×2
algorithm ×1
convolution ×1
dataframe ×1
histogram ×1
math ×1
probability ×1
r-factor ×1
random ×1
smoothing ×1
statistics ×1