df_extensions/space.py
def self_distance(
        self,
        id_col: Optional[str] = None,
        cols: Optional[List] = None,
        normalize: bool = False,
        transform: List[callable] = None,
    ):
        """
        Calculate the distance between all objects in a dataframe
        in an n-dimensional space.
        get back a dataframe with two labelled columns as well as the
        distance.
        id_col : unique column containing an ID or similar
        cols: all columns to be used in the calculation of distance
        normalize: should these columns be normalised before calculating
        distance
        transform: additonal functions to apply to columns after normalizating

        """
        source_df = self._obj

        if id_col == None:
            id_col = source_df.index.name
            source_df = source_df.reset_index()

        if id_col not in source_df.columns:
            source_df = source_df.reset_index()

        if cols is None:
            cols = [x for x in source_df.columns if x != id_col]

        a_col = id_col + "_A"
        b_col = id_col + "_B"
        _ = list(product(source_df[id_col], source_df[id_col]))

        df = pd.DataFrame(_, columns=[a_col, b_col])

        grid = source_df[cols]
        # normalise columns
        if normalize:
            grid = grid.apply(fnormalize, axis=0)

        if transform:
            for k, v in transform.items():
                grid[k] = v(grid[k])

        distance = pdist(grid.to_numpy())
        # back into square grid, flatten to 1d
        df["distance"] = squareform(distance).flatten()
        df = df.loc[~(df[a_col] == df[b_col])]
        return df

        Calculate the distance between all objects in a dataframe
        in an n-dimensional space.
        get back a dataframe with two labelled columns as well as the
        distance.
        id_col : unique column containing an ID or similar
        cols: all columns to be used in the calculation of distance
        normalize: should these columns be normalised before calculating
        distance
        transform: additonal functions to apply to columns after normalizating

        
Calculate the distance between all objects in a dataframe
in an n-dimensional space.
get back a dataframe with two labelled columns as well as the
distance.
id_col : unique column containing an ID or similar
cols: all columns to be used in the calculation of distance
normalize: should these columns be normalised before calculating
distance
transform: additonal functions to apply to columns after normalizating
Calculate the distance between all objects in a dataframe in an n - dimensional space . get back a dataframe with two labelled columns as well as the distance . id_col : unique column containing an ID or similar cols : all columns to be used in the calculation of distance normalize : should these columns be normalised before calculating distance transform : additonal functions to apply to columns after normalizating
def self_distance(
        self,
        id_col: Optional[str] = None,
        cols: Optional[List] = None,
        normalize: bool = False,
        transform: List[callable] = None,
    ):
        source_df = self._obj
        if id_col == None:
            id_col = source_df.index.name
            source_df = source_df.reset_index()
        if id_col not in source_df.columns:
            source_df = source_df.reset_index()
        if cols is None:
            cols = [x for x in source_df.columns if x != id_col]
        a_col = id_col + "_A"
        b_col = id_col + "_B"
        _ = list(product(source_df[id_col], source_df[id_col]))
        df = pd.DataFrame(_, columns=[a_col, b_col])
        grid = source_df[cols]
        if normalize:
            grid = grid.apply(fnormalize, axis=0)
        if transform:
            for k, v in transform.items():
                grid[k] = v(grid[k])
        distance = pdist(grid.to_numpy())
        df["distance"] = squareform(distance).flatten()
        df = df.loc[~(df[a_col] == df[b_col])]
        return df
def self_distance ( self , id_col : Optional [ str ] = None , cols : Optional [ List ] = None , normalize : bool = False , transform : List [ callable ] = None , ) : source_df = self . _obj if id_col == None : id_col = source_df . index . name source_df = source_df . reset_index ( ) if id_col not in source_df . columns : source_df = source_df . reset_index ( ) if cols is None : cols = [ x for x in source_df . columns if x != id_col ] a_col = id_col + "_A" b_col = id_col + "_B" _ = list ( product ( source_df [ id_col ] , source_df [ id_col ] ) ) df = pd . DataFrame ( _ , columns = [ a_col , b_col ] ) grid = source_df [ cols ] if normalize : grid = grid . apply ( fnormalize , axis = 0 ) if transform : for k , v in transform . items ( ) : grid [ k ] = v ( grid [ k ] ) distance = pdist ( grid . to_numpy ( ) ) df [ "distance" ] = squareform ( distance ) . flatten ( ) df = df . loc [ ~ ( df [ a_col ] == df [ b_col ] ) ] return df
Calculate the distance between all objects in a dataframe
in an n-dimensional space.
Calculate the distance between all objects in a dataframe in an n - dimensional space .
"""
        Calculate the distance between all objects in a dataframe
        in an n-dimensional space.
        get back a dataframe with two labelled columns as well as the
        distance.
        id_col : unique column containing an ID or similar
        cols: all columns to be used in the calculation of distance
        normalize: should these columns be normalised before calculating
        distance
        transform: additonal functions to apply to columns after normalizating

        """ # normalise columns # back into square grid, flatten to 1d
self
id_col
Optional[str]
cols
Optional[List]
normalize
bool
transform
List[callable]