Skip to content

Commit

Permalink
switch to native stats functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Nov 24, 2024
1 parent 5b502eb commit 793b9e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
25 changes: 5 additions & 20 deletions oldnyc/geocode/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import csv
import re
import statistics
import sys
from collections import Counter, defaultdict
from dataclasses import dataclass
from typing import Sequence

import numpy as np
from pygeojson import Optional
from word2number import w2n

Expand Down Expand Up @@ -125,17 +124,6 @@ def ave_to_num(ave: str):
return int(ave)


# TODO: use statistics.correlation instead
def correl(xs_list: Sequence[float | int], ys_list: Sequence[float | int]):
xs = np.array(xs_list, dtype=float)
ys = np.array(ys_list, dtype=float)
meanx = xs.mean()
meany = ys.mean()
stdx = xs.std()
stdy = ys.std()
return ((xs * ys).mean() - meanx * meany) / (stdx * stdy)


def extract_lat_lons(num_to_lls):
"""Returns (xs, lats, lons) as parallel lists."""
lats = sorted([(ave_to_num(x), num_to_lls[x][0]) for x in num_to_lls.keys()])
Expand All @@ -152,8 +140,8 @@ def correl_lat_lons(num_to_lls):
Given a dict mapping street/ave # --> (lat, lon), returns min(r^2).
"""
xs, lats, lons = extract_lat_lons(num_to_lls)
r_lat = correl(xs, lats)
r_lon = correl(xs, lons)
r_lat = statistics.correlation(xs, lats)
r_lon = statistics.correlation(xs, lons)
return min(r_lat * r_lat, r_lon * r_lon)


Expand All @@ -163,11 +151,8 @@ def get_line(num_to_lls):
Returns (b, a), i.e. (intercept, slope)
"""
ns, lats, lons = extract_lat_lons(num_to_lls)
xs = np.zeros((len(lons), 2))
xs[:, 0] = 1
xs[:, 1] = lons
ys = np.array(lats)
return np.linalg.lstsq(xs, ys)[0]
slope, intercept = statistics.linear_regression(lons, lats)
return intercept, slope


def may_extrapolate(avenue: str, street: str):
Expand Down
2 changes: 1 addition & 1 deletion oldnyc/geocode/locatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

def round_pt(pt: Point) -> Point:
lat, lng = pt
return round(float(lat), 7), round(float(lng), 7) # they may be numpy floats
return round(lat, 7), round(lng, 7)


def locate_with_osm(
Expand Down

0 comments on commit 793b9e6

Please sign in to comment.