Skip to content

nbapr

pr_traditional(pool, statscols=('WFGP', 'FTM', 'FG3M', 'REB', 'AST', 'STL', 'BLK', 'TOV', 'PTS'))

Traditional player rater

Parameters:

Name Type Description Default
pool DataFrame

the player pool dataframe

required
statscols Iterable[str]

the stats columns

('WFGP', 'FTM', 'FG3M', 'REB', 'AST', 'STL', 'BLK', 'TOV', 'PTS')

Returns:

Type Description
DataFrame

pd.DataFrame with columns player[str], pts[float]

Source code in nbapr/nbapr.py
def pr_traditional(pool: pd.DataFrame, 
        statscols: Iterable[str] = ('WFGP', 'FTM', 'FG3M', 'REB', 'AST', 'STL', 'BLK', 'TOV', 'PTS'),
        ) -> pd.DataFrame:
    """Traditional player rater

    Args:
        pool (pd.DataFrame): the player pool dataframe
        statscols (Iterable[str]): the stats columns

    Returns:
        pd.DataFrame with columns
           player[str], pts[float]

    """
    pool = pool.dropna()
    stats = pool.loc[:, statscols].values
    pts = np.sum(_zscore(stats), axis=1)

    # return results
    return pd.DataFrame({
        'player': pool.PLAYER_NAME,
        'pos': pool.POS,
        'team': pool.TEAM, 
        'pr_zscore': pts
    })

rankdata(a, method='average', *, axis=None)

Assign ranks to data, dealing with ties appropriately.

Parameters:

Name Type Description Default
a ndarray

the array of values to be ranked.

required
method str

{'average', 'min', 'max', 'dense', 'ordinal'}, optional

'average'
axis Optional[int]

Union[None, int], optional

None

Returns:

Type Description
ndarray

ndarray Size equal to the size of a, containing rank scores.

Source code in nbapr/nbapr.py
def rankdata(a: np.ndarray, method: str = 'average', *, axis: Union[None, int] = None) -> np.ndarray:
    """Assign ranks to data, dealing with ties appropriately.

    Args:
        a (np.ndarray): the array of values to be ranked.
        method (str): {'average', 'min', 'max', 'dense', 'ordinal'}, optional
        axis: Union[None, int], optional

    Returns:
        ndarray
        Size equal to the size of `a`, containing rank scores.

    """
    # NOTE: this is from scipy 1.6.0 to avoid importing full library
    #       not a problem on local machine but slows github builds 

    if method not in ('average', 'min', 'max', 'dense', 'ordinal'):
        raise ValueError('unknown method "{0}"'.format(method))

    if axis is not None:
        a = np.asarray(a)
        if a.size == 0:
            # The return values of `normalize_axis_index` are ignored.  The
            # call validates `axis`, even though we won't use it.
            # use scipy._lib._util._normalize_axis_index when available
            np.core.multiarray.normalize_axis_index(axis, a.ndim)
            dt = np.float64 if method == 'average' else np.int_
            return np.empty(a.shape, dtype=dt)
        return np.apply_along_axis(rankdata, axis, a, method)

    arr = np.ravel(np.asarray(a))
    algo = 'mergesort' if method == 'ordinal' else 'quicksort'
    sorter = np.argsort(arr, kind=algo)

    inv = np.empty(sorter.size, dtype=np.intp)
    inv[sorter] = np.arange(sorter.size, dtype=np.intp)

    if method == 'ordinal':
        return inv + 1

    arr = arr[sorter]
    obs = np.r_[True, arr[1:] != arr[:-1]]
    dense = obs.cumsum()[inv]

    if method == 'dense':
        return dense

    # cumulative counts of each unique value
    count = np.r_[np.nonzero(obs)[0], len(obs)]

    if method == 'max':
        return count[dense]

    if method == 'min':
        return count[dense - 1] + 1

    # average method
    return .5 * (count[dense] + count[dense - 1] + 1)