Hướng dẫn moving median coderbyte python - con trăn coderbyte di chuyển trung bình

Các giải pháp cho {{thử thách.title}}{{ challenge.title }}

// Your account does not have access to view these user solutions.

{{Votelist [user.username]? Votelist [user.username] ['phiếu']: 0}}

{{user.username}} đã nhận {{user.score}} điểm | Mã vận hành{{ user.score }} points | Run code

{{idx + 1}}

{{user.username}}

{{chọn Language.replace ('CPP', 'C ++').

Điểm: {{user.score}}{{ user.score }}

Xem giải pháp

Không có giải pháp nào cho ngôn ngữ này.

Rolling.Median (numeric_only = false, động cơ = none, động cơ_kwargs = none, ** kwargs) [nguồn]#median(numeric_only=False, engine=None, engine_kwargs=None, **kwargs)[source]#

Tính trung bình cuộn.

ParameterSnumeric_onlyBool, mặc định sainumeric_onlybool, default False

Chỉ bao gồm float, int, các cột boolean.

Mới trong phiên bản 1.5.0.

enginest, mặc định không cóstr, default None
  • 'cython': Chạy hoạt động thông qua các phần mở rộng C từ Cython.

  • 'numba': Chạy hoạt động thông qua mã tổng hợp JIT từ Numba.

  • None: Mặc định là 'cython' hoặc cài đặt toàn cầu compute.use_numba

    Mới trong phiên bản 1.3.0.

động cơ_kwargsdict, mặc định không códict, default None
  • Đối với động cơ 'cython', không có sự chấp nhận

    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    0

  • Đối với động cơ 'numba', động cơ có thể chấp nhận các phím từ điển

    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    2,
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    3 và
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    4. Các giá trị phải là
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    5 hoặc
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    6. Mặc định
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    0 cho động cơ 'numba'
    >>> s = pd.Series([0, 1, 2, 3, 4])
    >>> s.rolling(3).median()
    0    NaN
    1    NaN
    2    1.0
    3    2.0
    4    3.0
    dtype: float64
    
    9

    Mới trong phiên bản 1.3.0.

**kwargs

động cơ_kwargsdict, mặc định không có

Đối với động cơ 'cython', không có sự chấp nhận

>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
0

Đối với động cơ 'numba', động cơ có thể chấp nhận các phím từ điển
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
2,
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
3 và
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
4. Các giá trị phải là
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
5 hoặc
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
6. Mặc định
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
0 cho động cơ 'numba'
>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64
9

Đối với khả năng tương thích numpy và sẽ không có ảnh hưởng đến kết quả.

Không dùng nữa kể từ phiên bản 1.5.0.

ReturnSseries hoặc dataFrameNumba engine and Numba (JIT compilation) for extended documentation and performance considerations for the Numba engine.

Loại trả về giống như đối tượng ban đầu với

from heapq import heappush, heappop


class RunningMedian:
    def __init__(self):
        self.lowers, self.highers = [], []

    def add_number(self, number):
        if not self.highers or number > self.highers[0]:
            heappush(self.highers, number)
        else:
            heappush(self.lowers, -number)  # for lowers we need a max heap
        self.rebalance()

    def rebalance(self):
        if len(self.lowers) - len(self.highers) > 1:
            heappush(self.highers, -heappop(self.lowers))
        elif len(self.highers) - len(self.lowers) > 1:
            heappush(self.lowers, -heappop(self.highers))

    def get_median(self):
        if len(self.lowers) == len(self.highers):
            return (-self.lowers[0] + self.highers[0])/2
        elif len(self.lowers) > len(self.highers):
            return -self.lowers[0]
        else:
            return self.highers[0]
0 DTYPE.

Ghi chú

>>> s = pd.Series([0, 1, 2, 3, 4])
>>> s.rolling(3).median()
0    NaN
1    NaN
2    1.0
3    2.0
4    3.0
dtype: float64

Có hàm Arraychallenge (ARR) đọc mảng các số được lưu trữ trong ARR sẽ chứa kích thước cửa sổ trượt, N, vì phần tử đầu tiên trong mảng và phần còn lại sẽ là danh sách các số. Chương trình của bạn sẽ trả về trung bình di chuyển cho từng phần tử dựa trên phần tử và người tiền nhiệm N-1 của nó, trong đó N là kích thước cửa sổ trượt. Đầu ra cuối cùng phải là một chuỗi với trung bình chuyển động tương ứng với mỗi mục trong mảng gốc được phân tách bằng dấu phẩy.

Lưu ý rằng trong một vài yếu tố đầu tiên (cho đến khi đạt được kích thước cửa sổ), trung bình được tính toán trên một số lượng nhỏ hơn các mục. Ví dụ: nếu ARR là [3, 1, 3, 5, 10, 6, 4, 3, 1] thì chương trình của bạn sẽ xuất ra "1,2,3,5,6,6,4,3"

Bạn có thể sử dụng hai đống để duy trì nửa và cao hơn của mẫu dữ liệu khi bạn xử lý nó.Thuật toán diễn ra như thế này: Đối với mỗi giá trị, hãy đặt nó vào một đống thích hợp và 'cân bằng lại' các đống để kích thước của chúng không khác nhau hơn 1. Sau đó, để lấy trung bình, chỉ cần rút ra phần tử đầu tiên từ phần đầu tiên lớn hơnHEAP, hoặc lấy trung bình của các yếu tố đầu tiên của hai đống nếu chúng có kích thước bằng nhau.Giải pháp này có độ phức tạp thời gian

from heapq import heappush, heappop


class RunningMedian:
    def __init__(self):
        self.lowers, self.highers = [], []

    def add_number(self, number):
        if not self.highers or number > self.highers[0]:
            heappush(self.highers, number)
        else:
            heappush(self.lowers, -number)  # for lowers we need a max heap
        self.rebalance()

    def rebalance(self):
        if len(self.lowers) - len(self.highers) > 1:
            heappush(self.highers, -heappop(self.lowers))
        elif len(self.highers) - len(self.lowers) > 1:
            heappush(self.lowers, -heappop(self.highers))

    def get_median(self):
        if len(self.lowers) == len(self.highers):
            return (-self.lowers[0] + self.highers[0])/2
        elif len(self.lowers) > len(self.highers):
            return -self.lowers[0]
        else:
            return self.highers[0]
1.

from heapq import heappush, heappop


class RunningMedian:
    def __init__(self):
        self.lowers, self.highers = [], []

    def add_number(self, number):
        if not self.highers or number > self.highers[0]:
            heappush(self.highers, number)
        else:
            heappush(self.lowers, -number)  # for lowers we need a max heap
        self.rebalance()

    def rebalance(self):
        if len(self.lowers) - len(self.highers) > 1:
            heappush(self.highers, -heappop(self.lowers))
        elif len(self.highers) - len(self.lowers) > 1:
            heappush(self.lowers, -heappop(self.highers))

    def get_median(self):
        if len(self.lowers) == len(self.highers):
            return (-self.lowers[0] + self.highers[0])/2
        elif len(self.lowers) > len(self.highers):
            return -self.lowers[0]
        else:
            return self.highers[0]

Demo:

>>> running_median = RunningMedian()
>>> for n in (12, 4, 5, 3, 8, 7):
...     running_median.add_number(n)
...     print(running_median.get_median())
... 
12
8.0
5
4.5
5
6.0