范围可用于切片Boost多维数组(multi_array).根据文档,有几种方法可以定义范围,但并非所有方法都可以编译.我在Ubuntu 11.04上使用GCC 4.5.2.
#include <boost/multi_array.hpp>
int main() {
typedef boost::multi_array_types::index_range range;
range a_range;
// indices i where 3 <= i
// Does compile
a_range = range().start(3);
// Does not compile
a_range = 3 <= range();
a_range = 2 < range();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器输出是:
ma.cpp: In function ‘int main()’:
ma.cpp:9:26: error: no match for ‘operator<=’ in ‘3 <= boost::detail::multi_array::index_range<long int, long unsigned int>()’
ma.cpp:10:25: error: no match for ‘operator<’ in ‘2 < boost::detail::multi_array::index_range<long int, long unsigned int>()’
Run Code Online (Sandbox Code Playgroud)
知道如何编译这个,或者缺少什么?
如何对以下双循环进行矢量化?
我有一个N乘A矩阵和一个N乘B矩阵,其中A和B可能不同,N比A和B小得多.我想按如下方式生成A×B矩阵,但理想情况下没有循环:
import numpy as np
def foo(arr):
# can be anything - just an example so that the code runs
return np.sum(arr)
num_a = 12
num_b = 8
num_dimensions = 3
a = np.random.rand(num_dimensions, num_a)
b = np.random.rand(num_dimensions, num_b)
# this is the loop I want to eliminate:
output = np.zeros( (num_a, num_b) )
for i in xrange(num_a):
for j in xrange(num_b):
output[i,j] = foo(a[:,i] - b[:,j])
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我使用最小二乘法将平面拟合到3D点集.我已经有了算法来做到这一点,但我想修改它以使用加权最小二乘法.意思是每个点都有一个重量(重量越大,平面越接近该点).
当前算法(无重量)如下所示:
计算总和:
for(Point3D p3d : pointCloud) {
pos = p3d.getPosition();
fSumX += pos[0];
fSumY += pos[1];
fSumZ += pos[2];
fSumXX += pos[0]*pos[0];
fSumXY += pos[0]*pos[1];
fSumXZ += pos[0]*pos[2];
fSumYY += pos[1]*pos[1];
fSumYZ += pos[1]*pos[2];
}
Run Code Online (Sandbox Code Playgroud)
而不是制作矩阵:
double[][] A = {
{fSumXX, fSumXY, fSumX},
{fSumXY, fSumYY, fSumY},
{fSumX, fSumY, pointCloud.size()}
};
double[][] B = {
{fSumXZ},
{fSumYZ},
{fSumZ}
};
Run Code Online (Sandbox Code Playgroud)
比解Ax = B,解的3个分量是拟合平面的系数...
那么,你能帮助我如何修改它来使用权重吗?谢谢!
我有一个结构化的numpy数组,并且按顺序对其进行排序。它工作正常,但仅在一个方向上!
降序:
sort(myStructuredArray,order=my_order)[::-1]
Run Code Online (Sandbox Code Playgroud)
和
上升:
sort(myStructuredArray,order=my_order)
Run Code Online (Sandbox Code Playgroud)
my_order的顺序是类似的[col1,col2,-col3,col4,-col5,...,colN],对于某些列,我想将其升序排序,例如col1,col2和colN,而另一些则降序col3和col5(减号)。在此示例中,我想先按col1升序,然后按col2升序,然后按col3降序,再按col4升序,再按col5降序,依此类推,对我的数组进行排序...我该怎么做?
谢谢
我无法弄清楚如何排列轴,以便我可以以矢量化方式执行以下操作.
基本上我有一个向量数组,一个矩阵数组,我想评估每个对应的向量V和矩阵M的VMV ^ T
import numpy as np
N = 5 # normally 100k or so
vecs = np.random.rand(N, 2)
mats = np.random.rand(N, 2, 2)
output = np.array([np.dot(np.dot(vecs[i, ...], mats[i, ...]), vecs[i, ...].T) for i in range(N)])
Run Code Online (Sandbox Code Playgroud)
如果它更简单,矢量化下面的中间结果也会有所帮助:
intermediate_result = np.array([np.dot(vecs[i, ...], mats[i, ...]) for i in range(N)])
# then I can do
output = np.sum(intermediate_result * vecs, axis=-1)
Run Code Online (Sandbox Code Playgroud) 我在高斯模糊中看到了 sigma 和半径之间的以下关系(来自http://en.wikipedia.org/wiki/Talk%3AGaussian_blur#Radius_again以及一些程序的实现,例如http://imagej .nih.gov/ij/source/ij/plugin/filter/GaussianBlur.java第 526 行)

这种关系从何而来?(我认为 255 与精度有关(255 = 2^8-1 => 8 位图像)
我有一个 Pandas 数据框,我试图用该组的平均值替换每个组中的值。在我的机器上,该行df["signal"].groupby(g).transform(np.mean)大约需要 10 秒才能运行N并N_TRANSITIONS设置为以下数字。
有没有更快的方法来达到相同的结果?
import pandas as pd
import numpy as np
from time import time
np.random.seed(0)
N = 120000
N_TRANSITIONS = 1400
# generate groups
transition_points = np.random.permutation(np.arange(N))[:N_TRANSITIONS]
transition_points.sort()
transitions = np.zeros((N,), dtype=np.bool)
transitions[transition_points] = True
g = transitions.cumsum()
df = pd.DataFrame({ "signal" : np.random.rand(N)})
# here is my bottleneck for large N
tic = time()
result = df["signal"].groupby(g).transform(np.mean)
toc = time()
print toc - tic
Run Code Online (Sandbox Code Playgroud) 我很难进入 numpy。我最终想要的是一个简单的由矩阵变换的向量的箭袋图。我已经阅读了很多次,只是将数组用于矩阵,这很公平。我有一个用于 x 和 y 坐标的网格
X,Y = np.meshgrid( np.arange(0,10,2),np.arange(0,10,1) )
a = np.array([[1,0],[0,1.1]])
Run Code Online (Sandbox Code Playgroud)
但即使在谷歌搜索并尝试了两个多小时之后,我也无法从a每个向量的矩阵乘法中得到结果向量。我知道 quiver 将分量长度作为输入,因此进入 quiver 函数的结果向量应该类似于np.dot(a, [X[i,j], Y[i,j]]) - X[i,j]x 分量,其中 i 和 j 在范围内迭代。
我当然可以在循环中对其进行编程,但是 numpy 有很多内置工具可以使这些矢量化的东西变得方便,我相信有更好的方法。
编辑:好的,这是循环版本。
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10,10))
n=10
X,Y = np.meshgrid( np.arange(-5,5),np.arange(-5,5) )
print("val test", X[5,3])
a = np.array([[0.5,0],[0,1.3]])
U = np.zeros((n,n))
V = np.zeros((n,n))
for i in range(10):
for j in range(10):
product = np.dot(a, [X[i,j], Y[i,j]]) #matrix with vector …Run Code Online (Sandbox Code Playgroud) 是否可以启动bpython解释器,以便它在启动时始终运行一些自定义命令?
就我而言,我只想做:
import numpy as np
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
我在文档中看不到任何内容.有人知道吗?
我正在尝试实现核密度估计.但是我的代码没有提供它应该的答案.它也是用朱莉娅写的,但代码应该是自我解释的.
这是算法:
哪里
因此算法测试是否x和观察X_I由一些常数因数(binwidth)加权之间的距离小于一个.如果是这样,它分配0.5 /(N*h)至该值,其中n =循环移位#of观测.
这是我的实现:
#Kernel density function.
#Purpose: estimate the probability density function (pdf)
#of given observations
#@param data: observations for which the pdf should be estimated
#@return: returns an array with the estimated densities
function kernelDensity(data)
|
| #Uniform kernel function.
| #@param x: Current x value
| #@param X_i: x value of observation i
| #@param width: binwidth
| #@return: Returns 1 if the absolute distance from
| #x(current) to x(observation) weighted by the binwidth …Run Code Online (Sandbox Code Playgroud)