Skip to content

Instantly share code, notes, and snippets.

@yitznewton
Last active March 23, 2018 15:54
Show Gist options
  • Save yitznewton/55c6b624720d09c7117e543f4cb498a4 to your computer and use it in GitHub Desktop.
Save yitznewton/55c6b624720d09c7117e543f4cb498a4 to your computer and use it in GitHub Desktop.
Find pay centers
require 'active_support'
require 'active_support/core_ext'
require 'kmeans-clusterer'
def median(array)
sorted = array.sort
len = sorted.length
(sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
end
def currency(cents)
"$#{(cents/100).round(2)}"
end
def rate(duration:, pay:)
(pay*100) / (duration.to_f / 60 / 60)
end
data = %(
369 2.383118948 3/20/2018 4:30:30 Secaucus Junction Terminal, Customer Pick-Up/Drop Off, Secaucus Junction 701 Penhorn Ave, Secaucus, NJ 07094, USA 40.7616511 -74.0747941 7.42
673 3.239628683 3/20/2018 4:43:42 Secaucus Junction Terminal, Customer Pick-Up/Drop Off, Secaucus Junction 241 Webster Ave, Jersey City, NJ 07307, USA 40.7616511 -74.0747941 10.33
1184 11.81139729 3/20/2018 4:59:14 350 Webster Ave, Jersey City, NJ 07307, USA 3 Brewster Rd, Newark, NJ 07114, US 40.7503546 -74.0420997 22.50
275 1.747229291 3/20/2018 5:37:29 2834-2838 Kathleen Terrace, Union, NJ 07083, USA 1075 Lousons Rd, Union, NJ 07083, USA 40.7121856 -74.2938312 3.71
462 2.81901845 3/20/2018 21:07:59 380 State Rt 3, Clifton, NJ 07014, US 593 Main Ave, Passaic, NJ 07055, USA 40.8303149 -74.1376647 3.71
341 1.297403495 3/20/2018 21:21:01 28 Broadway, Passaic, NJ 07055, US 57 Highland Ave, Passaic, NJ 07055, USA 40.8604571 -74.1274598 3.71
1408 14.8826663 3/19/2018 4:20:25 122 Mortimer Ave, Rutherford, NJ 07070, USA 3 Brewster Rd, Newark, NJ 07114, USA $2.15 $2.15 40.831165 -74.107172 13.99
1196 13.99936251 3/19/2018 5:02:19 2424 Richlieu Pl, Scotch Plains, NJ 07076, US Terminal C, Newark, NJ 07114, USA 40.6633443 -74.3880012 12.79
287 0.9289578727 3/19/2018 5:39:18 534 Chilton St, Elizabeth, NJ 07208, EE. UU. 260 N Broad St, Elizabeth, NJ 07208, EE. UU. 40.6704441 -74.2295863 6.82
1314 15.24713019 3/19/2018 9:18:01 117 Woodland Ave, Rutherford, NJ 07070, USA 3 Brewster Rd, Newark, NJ 07114, USA $2.15 $2.15 40.8199013 -74.1124112 13.88
1616 19.77261877 3/19/2018 11:59:31 1200 Wall St W, Lyndhurst, NJ 07071, USA 4 Distler Ave, West Caldwell, NJ 07006, USA 40.8048356 -74.1027987 17.80
622 6.507989301 3/18/2018 4:22:35 182 W 4th St, Clifton, NJ 07011, USA 142 Entin Rd, Clifton, NJ 07014, USA 40.8865031 -74.1562232 6.49
164 1.010154813 3/18/2018 4:53:27 97 Merrill Rd, Clifton, NJ 07012, US 8 Lowry Ct, Clifton, NJ 07012, US 40.8491749 -74.1632758 4.82
518 4.274475734 3/18/2018 5:07:37 1646-1648 Main Ave, Clifton, NJ 07011, USA 265 Adriana St, Saddle Brook, NJ 07663, USA 40.8898639 -74.1539588 4.74
590 5.993422444 3/18/2018 5:24:52 23 MacArthur Ave, Garfield, NJ 07026, US 4-05 Cyril Ave, Fair Lawn, NJ 07410, USA 40.868318 -74.102603 7.89
895 10.06484202 3/18/2018 5:46:15 125 Cambridge Ave, Saddle Brook, NJ 07663, USA 64 Clove Rd, Little Falls, NJ 07424, US $0.50 $0.50 40.9122951 -74.099472 16.13
1270 18.50374831 3/18/2018 6:02:40 86-90 Huemmer Terrace, Clifton, NJ 07013, USA 3 Brewster Rd, Newark, NJ 07114, US $1.50 $1.50 40.8586534 -74.1791115 19.30
1015 13.04291143 3/18/2018 6:35:20 Terminal C, Arrivals, Pick-Up 3, Newark Liberty International Airport 1601 Union Turnpike, North Bergen, NJ 07047, USA $2.15 $2.15 40.6895314 -74.1744624 14.79
601 5.499937732 3/17/2018 21:26:31 405 NJ-3, Clifton, NJ 07014, USA Bohn Hall, Stone St, Montclair, NJ 07043, USA 40.8290154 -74.1403282 5.74
517 4.352585614 3/17/2018 21:35:10 45 Clove Rd, Little Falls, NJ 07424, USA 375 Union Blvd, Totowa, NJ 07512, US 40.8701867 -74.1985703 6.50
457 3.690988499 3/17/2018 21:55:50 22 Mitchell Ave, Totowa, NJ 07512, USA 95 Mountainview Blvd, Wayne, NJ 07470, US 40.9041322 -74.2127279 4.64
803 6.655544672 3/17/2018 22:18:52 235 US-46, Totowa, NJ 07512, USA 103 Warbler Dr, Wayne, NJ 07470, USA 40.8944101 -74.228498 10.40
1372 12.02639874 3/17/2018 22:33:02 1176 Hamburg Turnpike, Wayne, NJ 07470, USA 380 Monroe St, Passaic, NJ 07055, USA 40.9617978 -74.2427847 11.74
1151 14.6385587 3/16/2018 4:17:42 233 Van Buren St, Lyndhurst, NJ 07071, USA 3 Brewster Rd, Newark, NJ 07114, US $2.15 40.807284 -74.111276 33.71
1362 17.02954902 3/16/2018 4:57:18 45 Garfield Ave, Madison, NJ 07940, US 3 Brewster Rd, Newark, NJ 07114, US 40.7466963 -74.425289 21.22
409 2.347251995 3/16/2018 5:40:09 1436 Vauxhall Rd, Union, NJ 07083, USA 27 Westfield Ave, Elizabeth, NJ 07208, EE. UU. 40.6928156 -74.2444124 4.45
998 4.637190289 3/16/2018 5:53:51 253 Morris Ave, Elizabeth, NJ 07208, EE. UU. 801 W Linden AveLinden, Nueva Jersey 40.6679038 -74.216041 7.23
1037 9.391018809 3/16/2018 12:11:38 364 New York Ave, Lyndhurst, NJ 07071, USA 50 Bortic Rd, Cedar Grove, NJ 07009, USA 40.8148294 -74.1221379 9.28
988 10.01406596 3/16/2018 12:34:52 146 1st Ave, Little Falls, NJ 07424, USA E 25th St & 5th Ave, Paterson, New Jersey 07514, US 40.8765017 -74.2270347 9.62
).split("\n").select(&:present?).map { |line| line.split("\t") }.map { |fields| [[fields[7].to_f, fields[8].to_f], rate(duration: fields[0].to_i, pay: fields[9].to_f)] }
cluster_count = 7
kmeans = KMeansClusterer.run(cluster_count, data.map(&:first), labels: data.map(&:last))
puts 'median_rate,coordinate'
kmeans.clusters.each { |c| puts "#{currency(median(c.points.map { |p| p.label }))},\"#{c.centroid.data[0]},#{c.centroid.data[1]}\"" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment