def columns_used(self): """ Returns all the columns used across all models in the group for filtering and in the model expression. """ return list(tz.unique(tz.concat( m.columns_used() for m in self.models.values())))
def columns_used(self): """ Returns all the columns used across all models in the group for filtering and in the model expression. """ return list( tz.unique(tz.concat(m.columns_used() for m in self.models.values())))
def _column_names_from_metadata(dicts): """ Get the unique set of keys from a list of dictionaries. Parameters ---------- dicts : iterable Sequence of dictionaries. Returns ------- keys : list Unique set of keys. """ return list(tz.unique(tz.concat(dicts)))
def initial_household_utilities(utilities, people, hh_id_col): """ Create initial household utilities by grouping and summing utilities from individual household members. Parameters ---------- utilities : pandas.DataFrame Should have the index of `people` and columns for each alternative. people : pandas.DataFrame DataFrame of individual people data. hh_id_col : str Name of the column in `people` that has their household ID. Returns ------- hh_util : dict of pandas.Series Keys will be household IDs and values will be Series mapping alternative choices to their utility. """ hh_util = {} alts = utilities.columns combo_cache = {} for hh_id, df in people.groupby(hh_id_col, sort=False): hh_size = len(df) utils = utilities.loc[df.index].as_matrix() if hh_size in combo_cache: ncombos, combos, flat_combos, tiled = combo_cache[hh_size] else: combos = list(itertools.product(alts, repeat=hh_size)) flat_combos = list( tz.concat(itertools.product(range(len(alts)), repeat=hh_size))) ncombos = len(combos) tiled = np.tile(np.arange(hh_size), ncombos) combo_cache[hh_size] = (ncombos, combos, flat_combos, tiled) u = utils[tiled, flat_combos].reshape((ncombos, hh_size)).sum(axis=1) hh_util[hh_id] = pd.Series(u, index=combos) return hh_util
def household_choices_to_people(hh_choices, people): """ Map household choices to people so that we know the activity pattern for individuals. Parameters ---------- hh_choices : pandas.Series Maps household ID to chosen alternative, where the alternative is a tuple of individual utilities. people : pandas.DataFrame DataFrame of individual people data. Returns ------- choices : pandas.Series Maps index of `people` to their activity pattern choice. """ return pd.Series( gen(tz.concat(hh_choices.values)), index=people.index)