我正在寻找一个通用函数来沿任意数量的维度平铺或重复矩阵任意次数.Python和Matlab在NumPy的tile和Matlab的repmat函数中都有这些特性.Julia的repmat函数似乎只支持二维数组.
该函数应该看起来像repmatnd(a,(n1,n2,...,nk)).a是任意维度的数组.第二个参数是一个元组,指定每个维度k重复数组的次数.
知道如何在大于2维上平铺Julia数组吗?在Python中我会使用np.tile和matlab repmat,但Julia中的repmat函数只支持2维.
例如,
x = [1 2 3]
repmatnd(x, 3, 1, 3)
Run Code Online (Sandbox Code Playgroud)
会导致:
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Run Code Online (Sandbox Code Playgroud)
并为
x = [1 2 3; 1 2 3; 1 2 3]
repmatnd(x, (1, 1, 3))
Run Code Online (Sandbox Code Playgroud)
会导致与以前相同的事情.我想Julia开发人员会在标准库中实现这样的东西,但在那之前,修复它会很好.
我正在尝试创建我的课程计划的 pdf 导出,并且我对图表使用了离线绘图。在下面的 MWE 中,绘图将显示在 Jupyter Notebook 中,但在导出为 pdf 时不会显示。我使用File-->Download as-->PDF via Latex (.pdf).
我想制作 pdf 而不是使用 html。我知道将 html 导出转换为 pdf 可能需要额外的步骤,但我只是想知道是否有更直接的路由(代码修改?)可以让我直接通过File-->Download as-->PDF via Latex (.pdf)
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
data = [go.Scatter(
x=[1, 2, 3],
y=[3, 2, 1])
]
iplot(data)
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,我想尽可能多地使用 Cython 将其转换为 C。为此,我需要使用线性代数运算。这是我的功能。编辑:我学到的教训是尝试处理循环之外的线性代数,这在很大程度上是我能够做到的。否则,求助于包装 LAPACK/BLAS 或编写我自己的函数。
import numpy as np
from scipy.stats import multivariate_normal as mv
import itertools
def llf(data, rho, mu, sigma, A, V, n):
'''evaluate likelihood by guass-hermite quadrature
Parameters
----------
data : array
N x J matrix, columns are measurements
rho : array
length L vector of weights for mixture of normals
mu : array
L x K vector of means of mixture of normals
sigma : array
K x L*K matrix of variance matrices for …Run Code Online (Sandbox Code Playgroud) 当我选择用"AND"链接不同的条件时,选择工作正常.当我通过链接条件选择"OR"时,选择会引发错误.
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame([[1,4,3],[2,3,5],[4,5,6],[3,2,5]],
... columns=['a', 'b', 'c'])
>>> df
a b c
0 1 4 3
1 2 3 5
2 4 5 6
3 3 2 5
>>> df.loc[(df.a != 1) & (df.b < 5)]
a b c
1 2 3 5
3 3 2 5
>>> df.loc[(df.a != 1) or (df.b < 5)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/pandas/core/generic.py", …Run Code Online (Sandbox Code Playgroud) 我有一个数组,我想切换轴的顺序.它类似于转置,除了我想在尺寸大于2的数组上执行它.在Python中我会使用np.transpose和Matlab中的permute,但我似乎无法在Julia中找到它.例如,
a = ones(2, 3, 4)
size(a)
(2,3,4)
Run Code Online (Sandbox Code Playgroud)
从此我想通过将轴(尺寸)重新排列为(2,3,1)来得到一个形状(3,4,2)的数组.我正在寻找一个名为new_func的函数.
b = new_func(a, (2, 3, 1))
size(b)
(3,4,2)
Run Code Online (Sandbox Code Playgroud) 我在这里定义了计算矩阵行列式的函数.但有时我会得到错误的信号.我从这个答案中建模了我的功能.
from scipy.linalg.cython_lapack cimport dgetrf
cpdef double det_c(double[:, ::1] A, double[:, ::1] work, double[::1] ipiv):
'''obtain determinant of float type square matrix A
Notes
-----
As is, this function is not yet computing the sign of the determinant
correctly, help!
Parameters
----------
A : memoryview (numpy array)
n x n array to compute determinant of
work : memoryview (numpy array)
n x n array to use within function
ipiv : memoryview (numpy array)
length n vector use within …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个 Anki 牌组,例如,前面有一个单词。然后我在后面添加带有定义的单词以及图片。但是当已经有两个字段(前面的文本和后面的文本)时,我在包含图形时遇到了麻烦。这是一个注释示例:
\begin{note}
\begin{field}
\textbf{\large ruminate}
\end{field}
\begin{field}
\textbf{\large ruminate}
\begin{description}
\item[verb] \hfill \\
chew the cuds
\item[verb] \hfill \\
reflect deeply on a subject
\end{description}
\end{field}
\end{note}
Run Code Online (Sandbox Code Playgroud)
这篇笔记工作得很好,但是当我尝试包含图形(如下所示)时,Anki 不会让我在背面有两个字段。
\begin{note}
\begin{field}
\textbf{\large ruminate}
\end{field}
\begin{field}
\textbf{\large ruminate}
\begin{description}
\item[verb] \hfill \\
chew the cuds
\item[verb] \hfill \\
reflect deeply on a subject
\end{description}
\end{field}
\xplain(<img src="files/image.jpg" />)
\end{note}
Run Code Online (Sandbox Code Playgroud)
我尝试使用\includegraphics(Anki 禁止这样做,但我更改了源代码并使包可以工作),但在编译时找不到图像。
我正在切片pandas数据框,并且.loc与numpy和普通python切片相比,我似乎正在使用来获得意外切片。请参见下面的示例。
>>> import pandas as pd
>>> a = pd.DataFrame([[0,1,2],[3,4,5],[4,5,6],[9,10,11],[34,2,1]])
>>> a
0 1 2
0 0 1 2
1 3 4 5
2 4 5 6
3 9 10 11
4 34 2 1
>>> a.loc[1:3, :]
0 1 2
1 3 4 5
2 4 5 6
3 9 10 11
>>> a.values[1:3, :]
array([[3, 4, 5],
[4, 5, 6]])
Run Code Online (Sandbox Code Playgroud)
有趣的是,这仅发生于.loc,而不是.iloc。
>>> a.iloc[1:3, :]
0 1 2
1 3 4 5
2 …Run Code Online (Sandbox Code Playgroud)