当前位置: 首页>>代码示例>>Python>>正文


Python algorithms.factorize函数代码示例

本文整理汇总了Python中pandas.core.algorithms.factorize函数的典型用法代码示例。如果您正苦于以下问题:Python factorize函数的具体用法?Python factorize怎么用?Python factorize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了factorize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: from_array

    def from_array(cls, data):
        try:
            labels, levels, _ = factorize(data, sort=True)
        except TypeError:
            labels, levels, _ = factorize(data, sort=False)

        return Factor(labels, levels)
开发者ID:msabramo,项目名称:pandas,代码行数:7,代码来源:factor.py

示例2: from_array

    def from_array(cls, data):
        try:
            labels, levels, _ = factorize(data, sort=True)
        except TypeError:
            labels, levels, _ = factorize(data, sort=False)

        return Categorical(labels, levels, name=getattr(data, "name", None))
开发者ID:mattyhk,项目名称:basketball-django,代码行数:7,代码来源:categorical.py

示例3: test_datelike

    def test_datelike(self):

        # M8
        v1 = pd.Timestamp('20130101 09:00:00.00004')
        v2 = pd.Timestamp('20130101')
        x = Series([v1, v1, v1, v2, v2, v1])
        labels, uniques = algos.factorize(x)
        self.assert_numpy_array_equal(labels, np.array(
            [0, 0, 0, 1, 1, 0], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array(
            [v1.value, v2.value], dtype='M8[ns]'))

        labels, uniques = algos.factorize(x, sort=True)
        self.assert_numpy_array_equal(labels, np.array(
            [1, 1, 1, 0, 0, 1], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array(
            [v2.value, v1.value], dtype='M8[ns]'))

        # period
        v1 = pd.Period('201302', freq='M')
        v2 = pd.Period('201303', freq='M')
        x = Series([v1, v1, v1, v2, v2, v1])

        # periods are not 'sorted' as they are converted back into an index
        labels, uniques = algos.factorize(x)
        self.assert_numpy_array_equal(labels, np.array(
            [0, 0, 0, 1, 1, 0], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, pd.PeriodIndex([v1, v2]))

        labels, uniques = algos.factorize(x, sort=True)
        self.assert_numpy_array_equal(labels, np.array(
            [0, 0, 0, 1, 1, 0], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, pd.PeriodIndex([v1, v2]))
开发者ID:DLlearn,项目名称:pandas,代码行数:33,代码来源:test_algos.py

示例4: from_array

    def from_array(cls, data):
        from pandas.core.algorithms import factorize

        try:
            labels, levels, _ = factorize(data, sort=True)
        except TypeError:
            labels, levels, _ = factorize(data, sort=False)

        return Factor(labels, levels)
开发者ID:paddymul,项目名称:pandas,代码行数:9,代码来源:factor.py

示例5: from_array

    def from_array(cls, data):
        if isinstance(data, Index) and hasattr(data, 'factorize'):
            labels, levels = data.factorize()
        else:
            try:
                labels, levels = factorize(data, sort=True)
            except TypeError:
                labels, levels = factorize(data, sort=False)

        return Categorical(labels, levels,
                           name=getattr(data, 'name', None))
开发者ID:123jefferson,项目名称:MiniBloq-Sparki,代码行数:11,代码来源:categorical.py

示例6: test_mixed

    def test_mixed(self):

        # doc example reshaping.rst
        x = Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
        labels, uniques = algos.factorize(x)

        self.assert_numpy_array_equal(labels, np.array([ 0,  0, -1,  1,  2,  3],dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array(['A', 'B', 3.14, np.inf], dtype=object))

        labels, uniques = algos.factorize(x, sort=True)
        self.assert_numpy_array_equal(labels, np.array([ 2,  2, -1,  3,  0,  1],dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array([3.14, np.inf, 'A', 'B'], dtype=object))
开发者ID:ajcr,项目名称:pandas,代码行数:12,代码来源:test_algos.py

示例7: __new__

    def __new__(cls, data):
        from pandas.core.index import _ensure_index
        from pandas.core.algorithms import factorize

        try:
            labels, levels, _ = factorize(data, sort=True)
        except TypeError:
            labels, levels, _ = factorize(data, sort=False)

        labels = labels.view(Factor)
        labels.levels = _ensure_index(levels)
        return labels
开发者ID:andreas-h,项目名称:pandas,代码行数:12,代码来源:factor.py

示例8: __init__

    def __init__(self, labels, levels=None, name=None):
        if levels is None:
            if name is None:
                name = getattr(labels, 'name', None)
            try:
                labels, levels = factorize(labels, sort=True)
            except TypeError:
                labels, levels = factorize(labels, sort=False)

        self.labels = labels
        self.levels = levels
        self.name = name
开发者ID:ArbiterGames,项目名称:BasicPythonLinearRegression,代码行数:12,代码来源:categorical.py

示例9: __init__

    def __init__(self, labels, levels=None, name=None):
        if levels is None:
            if name is None:
                name = getattr(labels, 'name', None)
            if isinstance(labels, Index) and hasattr(labels, 'factorize'):
                labels, levels = labels.factorize()
            else:
                try:
                    labels, levels = factorize(labels, sort=True)
                except TypeError:
                    labels, levels = factorize(labels, sort=False)

        self.labels = labels
        self.levels = levels
        self.name = name
开发者ID:AjayRamanathan,项目名称:pandas,代码行数:15,代码来源:categorical.py

示例10: test_mixed

    def test_mixed(self):

        # doc example reshaping.rst
        x = Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
        labels, uniques = algos.factorize(x)

        exp = np.array([0, 0, -1, 1, 2, 3], dtype=np.int_)
        self.assert_numpy_array_equal(labels, exp)
        exp = pd.Index(['A', 'B', 3.14, np.inf])
        tm.assert_index_equal(uniques, exp)

        labels, uniques = algos.factorize(x, sort=True)
        exp = np.array([2, 2, -1, 3, 0, 1], dtype=np.int_)
        self.assert_numpy_array_equal(labels, exp)
        exp = pd.Index([3.14, np.inf, 'A', 'B'])
        tm.assert_index_equal(uniques, exp)
开发者ID:awolf78,项目名称:pandas,代码行数:16,代码来源:test_algos.py

示例11: factorize

 def factorize(self):
     """
     Specialized factorize that boxes uniques
     """
     from pandas.core.algorithms import factorize
     labels, uniques = factorize(self.values)
     uniques = PeriodIndex(ordinal=uniques, freq=self.freq)
     return labels, uniques
开发者ID:Wuvist,项目名称:pandas,代码行数:8,代码来源:period.py

示例12: _make_labels

 def _make_labels(self):
     if self._was_factor:  # pragma: no cover
         raise Exception('Should not call this method grouping by level')
     else:
         labs, uniques, counts = algos.factorize(self.grouper,
                                                 sort=self.sort)
         uniques = Index(uniques, name=self.name)
         self._labels = labs
         self._group_index = uniques
         self._counts = counts
开发者ID:cournape,项目名称:pandas,代码行数:10,代码来源:groupby.py

示例13: from_array

    def from_array(cls, data):
        """
        Make a Categorical type from a single array-like object.

        Parameters
        ----------
        data : array-like
            Can be an Index or array-like. The levels are assumed to be
            the unique values of `data`.
        """
        if isinstance(data, Index) and hasattr(data, "factorize"):
            labels, levels = data.factorize()
        else:
            try:
                labels, levels = factorize(data, sort=True)
            except TypeError:
                labels, levels = factorize(data, sort=False)

        return Categorical(labels, levels, name=getattr(data, "name", None))
开发者ID:pombredanne,项目名称:pandas,代码行数:19,代码来源:categorical.py

示例14: _make_labels

 def _make_labels(self):
     if self._labels is None or self._group_index is None:
         # we have a list of groupers
         if isinstance(self.grouper, BaseGrouper):
             labels = self.grouper.label_info
             uniques = self.grouper.result_index
         else:
             labels, uniques = algorithms.factorize(
                 self.grouper, sort=self.sort)
             uniques = Index(uniques, name=self.name)
         self._labels = labels
         self._group_index = uniques
开发者ID:forking-repos,项目名称:pandas,代码行数:12,代码来源:grouper.py

示例15: test_basic

    def test_basic(self):

        labels, uniques = algos.factorize(["a", "b", "b", "a", "a", "c", "c", "c"])
        # self.assert_numpy_array_equal(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array(["a", "b", "c"], dtype=object))

        labels, uniques = algos.factorize(["a", "b", "b", "a", "a", "c", "c", "c"], sort=True)
        self.assert_numpy_array_equal(labels, np.array([0, 1, 1, 0, 0, 2, 2, 2], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array(["a", "b", "c"], dtype=object))

        labels, uniques = algos.factorize(list(reversed(range(5))))
        self.assert_numpy_array_equal(labels, np.array([0, 1, 2, 3, 4], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array([4, 3, 2, 1, 0], dtype=np.int64))

        labels, uniques = algos.factorize(list(reversed(range(5))), sort=True)
        self.assert_numpy_array_equal(labels, np.array([4, 3, 2, 1, 0], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array([0, 1, 2, 3, 4], dtype=np.int64))

        labels, uniques = algos.factorize(list(reversed(np.arange(5.0))))
        self.assert_numpy_array_equal(labels, np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float64))
        self.assert_numpy_array_equal(uniques, np.array([4, 3, 2, 1, 0], dtype=np.int64))

        labels, uniques = algos.factorize(list(reversed(np.arange(5.0))), sort=True)
        self.assert_numpy_array_equal(labels, np.array([4, 3, 2, 1, 0], dtype=np.int64))
        self.assert_numpy_array_equal(uniques, np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float64))
开发者ID:arvindchari88,项目名称:newGitTest,代码行数:25,代码来源:test_algos.py


注:本文中的pandas.core.algorithms.factorize函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。