티스토리 뷰

SWDesk

series2supervised 변환 예제

bizmaker 2021. 4. 10. 17:25

BStockTrade23

series2supervised

 

<Source Code #1>

def Test24():
    from BStockTrade23 import cBStockTrade23
    from pandas import DataFrame

    st23 = cBStockTrade23()
    datas1 = {
        'c1': [11, 12, 13, 14],
        'c2': [211, 212, 213, 215]
    }
    datas_DF = DataFrame(datas1)
    datas11 = datas_DF.values
    datas12 = st23.series2supervised(datas11)
    print(datas12)

<Result #1>

<Source Code #2>

def Test24():
    from BStockTrade23 import cBStockTrade23
    from pandas import DataFrame

    st23 = cBStockTrade23()
    datas1 = {
        'c1': [11, 12, 13, 14],
        'c2': [211, 212, 213, 215]
    }
    datas_DF = DataFrame(datas1)
    datas11 = datas_DF.values
    datas12 = st23.series2supervised(datas11, 0, 2)
    print(datas12)

<Result #2>

<Source Code : series2supervised()>

    def series2supervised(self, data, n_in=1, n_out=1, dropnan=False):
        n_vars = 1 if type(data) is list else data.shape[1]
        df = DataFrame(data)
        cols, names = list(), list()
        # input sequence(t-n, ... t-1)
        for i in range(n_in, 0, -1):
            cols.append(df.shift(i))
            names += [('var%d(t-%d)' %(j+1, i)) for j in range(n_vars)]
        # forecast sequence(t, t+1, .. t+n)
        for i in range(0, n_out):
            cols.append(df.shift(-i))
            if i==0:
                names += [('var%d(t)' %(j+1)) for j in range(n_vars)]
            else:
                names += [('var%d(t+%d)' %(j+1, i)) for j in range(n_vars)]

        # put it all together
        agg = pd.concat(cols, axis=1)
        agg.columns = names

        # drop rows with NaN values
        if dropnan:
            agg.dropna(inplace=True)

        return agg
반응형

'SWDesk' 카테고리의 다른 글

[Python] 단어 구름 네트워크  (0) 2021.04.25
[Python] Download Images from Web  (0) 2021.04.24
[Python] 카페 게시글 크롤링 프로그램 소스  (2) 2021.03.31
DataFrame 예제  (0) 2021.03.18
Database Backup  (0) 2021.03.11