numpy vectorize函数很有用,但是当函数参数是列表而不是标量时,它表现不佳.举个例子:
import numpy as np
def f(x, A):
print "type(A)=%s, A=%s"%(type(A),A)
return sum(A)/x
X = np.linspace(1,2,10)
P = [1,2,3]
f2 = np.vectorize(f)
f(X,P)
f2(X,P)
Run Code Online (Sandbox Code Playgroud)
得到:
type(A)=<type 'list'>, A=[1, 2, 3]
type(A)=<type 'numpy.int64'>, A=1
Traceback (most recent call last):
File "vectorize.py", line 14, in <module>
f2(X,P)
File "/usr/local/lib/python2.6/dist-packages/numpy/lib/function_base.py", line 1824, in __call__
theout = self.thefunc(*newargs)
File "vectorize.py", line 5, in f
return sum(A)/x
TypeError: 'numpy.int64' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我知道函数f 在没有vectorize它的情况下工作正常,但是我想知道如何(通常)向量化一个函数,该函数的参数采用列表而不是标量.
有关矢量化树操作的一般提示/指示是什么?内存布局明智,算法明智等
一些域特定的东西:
我需要在matlab中计算2个矩阵之间的欧氏距离.目前我正在使用bsxfun并计算距离如下(我附加了一段代码):
for i=1:4754
test_data=fea_test(i,:);
d=sqrt(sum(bsxfun(@minus, test_data, fea_train).^2, 2));
end
Run Code Online (Sandbox Code Playgroud)
fea_test的大小是4754x1024而fea_train是6800x1024,使用他的for循环导致for的执行花费大约12分钟,我认为太高了.有没有办法更快地计算两个矩阵之间的欧氏距离?
我被告知通过删除不必要的for循环,我可以减少执行时间.我也知道pdist2可以帮助减少计算时间,但由于我使用的是matlab版本7.我没有pdist2函数.升级不是一种选择.
任何帮助.
问候,
巴维亚
I'm implementing an ultra fast popcount on Intel Xeon® Phi®, as it's a performance hotspot of various bioinformatics software.
I've implemented five pieces of code,
#if defined(__MIC__)
#include <zmmintrin.h>
__attribute__((align(64))) static const uint32_t POPCOUNT_4bit[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
__attribute__((align(64))) static const uint32_t MASK_4bit[16] = {0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF};
inline uint64_t vpu_popcount1(uint64_t* buf, size_t n) { …Run Code Online (Sandbox Code Playgroud) 我有两个向量:
Start = c(1,10,20)
Finish = c(9,19,30)
Run Code Online (Sandbox Code Playgroud)
我希望这样的东西能起作用......
开始:完成
但当然不是.
我想生成如下列表:
[1] 1,2,3,4,5,6,7,8,9
[2] 10 11 12 13 14 15 16 17 18 19
[3] 20 21 22 23 24 25 26 27 28 29 30
Run Code Online (Sandbox Code Playgroud)
优选地以某种矢量化方式.Start矢量将始终大于相应元素的Finish矢量.
我正在尝试使用Haversine公式计算纬度和经度标识的一长串位置的距离矩阵,该公式采用两个坐标对元组来产生距离:
def haversine(point1, point2, miles=False):
""" Calculate the great-circle distance bewteen two points on the Earth surface.
:input: two 2-tuples, containing the latitude and longitude of each point
in decimal degrees.
Example: haversine((45.7597, 4.8422), (48.8567, 2.3508))
:output: Returns the distance bewteen the two points.
The default unit is kilometers. Miles can be returned
if the ``miles`` parameter is set to True.
"""
Run Code Online (Sandbox Code Playgroud)
我可以使用嵌套for循环计算所有点之间的距离,如下所示:
data.head()
id coordinates
0 1 (16.3457688674, 6.30354512503)
1 2 (12.494749307, 28.6263955635)
2 3 (27.794615136, 60.0324947881) …Run Code Online (Sandbox Code Playgroud) 我想了解什么是机器学习方面的向量.
我查看了以下2个链接:
https://en.wikipedia.org/wiki/Support_vector_machine https://en.wikipedia.org/wiki/Feature_vector.
我无法理解它.有人可以用简单的话来解释这个吗?
我有以下代码片段:
#include <cstdio>
#include <cstdint>
static const size_t ARR_SIZE = 129;
int main()
{
uint32_t value = 2570980487;
uint32_t arr[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
arr[x] = value;
float arr_dst[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
{
arr_dst[x] = static_cast<float>(arr[x]);
}
printf("%s\n", arr_dst[ARR_SIZE - 1] == arr_dst[ARR_SIZE - 2] ? "OK" : "WTF??!!");
printf("magic = %0.10f\n", arr_dst[ARR_SIZE - 2]);
printf("magic = %0.10f\n", arr_dst[ARR_SIZE - 1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我在MS Visual Studio 2015下编译它,我可以看到输出是:
WTF??!! …Run Code Online (Sandbox Code Playgroud) 我想用JavaScript编写需要大量数值计算的应用程序.但是,我对客户端JavaScript中有效的线性代数式计算的状态非常困惑.似乎有很多方法,但没有明确表明他们的准备情况.它们中的大多数似乎都限制了允许计算的向量和矩阵的大小.
显然允许GPU上的矢量和矩阵计算,但我不清楚其局限性.围绕此库尝试的 包装器似乎限制了矩阵和向量的大小.这是一个实际的限制(浏览器不支持其他任何东西)或只是开发限制(有人需要编写代码)?
WebCL是OpenCL的一个提议的浏览器级实现,但似乎停留在开发中.
Apple最近提出了一种名为WebGPU的 WebCL替代方案.到目前为止,有一个原型和演示,但我不清楚这是否会被广泛采用.
Mozilla已经推出了一个用于SIMD操作的API,但它只有实验支持.
JavaScript支持浏览器端的矢量化计算吗?
笔记:
我的问题不是"JavaScript中的数值计算有什么好的库",而是"JavaScrpt中的矢量化操作是否可行?" 可接受的答案将链接到在非实验性浏览器中工作的矢量化计算的演示.
我可能会对SIMD,矢量化和GPU计算感到困惑.我认为在这种情况下同义地使用它们是可以的,因为它们都允许通过使用专门的计算机硬件进行涉及高维向量的有效计算.