dot-product

Numpy dot product very slow using ints

China☆狼群 提交于 2019-12-18 12:57:17
问题 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,

Cosine Similarity

試著忘記壹切 提交于 2019-12-17 21:57:29
问题 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? 回答1: a * b sim(a,b) =-------- |a|*

Retaining dot product on GPGPU using CUBLAS routine

时光总嘲笑我的痴心妄想 提交于 2019-12-17 20:53:57
问题 I am writing a code to compute dot product of two vectors using CUBLAS routine of dot product but it returns the value in host memory. I want to use the dot product for further computation on GPGPU only. How can I make the value reside on GPGPU only and use it for further computations without making an explicit copy from CPU to GPGPU? 回答1: You can't, exactly, using CUBLAS. As per talonmies' answer, starting with the CUBLAS V2 api (CUDA 4.0) the return value can be a device pointer. Refer to

Vectorized way of calculating row-wise dot product two matrices with Scipy

喜夏-厌秋 提交于 2019-12-17 07:26:14
问题 I want to calculate the row-wise dot product of two matrices of the same dimension as fast as possible. This is the way I am doing it: import numpy as np a = np.array([[1,2,3], [3,4,5]]) b = np.array([[1,2,3], [1,2,3]]) result = np.array([]) for row1, row2 in a, b: result = np.append(result, np.dot(row1, row2)) print result and of course the output is: [ 26. 14.] 回答1: Check out numpy.einsum for another method: In [52]: a Out[52]: array([[1, 2, 3], [3, 4, 5]]) In [53]: b Out[53]: array([[1, 2,

Is dot product and normal multiplication results of 2 numpy arrays same?

点点圈 提交于 2019-12-13 08:04:17
问题 I am working with kernel PCA in Python and I have to find the values after projecting the original data to the principal components.I use the equation fv = eigvecs[:,:ncomp] print(len(fv)) td = fv.T * K.T where K is the kernel matrix of dimension (150x150),ncomp is the number of principal components.The code works perfectly fine when fv has dimension (150x150).But when I select ncomp as 3 making fv to be of (150x3) as dimension,there occurs error stating operands could not be broadcast

Using Python numpy einsum to obtain dot product between 2 Matrices

核能气质少年 提交于 2019-12-12 04:13:52
问题 Just came across this: Vectorized way of calculating row-wise dot product two matrices with Scipy This numpy.einsum is really awesome but its a little confusing to use. Suppose I have: import numpy as np a = np.array([[1,2,3], [3,4,5]]) b = np.array([[0,1,2], [1,1,7]]) How would i use the "ij" in einsum to get a "cross dot product" between a and b? Using the example basically I would like to compute dot product of [1,2,3] and [0,1,2] [1,2,3] and [1,2,7] [3,4,5] and [0,1,2] [3,4,5] and [1,1,7]

Find if 2 triangles are perpendicular in 3D space

蹲街弑〆低调 提交于 2019-12-11 21:52:48
问题 I have 2 triangles in 3D space made of 3 points. I assume I need to use the dot product but how do I arrange the matrix? I think I have the pieces but need help to arrange it :) Thank you. Current code included below, not convinced it is correct. vx1 = self.vertices[f[1]].x-self.vertices[f[0]].x vy1 = self.vertices[f[1]].y-self.vertices[f[0]].y vz1 = self.vertices[f[1]].z-self.vertices[f[0]].z vx2 = self.vertices[f[2]].x-self.vertices[f[0]].x vy2 = self.vertices[f[2]].y-self.vertices[f[0]].y

dot product of multiple vectors in R to optimize pokemon teams

核能气质少年 提交于 2019-12-11 06:37:49
问题 My plan is to create a way to pick the best pokemon team. Im not sure how to create a list of all possible combinations of 12 vectors from the 16 defense vectors with the dot product of the 12 vectors and then do the same thing for the atk vectors. My other problem is finding a way to sum the Total value for the pokemon in each team. I want my results to look something like this matrix: Team............TotalStats..............Atk Score...............................................Def Score

Check if a Point is between two Points

妖精的绣舞 提交于 2019-12-11 04:55:39
问题 I just recognized my math is a bit rusty.. I wanna check if Point C is between Point A and Point B . C can be on the line segment of A and B, or not. There can be three cases and I have to identify all of them: C is between A and B C / \ A---B C is in front of A and B C \ \ A--B C is in the back of A and B C / / A--B The "sketch" in the last two points should be a triangle. I used the dotproduct to check if C is between A and B. if (VectorOf(AB) * VectorOf(BC)) >= 0) To check if C is in the

Prolog: Multiplying 2 lists with 1 of them not instantiated?

妖精的绣舞 提交于 2019-12-11 02:16:09
问题 I am trying to write a rule that can return the sum of the product of each element from two lists (same length). Here is what I have right now: sum(0, _, []). sum(Result, [H1|T1], [H2|T2]) :- sum(Remaining,T1, T2), Remaining is Result - (H1*H2). It won't work when one of the list is not instantiated. What changes I need to make in order to make the following possible? sum([1,2],X,3). X = [3,0]. Thanks. 回答1: What you are calculating is commonly referred to as a dot product (also known as