Beispiel #1
0
def recommend(
    datestrs,
    name='departure_tree',
    verbose=False,
):
    """
    Parameters
    ---------
    datestrs: list of strings
        Datestrings of the format YYYY-MM-DD
    names: string
        The stems fo the filename where the model is store.
    verbose: boolean

    Returns
    -------
    recommendations: dict of int
        Dictionary keys are datestrings and values are departure times
    """
    model_name = name + '.pickle'
    try:
        # Try to load a saved tree
        tree = tools.restore(model_name)
    except Exception:
        # If unsuccessful, create a new one
        tree = create_tree(verbose=verbose)
        tools.store(tree, model_name)

    features_df = create_features(datestrs)
    departures = {}
    for datestr in datestrs:
        estimated_departure = tree.estimate(features_df.loc[datestr, :])
        departures[datestr] = estimated_departure

    return departures
Beispiel #2
0
def get_trips():
    """
    Attempt to restore a saved copy.
    If unsuccessful, download a new one.

    Returns
    -------
    trips: list of dictionaries
    """
    trips_filename = 'trips.pickle'
    try:
        trips = tools.restore(trips_filename)
    except Exception:
        trips = download_data()
        tools.store(trips, trips_filename)
    return trips
Beispiel #3
0
def get_arrival_times(trips_df):
    """
    Attempt to restore a saved copy.
    if unsuccessful, download a new one.

    Parameters
    ----------
    trips_df: DataFrame

    Returns
    -------
    arrival_times_df: DataFrame
    """
    arrival_times_filename = 'arrival_times.pickle'
    try:
        arrival_times_df = tools.restore(arrival_times_filename)
    except Exception:
        arrival_times_df = None
    if arrival_times_df is None:
        arrival_times_df = calculate_arrival_times(trips_df)
        tools.store(arrival_times_df, arrival_times_filename)

    return arrival_times_df