在这个网站上有很多关于在cython中使用numpy的问题,一个特别有用的问题是使用cython 简单包装C代码.
但是,cython/numpy接口api 似乎有所改变,特别是在确保内存连续数组的传递方面.
在cython中编写包装函数的最佳方法是:
double* data_in, double* data_outdouble*该方法写入的numpy数组?我的尝试如下:
cimport numpy as np
import numpy as np # as suggested by jorgeca
cdef extern from "myclass.h":
cdef cppclass MyClass:
MyClass() except +
void run(double* X, int N, int D, double* Y)
def run(np.ndarray[np.double_t, ndim=2] X):
cdef int N, D
N = X.shape[0]
D = X.shape[1]
cdef np.ndarray[np.double_t, ndim=1, mode="c"] X_c
X_c = np.ascontiguousarray(X, dtype=np.double)
cdef np.ndarray[np.double_t, ndim=1, mode="c"] Y_c
Y_c = np.ascontiguousarray(np.zeros((N*D,)), …Run Code Online (Sandbox Code Playgroud) 什么是npm相当于:
pip freeze > requirements.txt
pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud) 在下面的图中,我有一个刻面网格.
有没有办法将两个子图集中在一起0,同时保持轴的不同min/ max值x?
在下面的情况下,这将是xlim=c(-1,1)左和xlim=c(-2,2)右,但它应该是普遍适用的.
(在现实生活中的例子中,那些是刻面的火山地块,我想以0效果大小居中,但保持x不同地块的不同尺度)
library(ggplot2)
df = data.frame(x=c(1,2), y=c(0,0), group=c(1,2))
ggplot(df, aes(x=x, y=y)) + geom_point() + facet_wrap(~group, scale="free_x")
Run Code Online (Sandbox Code Playgroud)
首先:我查看了相关问题,但遗憾的是它们并没有很大帮助.我正在尝试从外部库中包装枚举和类.
#include <Python.h>
#include <boost/python.hpp>
using namespace boost::python;
#include <libvpsc/rectangle.h>
using vpsc::Rectangle;
using vpsc::Dim;
BOOST_PYTHON_MODULE(adaptagrams)
{
enum_<Dim>("dim")
.value("x", vpsc::XDIM)
.value("y", vpsc::YDIM)
.value("unset", vpsc::UNSET)
;
class_<Rectangle>("Rectangle",
init<double, double, double, double, optional<bool> >())
.add_property("centerX", &Rectangle::getCentreX)
.add_property("centerY", &Rectangle::getCentreY)
.add_property("width", &Rectangle::width, &Rectangle::set_width)
.add_property("height", &Rectangle::height, &Rectangle::set_height)
;
}
Run Code Online (Sandbox Code Playgroud)
并编译:
g++ -fPIC -I/usr/include/python2.7 -c adaptagrams.cpp -o adaptagrams.o
g++ -shared -Wl,-soname,adaptagrams.so -o adaptagrams.so adaptagrams.o -lpython2.7 -lboost_python -lvpsc
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试导入.so模块时,出现错误:
ImportError: dynamic module does not define init function (PyInit_adaptagrams)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
更新:当我重新启动Python并尝试导入时,我得到的第一个错误是:
ImportError: ./adaptagrams.so: undefined symbol: _ZN8topology13computeStressERKSt6vectorIPNS_4EdgeESaIS2_EE
Run Code Online (Sandbox Code Playgroud)
当我再次尝试时,第二个是从上面动态导入(2.7)和段错误(3.2).Boost是针对2.7和3.2进行编译的,我在每种方法上都链接正确的.
更新2: …
在R逐步前向回归中,我指定了一个最小模型和一组要添加(或不添加)的变量:
min.model = lm(y ~ 1)
fwd.model = step(min.model, direction='forward', scope=(~ x1 + x2 + x3 + ...))
Run Code Online (Sandbox Code Playgroud)
有没有办法指定使用矩阵/ data.frame中的所有变量,所以我不必枚举它们?
举例来说明我想做什么,但它们不起作用:
# 1
fwd.model = step(min.model, direction='forward', scope=(~ ., data=my.data.frame))
# 2
min.model = lm(y ~ 1, data=my.data.frame)
fwd.model = step(min.model, direction='forward', scope=(~ .))
Run Code Online (Sandbox Code Playgroud) CrossValidated上有一个关于如何使用PyMC将两个Normal分布拟合到数据的问题.Cam.Davidson.Pilon的答案是使用伯努利分布将数据分配给两个法线之一:
size = 10
p = Uniform( "p", 0 , 1) #this is the fraction that come from mean1 vs mean2
ber = Bernoulli( "ber", p = p, size = size) # produces 1 with proportion p.
precision = Gamma('precision', alpha=0.1, beta=0.1)
mean1 = Normal( "mean1", 0, 0.001 )
mean2 = Normal( "mean2", 0, 0.001 )
@deterministic
def mean( ber = ber, mean1 = mean1, mean2 = mean2):
return ber*mean1 + (1-ber)*mean2
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:如何用三个法线做到这一点? …
我在Python中有两个字符串,
A m * B s / (A m + C m)
Run Code Online (Sandbox Code Playgroud)
和
C m * B s / (C m + A m)
Run Code Online (Sandbox Code Playgroud)
它们都是无序集(A,C)和无序集(B)的等价函数.m和s表示可以在相同但不与另一个单元交换的单位.
到目前为止,我正在进行A,B和C的排列,并使用eval和SymPy的==运算符对它们进行测试.这有许多缺点:
有没有pythonian方法来测试这种等价?它应该是一个任意的表达式.
我试图写一个自定义stat_*的ggplot2,在这里我想用瓷砖颜色的2D黄土表面。当我从扩展指南开始时,我可以像他们一样编写stat_chull:
stat_chull = function(mapping = NULL, data = NULL, geom = "polygon",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...) {
chull = ggproto("chull", Stat,
compute_group = function(data, scales) {
data[chull(data$x, data$y), , drop = FALSE]
},
required_aes = c("x", "y")
)
layer(
stat = chull, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, …Run Code Online (Sandbox Code Playgroud) 我想将MathML表达式转换为python中的方程字符串,MathDOM模块应该适用于该字符串.
一个例子是:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<lambda>
<bvar><ci>A</ci></bvar>
<bvar><ci>B</ci></bvar>
<apply>
<plus/>
<ci>A</ci>
<ci>B</ci>
</apply>
</lambda>
</math>
Run Code Online (Sandbox Code Playgroud)
应映射到"A + B".这显然适用于更复杂的表达式.
但是,它已经很老了,并且不能正常使用新版本的xml模块(试图包含错误的模块结构等)
有谁知道有用的替代品?
考虑以下图形结构(借用于此问题):
G = networkx.DiGraph()
G.add_edges_from([('n', 'n1'), ('n', 'n2'), ('n', 'n3')])
G.add_edges_from([('n4', 'n41'), ('n1', 'n11'), ('n1', 'n12'), ('n1', 'n13')])
G.add_edges_from([('n2', 'n21'), ('n2', 'n22')])
G.add_edges_from([('n13', 'n131'), ('n22', 'n221')])
Run Code Online (Sandbox Code Playgroud)
产生:
n---->n1--->n11
| |--->n12
| |--->n13
| |--->n131
|--->n2
| |---->n21
| |---->n22
| |--->n221
|--->n3
Run Code Online (Sandbox Code Playgroud)
我可以从节点开始对后继者执行深度优先搜索,n并得到:
> dfs_successors(G, 'n')
{'n': ['n1', 'n2', 'n3'],
'n1': ['n12', 'n13', 'n11'],
'n13': ['n131'],
'n131': ['n221'],
'n2': ['n22', 'n21']}
Run Code Online (Sandbox Code Playgroud)
但是,当我在例如节点处对前辈进行深度优先搜索时n221,什么也没有发生:
> dfs_predecessors(G, 'n221')
{}
Run Code Online (Sandbox Code Playgroud)
我希望输出是:
{'n221': ['n22', 'n2', 'n']}
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题,我怎样才能得到我预期的行为?