dot-product

dot product in python [closed]

做~自己de王妃 提交于 2019-12-02 19:24:20
Does this Python code actually find the dot product of two vectors? import operator vector1 = (2,3,5) vector2 = (3,4,6) dotProduct = reduce( operator.add, map( operator.mul, vector1, vector2)) You can also use the numpy implementation of dot product which has large array optimizations in native code to make computations slightly faster. Even better unless you are specifically trying to write a dot product routine or avoid dependencies, using a tried tested widely used library is much better than rolling your own. Yes it does. Here is another way >>> sum(map( operator.mul, vector1, vector2)) 48

Cartesian product of two RDD in Spark

[亡魂溺海] 提交于 2019-12-02 17:48:57
问题 I am completely new to Apache Spark and I trying to Cartesian product two RDD. As an example I have A and B like : A = {(a1,v1),(a2,v2),...} B = {(b1,s1),(b2,s2),...} I need a new RDD like: C = {((a1,v1),(b1,s1)), ((a1,v1),(b2,s2)), ...} Any idea how I can do this? As simple as possible :) Thanks in advance PS: I finally did it like this as suggested by @Amit Kumar: cartesianProduct = A.cartesian(B) 回答1: That's not the dot product, that's the cartesian product. Use the cartesian method: def

Is there alternative for imread command to reduce delay in matlab program?

耗尽温柔 提交于 2019-12-02 09:28:58
I am having 2900 images in this path G:\newdatabase\ It is taking too much time to read images.For dot product also it is taking too much time. Questions: 1.Is there any alternative for imread command which increases performance? 2.Is there any alternative for dot command which increases performance? Source code i tried: srcFiles = dir('G:\newdatabase\*.jpg'); % the folder in which ur images exists for b = 1 : length(srcFiles) filename = strcat('G:\newdatabase\',srcFiles(b).name); Imgdata = imread(filename); Source code i tried: for i = 1:aa pare = dot(NormImage,u(:,i)); p = [p; pare]; end 来源:

Matlab: Argmax and dot product for each row in a matrix

元气小坏坏 提交于 2019-12-01 22:17:51
I have 2 matrices = X in R^(n*m) and W in R^(k*m) where k<<n . Let x_i be the i-th row of X and w_j be the j-th row of W. I need to find, for each x_i what is the j that maximizes <w_j,x_i> I can't see a way around iterating over all the rows in X, but it there a way to find the maximum dot product without iterating every time over all of W? A naive implementation would be: n = 100; m = 50; k = 10; X = rand(n,m); W = rand(k,m); Y = zeros(n, 1); for i = 1 : n max_ind = 1; max_val = dot(W(1,:), X(i,:)); for j = 2 : k cur_val = dot(W(j,:),X(i,:)); if cur_val > max_val max_val = cur_val; max_ind =

Numpy dot product very slow using ints

狂风中的少年 提交于 2019-11-30 11:41:29
sorry for so many questions. I am running Mac OSX 10.6 on Intel core 2 Duo. I am running some benchmarks for my research and I have run into another thing that baffles me. If I run python -mtimeit -s 'import numpy as np; a = np.random.randn(1e3,1e3)' 'np.dot(a,a)' I get the following output: 10 loops, best of 3: 142 msec per loop However, if I run python -mtimeit -s 'import numpy as np; a = np.random.randint(10,size=1e6).reshape(1e3,1e3)' 'np.dot(a,a)' I get the following output: 10 loops, best of 3: 7.57 sec per loop Then I ran python -mtimeit -s 'import numpy as np; a = np.random.randn(1e3

How to properly use prefetch instructions?

旧街凉风 提交于 2019-11-30 08:46:55
问题 I am trying to vectorize a loop, computing dot product of a large float vectors. I am computing it in parallel, utilizing the fact that CPU has large amount of XMM registers, like this: __m128* A, B; __m128 dot0, dot1, dot2, dot3 = _mm_set_ps1(0); for(size_t i=0; i<1048576;i+=4) { dot0 = _mm_add_ps( dot0, _mm_mul_ps( A[i+0], B[i+0]); dot1 = _mm_add_ps( dot1, _mm_mul_ps( A[i+1], B[i+1]); dot2 = _mm_add_ps( dot2, _mm_mul_ps( A[i+2], B[i+2]); dot3 = _mm_add_ps( dot3, _mm_mul_ps( A[i+3], B[i+3]);

Dot product of two vectors in tensorflow

妖精的绣舞 提交于 2019-11-29 16:09:20
问题 I was wondering if there is an easy way to calculate the dot product of two vectors (i.e. 1-d tensors) and return a scalar value in tensorflow. Given two vectors X=(x1,...,xn) and Y=(y1,...,yn), the dot product is dot(X,Y) = x1 * y1 + ... + xn * yn I know that it is possible to achieve this by first broadcasting the vectors X and Y to a 2-d tensor and then using tf.matmul. However, the result is a matrix, and I am after a scalar. Is there an operator like tf.matmul that is specific to vectors

How to properly use prefetch instructions?

蹲街弑〆低调 提交于 2019-11-29 11:21:50
I am trying to vectorize a loop, computing dot product of a large float vectors. I am computing it in parallel, utilizing the fact that CPU has large amount of XMM registers, like this: __m128* A, B; __m128 dot0, dot1, dot2, dot3 = _mm_set_ps1(0); for(size_t i=0; i<1048576;i+=4) { dot0 = _mm_add_ps( dot0, _mm_mul_ps( A[i+0], B[i+0]); dot1 = _mm_add_ps( dot1, _mm_mul_ps( A[i+1], B[i+1]); dot2 = _mm_add_ps( dot2, _mm_mul_ps( A[i+2], B[i+2]); dot3 = _mm_add_ps( dot3, _mm_mul_ps( A[i+3], B[i+3]); } ... // add dots, then shuffle/hadd result. I heard that using prefetch instructions could help

Cosine Similarity

自古美人都是妖i 提交于 2019-11-28 17:20:34
I calculated tf/idf values of two documents. The following are the tf/idf values: 1.txt 0.0 0.5 2.txt 0.0 0.5 The documents are like: 1.txt = > dog cat 2.txt = > cat elephant How can I use these values to calculate cosine similarity? I know that I should calculate the dot product, then find distance and divide dot product by it. How can I calculate this using my values? One more question: Is it important that both documents should have same number of words? a * b sim(a,b) =-------- |a|*|b| a*b is dot product some details: def dot(a,b): n = length(a) sum = 0 for i in xrange(n): sum += a[i] * b

What is the pythonic way to calculate dot product?

纵饮孤独 提交于 2019-11-28 04:57:24
I have two lists, one is named as A, another is named as B. Each element in A is a triple, and each element in B is just an number. I would like to calculate the result defined as : result = A[0][0] * B[0] + A[1][0] * B[1] + ... + A[n-1][0] * B[n-1] I know the logic is easy but how to write in pythonic way? Thanks! import numpy result = numpy.dot( numpy.array(A)[:,0], B) http://docs.scipy.org/doc/numpy/reference/ If you want to do it without numpy, try sum( [a[i][0]*b[i] for i in range(len(b))] ) Henri Andre Python 3.5 has an explicit operator @ for the dot product, so you can write a = A @ B