distance

Optimized method for calculating cosine distance in Python

主宰稳场 提交于 2019-12-12 07:56:24
问题 I wrote a method to calculate the cosine distance between two arrays: def cosine_distance(a, b): if len(a) != len(b): return False numerator = 0 denoma = 0 denomb = 0 for i in range(len(a)): numerator += a[i]*b[i] denoma += abs(a[i])**2 denomb += abs(b[i])**2 result = 1 - numerator / (sqrt(denoma)*sqrt(denomb)) return result Running it can be very slow on a large array. Is there an optimized version of this method that would run faster? Update: I've tried all the suggestions to date,

Find color names for colors close to colorBrewer palette

三世轮回 提交于 2019-12-12 07:51:19
问题 I want to use the R package SNA to do social network analysis. SNA colors elements only using R color names (text names). I'd like to find near matches from a ColorBrewer palette (set3) to the color names in R. There aren't many exact matches in the RGB space. require(RColorBrewer) brew10 <- brewer.pal(10, "Set3") rcol <- colors() brew10rgb <- col2rgb(brew10) allrgb <- col2rgb(rcol) apply(t(brew10rgb), 1, paste, collapse="$$") %in% apply(t(allrgb), 1, paste,collapse="$$") brew10rgb[,1] fltr <

Find UK PostCodes closest to other UK Post Codes by matching the Post Code String

牧云@^-^@ 提交于 2019-12-12 06:37:04
问题 Here is a question that has me awake for a number of days now. The only conclusion I came up so far is that Red Bull does not usually help coders. I have a scenario in my application where I have a couple of jobs (1 to 50). The job has an address and I have the following properties of an address: Postcode, Latitude, and Longitude. I have a table of workers also and they too have addresses. While the jobs or workers are created through screens, I use Google Map queries to make sure the

Search distance between 2 points CakePHP [closed]

不羁的心 提交于 2019-12-12 04:37:40
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Im trying to build a small CakePHP website which has some lat and longitudes in the DB. A user performs a search query to locations (with giving her/his address) and the results needs to be ordered by distance

looking for fast way to compute pair wise distances of many strings

心已入冬 提交于 2019-12-12 04:24:57
问题 I have a list of ~1 million unique 16-character strings (an array called VEC) and I want to calculate the minimum pair-wise hamming distance for each one in Python (an array called RES). Basically, I'm calculating the full pair-wise distance matrix one row at a time but only storing the minimum value in RES for each row. VEC= ['AAAAAAAAAAAAAAAA','AAAAAAAAAAAAAAAT','AAAAGAAAAAATAAAA'...] so that dist(VEC[1],VEC[2])=1, dist(VEC[1],VEC[3])=2 etc... and RES[1]=1. Using tips and tricks from these

haversine formula php / mysql

我们两清 提交于 2019-12-12 03:44:37
问题 I'm trying to get a common database of geo points working with a radius search. I've found several good tutorials on this topic, but I'm failing at the very end. The main tutorial is here: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates. The basic formula, in the form of an SQL query, is SELECT * FROM Places WHERE (Lat => 1.2393 AND Lat <= 1.5532) AND (Lon >= -1.8184 AND Lon <= 0.4221) AND acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981))) <= 0.1570; I've

find location between two other locations

社会主义新天地 提交于 2019-12-12 03:33:20
问题 I am trying trying to find a new location(x,y,z) between two already existing locations (x,y,z). e.g. lets say the distance between locA and locB is 2500. locNew should always be the location with distance 300 and should be on the line of locA and locB. I have no issues finding the midpoint between locA and locB, but I keep banging my head trying to find locNew for this specific situation. I tried this, but it returns a point that is not on the line from locA to locB: locA = {x = 400, y = 400

How to extend polygon by a certain distance in PHP/Mysql?

℡╲_俬逩灬. 提交于 2019-12-12 03:30:02
问题 I have a location defined by a polygon (e.g.(-8.87 41.86,-8.74 41.98,-8.63 42.06,-8.16 42.13 ...) ) and want to search for places from db within the polygon extended by a distance. Google maps algorithm: http://www.geocodezip.com/v3_map-markers_ConvexHull_extend.asp Any idea how to get extended polygon in PHP/MySQL in order to search for records from MySQL? I use ST_CONTAINS mysql function to search for records within an exact polygon. 来源: https://stackoverflow.com/questions/35848073/how-to

Improving distance calculation

南楼画角 提交于 2019-12-12 03:27:53
问题 I have build my own distance (let's call it d1 ). Now, I have a matrix for which I need to compute the distance. Considering x as the matrix with the content for each sample, the code written to get the distance matrix is the following: # Build the matrix wDM <- matrix(0, nrow=nrow(x), ncol=nrow(x)) # Fill the matrix for (i in 1:(nrow(wDM)-1)){ for (j in (i+1):nrow(wDM)){ wDM[i,j] <- wDM[j,i] <- d1(x[i,], x[j,]) } } I have to implement this process several times. So, I was wondering if there

Distance for each intersected points of a line in increased order in 2D coordinate

核能气质少年 提交于 2019-12-12 00:43:22
问题 I am calculating the distance between A(x0,y0) and B(x1,y1) points in a 2D (x,y). I have the following code which represents intersected points of each cell in the given two boundary points, A(x0,y0) and B(x1,y1). def intersected_points(x0, x1, y0, y1): # slope m = (y1 - y0 )/( x1 - x0) # Boundary of the selected points x_ceil = ceil( min (x0, x1 )) x_floor = floor( max(x0, x1)) y_ceil = ceil( min(y0, y1)) y_floor = floor( max(y0, y1)) # calculate all intersected x coordinate ax = [] for x in