LoginSignup
0
0

More than 5 years have passed since last update.

しきい値でデータを分割

Last updated at Posted at 2017-04-20

昇順のデータに対し、複数のしきい値で、しきい値数+1個に分割する

python3
import numpy as np, pandas as pd
def partition(attr, ths, tgt=None):
    if tgt is None:
        tgt = attr
    elif isinstance(attr, pd.DataFrame):
        tgt = attr[tgt]
    po = 0
    for th in ths:
        pr = po
        while tgt[po] < th:
            po += 1
        yield tgt[pr:po]
    yield tgt[po:]
# def partition(arr, ths, tgt=None):
#     if tgt is None:
#         tgt = arr
#     elif isinstance(arr, pd.DataFrame):
#         tgt = arr[tgt]
#     r = []
#     pr = 0
#     for th in ths:
#         po = ilen(takewhile(lambda i: i < th, tgt[pr:]))+pr
#         r.append(arr[pr:po])
#         pr = po
#     r.append(arr[po:])
#     return r

from IPython.display import display
for i in partition(range(1,11), [3,6]):
    display(i)
for i in partition(np.arange(1,11), [3,6]):
    display(i)
for i in partition(pd.Series(np.arange(1,11)), [3,6]):
    display(i)
for i in partition(pd.DataFrame(np.arange(1,11)), [3,6], 0):
    display(i)
>>>
range(1, 3)
range(3, 6)
range(6, 11)

array([1, 2])
array([3, 4, 5])
array([ 6,  7,  8,  9, 10])

0    1
1    2
dtype: int32
2    3
3    4
4    5
dtype: int32
5     6
6     7
7     8
8     9
9    10
dtype: int32
0
0 1
1 2
0
2 3
3 4
4 5
0
5 6
6 7
7 8
8 9
9 10

コメント部分は、「NumPyでしきい値以上の位置を求める - Qiita」を参考に作ったものだが、数が少なければ、単純にwhileの方が早かったので、差し替えた。

以上

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0