在使用ggplot创建条形图时,我遇到麻烦,获得首选的千位分隔符.我希望数千个用点而不是逗号分隔.像这样它没有分隔符:
require(ggplot2)
require(scales)
options(scipen=10)
D = data.frame(x=c(1,2),y=c(1000001,500000))
p = ggplot(D,aes(x,y)) + geom_bar(stat="identity")
p
Run Code Online (Sandbox Code Playgroud)
并且像这样它给出一个逗号:
p + scale_y_continuous(labels=comma)
Run Code Online (Sandbox Code Playgroud)
你怎么得到一个点数千分隔符?除了http://docs.ggplot2.org/0.9.3.1/scale_continuous.html上的一些示例之外,我找不到关于哪些类型的其他标签存在的文档.
提前致谢,
极限竞速
在Python中使用numpy生成所有组合的数组有几个优雅的例子.例如答案:使用numpy构建两个数组的所有组合的数组.
现在假设存在一个额外的约束,即所有数字的总和不能超过给定的常数K.使用生成器itertools.product,例如K=3我们想要三个变量的组合,范围为0-1,0-3和0-2,我们可以这样做:
from itertools import product
K = 3
maxRange = np.array([1,3,2])
states = np.array([i for i in product(*(range(i+1) for i in maxRange)) if sum(i)<=K])
Run Code Online (Sandbox Code Playgroud)
返回
array([[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 1, 0],
[0, 1, 1],
[0, 1, 2],
[0, 2, 0],
[0, 2, 1],
[0, 3, 0],
[1, 0, 0],
[1, 0, 1],
[1, 0, 2],
[1, 1, 0],
[1, 1, 1],
[1, 2, 0]])
Run Code Online (Sandbox Code Playgroud)
原则上,来自 …
我在R中创建条形图时遇到了一个小问题.有3个变量:
a <- c(3,3,2,1,0)
b <- c(3,2,2,2,2)
c <- 0:4
Run Code Online (Sandbox Code Playgroud)
条形图应按"a"和"c"分组,"b"应堆叠在"a"的顶部.单独进行分组和堆叠非常简单:
barplot(rbind(a,c), beside=TRUE)
barplot(rbind(a,b), beside=FALSE)
Run Code Online (Sandbox Code Playgroud)
如何在一个图表中同时执行这两个操作?
假设需要计算一般数量的离散概率密度函数的卷积.对于下面的示例,有四个分布采用具有指定概率的值0,1,2:
import numpy as np
pdfs = np.array([[0.6,0.3,0.1],[0.5,0.4,0.1],[0.3,0.7,0.0],[1.0,0.0,0.0]])
Run Code Online (Sandbox Code Playgroud)
卷积可以这样找到:
pdf = pdfs[0]
for i in range(1,pdfs.shape[0]):
pdf = np.convolve(pdfs[i], pdf)
Run Code Online (Sandbox Code Playgroud)
然后给出看到0,1,...,8的概率
array([ 0.09 , 0.327, 0.342, 0.182, 0.052, 0.007, 0. , 0. , 0. ])
Run Code Online (Sandbox Code Playgroud)
这部分是我的代码的瓶颈,似乎必须有一些东西可用于矢量化这个操作.有没有人建议让它更快?
或者,您可以使用的解决方案
pdf1 = np.array([[0.6,0.3,0.1],[0.5,0.4,0.1]])
pdf2 = np.array([[0.3,0.7,0.0],[1.0,0.0,0.0]])
convolve(pd1,pd2)
Run Code Online (Sandbox Code Playgroud)
得到成对的卷积
array([[ 0.18, 0.51, 0.24, 0.07, 0. ],
[ 0.5, 0.4, 0.1, 0. , 0. ]])
Run Code Online (Sandbox Code Playgroud)
也会有很大的帮助.
在我的 Python 程序中,我连接了几个整数和一个数组。如果这行得通,那将是直观的:
x,y,z = 1,2,np.array([3,3,3])
np.concatenate((x,y,z))
Run Code Online (Sandbox Code Playgroud)
但是,所有整数都必须转换为 np.arrays:
x,y,z = 1,2,np.array([3,3,3])
np.concatenate((np.array([x]),np.array([y]),z))
Run Code Online (Sandbox Code Playgroud)
特别是如果您有很多变量,那么手动转换会很乏味。问题是 x 和 y 是 0 维数组,而 z 是 1 维数组。有没有办法在没有转换的情况下进行连接?
我有以下问题。有两个 n 维整数数组,我需要确定满足多个条件的项目的索引。
所以假设我们有:
array1 = np.array([1,-1,-2])
array2 = np.array([0,1,1])
Run Code Online (Sandbox Code Playgroud)
然后它应该返回索引 2(第三个数字)。我正在尝试按如下方式编程:
import numpy as np
n = 3
array1 = np.array([1,-1,-2])
array2 = np.array([0,1,1])
indices = [i for i in range(n) if array1[i]<0]
indices2 = [i for i in indices if array2[i] == min(array2[indices])]
index = [i for i in indices2 if array1[i] == min(array1[indices2])][0] #[0] breaks the tie.
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但是,我觉得它不是很优雅。在我看来,您应该能够在一两行中完成此操作,并且定义较少的新变量。有人有改进的建议吗?提前致谢。
这可能是之前被问过的,但是快速搜索并没有给我答案.
假设有一个包含所有变量的字典.如何将此字典传递给函数的本地命名空间?例如:
data = dict(a=1,b=2,c=3,d=4)
def f(data):
return data['a'] + data['d']
Run Code Online (Sandbox Code Playgroud)
需要在您想要访问的每个变量周围写入数据[''].如何将字典的所有条目添加到函数的本地命名空间?对于您可以使用的对象
self.__dict__.update(data)
Run Code Online (Sandbox Code Playgroud)
是否存在与函数等效的东西,以便您获得以下内容:
data = dict(a=1,b=2,c=3,d=4)
def f(data):
add_to_local_namespace(data)
return a + d
Run Code Online (Sandbox Code Playgroud) 我想对矩阵的每 n 列求和。如何在不使用 for 循环的情况下以简单的方式做到这一点?这就是我现在所拥有的:
n = 3 #size of a block we need to sum over
total = 4 #total required sums
ncols = n*total
nrows = 10
x = np.array([np.arange(ncols)]*nrows)
result = np.empty((total,nrows))
for i in range(total):
result[:,i] = np.sum(x[:,n*i:n*(i+1)],axis=1)
Run Code Online (Sandbox Code Playgroud)
结果将是
array([[ 3., 12., 21., 30.],
[ 3., 12., 21., 30.],
...
[ 3., 12., 21., 30.]])
Run Code Online (Sandbox Code Playgroud)
如何矢量化此操作?
我在Python 2.7中有一个舍入问题导致意外输出.我试图得到p1和p2的总和达到0.6或更少的组合.
from itertools import product
P = []
p1 = [0.0,0.2,0.4,0.6]
p2 = [0.0,0.2,0.4,0.6]
for p11,p22 in product(p1,p2):
if p11+p22 <= max(p1):
P.append((p11,p22))
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,它不包括p11 + p22 = 0.6的所有值:
[(0.0, 0.0),
(0.0, 0.2),
(0.0, 0.4),
(0.0, 0.6),
(0.2, 0.0),
(0.2, 0.2),
(0.4, 0.0),
(0.6, 0.0)]
Run Code Online (Sandbox Code Playgroud)
我设置时它可以正常工作p11+p22 <= max(p1)+0.01.对于不同的p1和p2可能会或可能不会出现问题.我发现这种行为非常奇怪,导致非常不可靠的结果.
它可能与浮动精度问题有关.在我看来,这种行为不应该存在于Python中,因为R和Matlab也没有这种行为.这有什么简单的方法吗?