小编Est*_*oza的帖子

如何优化使用Numpy的连续值的for循环?

我试图创建一个返回的功能numpy.arrayn0和1所使用的方法的细节之间的伪随机均匀分布的数字可以在这里找到:https://en.wikipedia.org/wiki/Linear_congruential_generator

到目前为止,效果很好。唯一的问题是,每个新值都是通过使用先前的值来计算的,因此,到目前为止,我发现的唯一解决方案是使用循环,为了提高效率,我尝试摆脱循环,可能是通过向量化操作-但是,我不知道该怎么做。

您对如何优化此功能有任何建议?

import numpy as np
import time

def unif(n):
    m = 2**32
    a = 1664525
    c = 1013904223

    result = np.empty(n)
    result[0] = int((time.time() * 1e7) % m)

    for i in range(1,n):
        result[i] = (a*result[i-1]+c) % m

    return result / m
Run Code Online (Sandbox Code Playgroud)

python numpy vectorization

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

LaTeX 中 tikzDevice 和 ggplot2 生成的标题和图形之间的空白过多

我目前正在使用 Rggplot2tikzDevice包来生成图形并在文档中介绍它们LaTeX,但我正在努力解决图形和标题之间产生的大空白,正如您在比较图像时看到的那样(我已手动突出显示空格以使其更清晰):

ggplot2 绘图 tikz图

这是我的 MWE:

R代码:

library(ggplot2)
library(tikzDevice)

set.seed(1)
x <- rnorm(200)

tikz(file = "Rplots.tex", width = 4, height = 4)
qplot(x, geom = "histogram")
dev.off()
Run Code Online (Sandbox Code Playgroud)

和乳胶代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\include{Rplots}
\caption{\texttt{ggplot2} plot.}
\end{figure}

\begin{figure}
\centering
\begin{tikzpicture}[scale=3]
    \clip (-0.1,-0.2)
    rectangle (1.8,1.2);
    \draw[step=.25cm,gray,very thin]
    (-1.4,-1.4) grid (3.4,3.4);
    \draw (-1.5,0) -- (2.5,0);
    \draw (0,-1.5) -- (0,1.5);
    \draw (0,0) circle (1cm);
    \filldraw[fill=green!20!white,
    draw=green!50!black]
    (0,0) -- (3mm,0mm)
    arc (0:30:3mm) -- cycle;
\end{tikzpicture}
\caption{\texttt{tikz} plot.}
\end{figure}

\end{document}
Run Code Online (Sandbox Code Playgroud)

我想知道如何 …

latex r tikz ggplot2

3
推荐指数
1
解决办法
9223
查看次数

标签 统计

ggplot2 ×1

latex ×1

numpy ×1

python ×1

r ×1

tikz ×1

vectorization ×1