'Python 분석과 프로그래밍'에 해당되는 글 272건

  1. 2020.02.05 [Python Numpy] numpy array 거꾸로 뒤집기 (how to reverse numpy array)
  2. 2020.01.30 [Python] 객체 지향 프로그래밍과 클래스 (Object-Oriented Programming and Class in Python)
  3. 2020.01.18 [Python] 여러개의 수평/수직 막대그래프를 축 단위를 고정하여 그리기 (multiple bar plots with fixed and shared axis scale)
  4. 2020.01.12 [Python] 선형회귀모형, 로지스틱 회귀모형에 대한 각 관측치 별 변수별 기여도(민감도) 분석 (Sensitivity analysis of linear regression & Logistic regression per each variables and each observations) 1
  5. 2020.01.05 [Python 시계열 자료 분석] 시계열 패턴별 지수 평활법 (exponential smoothing by time series patterns) 5
  6. 2020.01.02 [Python 시계열 자료 분석] 시계열 분해 (Time series Decomposition)
  7. 2020.01.01 [Python 시계열 자료 분석] 시계열 구성 요인 (Time series component factors): 추세(trend), 순환(cycle), 계절(seasonal), 불규칙(irregular) 요인 2
  8. 2019.12.31 [Python pandas] Upsampling 변환 시 생기는 결측값 채우기(fill na), 선형 보간하기(linear interpolation)
  9. 2019.12.30 [Python pandas] Downsampling 으로 시계열 데이터 집계 시 좌/우의 포함여부(closed), 라벨 이름(label) 설정하기
  10. 2019.12.30 [Python pandas] 분기 단위의 기간 날짜 범위 만들기, timestamp와 변환하기 (Quarterly period frequencies and range, conversion b/w timestamp)

이번 포스팅에서는 Python numpy 의 배열 원소의 순서를 거꾸로 뒤집기 (how to reverse the python numpy array) 하는 두가지 방법을 소개하겠습니다. 1D array의 원소를 뒤집는 것은 간단한데요, 2D 이상의 다차원 배열(multi-dimensional array)의 경우 좀 복잡하므로 간단한 예를 들어 유형별로 설명을 해보겠습니다.  


(1) x[::-1] 를 이용해서 배열 뒤집기 (mirror 반환하기)

(2) np.flip(x) 를 이용해서 배열 뒤집기





   1 차원 numpy 배열을 뒤집기 (how to reverse 1D numpy array?)


먼저 예제로 사용할 간단한 1차원 numpy 배열을 만들어보겠습니다. 



import numpy as np


# 1D array

arr_1d = np.arange(5)

arr_1d 

[Out]: array([0, 1, 2, 3, 4])




다음으로, 1차원 numpy 배열을 (1) x[::-1] 방법과, (2) np.flip(x) 방법을 이용하여 뒤집어보겠습니다. 


 (1) x[::-1]

(2) np.flip(x)


# returns a view in reversed order

arr_1d[::-1]

[Out]: array([4, 3, 2, 1, 0])


# 1D array in reversed order using np.flip()

np.flip(arr_1d)

 [Out]: array([4, 3, 2, 1, 0])





  2 차원 numpy 배열을 뒤집기 (how to reverse 2D numpy array?)


2차원 이상의 numpy 배열 뒤집기는 말로 설명하기가 좀 어렵고 복잡합니다. 왜냐하면 배열의 차원(축, axis) 을 무엇으로 하느냐에 따라서 뒤집기의 기준과 기대하는 결과의 모습(reversed output) 달라지기 때문입니다. 따라서 아래에는 2차원 numpy 배열에 대해서 3가지 경우의 수에 대해서 각각 x[::-1] 과 np.flip(x) 을 사용한 방법을 소개하였으니 원하는 뒤집기 output 에 맞게 선택해서 사용하시기 바랍니다. 


먼저 예제로 사용할 2차원 numpy 배열(2D numpy array)을 만들어보겠습니다. 



import numpy as np


# 2D array

arr_2d = np.arange(10).reshape(2, 5)

arr_2d

[Out]:
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])




이제 2차원 numpy 배열을 한번 뒤집어 볼까요? 



(2-1) axis = 0 기준으로 2차원 numpy 배열 뒤집기 (how to reverse numpy 2D array by axis=0)


 (1) x[::-1]

 (2) np.flip(x, axis=0)


# returns a view in reversed order by axis=0

arr_2d[::-1]

[Out]:

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


# reverse 2D array by axis 0

np.flip(arr_2d, axis=0)

[Out]:
array([[5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4]])




(2-2) axis = 1 기준으로 2차원 numpy 배열 뒤집기 (how to reverse numpy 2D array by axis=1)


 (1) x[:, ::-1]

 (2) np.flip(x, axis=1)


# returns a view of 2D array by axis=1

arr_2d[:, ::-1]

[Out]:
array([[4, 3, 2, 1, 0],
       [9, 8, 7, 6, 5]])


# reverse 2D array by axis 1

np.flip(arr_2d, axis=1) 

[Out]:
array([[4, 3, 2, 1, 0],
       [9, 8, 7, 6, 5]])




(2-3) axis =0 & axis = 1 기준으로 2차원 numpy 배열 뒤집기 (revserse numpy 2D array by axis=0 &1)


 (1) x[:, ::-1][::-1]

 (2) np.flip(x)


# returns a view

arr_2d[:, ::-1][::-1]

[Out]:

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


# 2D array

np.flip(arr_2d)

[Out]:
array([[9, 8, 7, 6, 5],
       [4, 3, 2, 1, 0]])



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)

728x90
반응형
Posted by Rfriend
,

JAVA, C++, C#, Python, PHP 등을 사용하는 SW 개발자 중에 객체 지향 프로그래밍(Object-Oriented Programming, OOP)과 클래스(Class)를 모르는 분은 없을 것입니다. 


반면에, 통계와 데이터 분석을 전공하신 분들 중에는 객체 지향 프로그래밍과 클래스에 대해서 모르는 분이 상당히 많을 것이라고 보며, 아마도 '내가 개발자도 아닌데 객체 지향 프로그래밍과 클래스를 왜 알아야 해? 사용자 정의함수 만들어 쓸 수 있으면 되는거 아닌가?' 라고 생각하며 아예 선을 그어버리고 외면하는 분도 계실 듯 합니다. (제가 예전에 이랬거든요. ^^;)


그런데 통계/머신러닝 모델을 운영 시스템에 반영하기 위해 개발자와 협업하다보면 클래스 얘기가 나오고, 또 딥러닝 공부하다보니 자꾸 클래스와 맞닥드리게 되어서 이참에 클래스 공부도 하고 블로그에 정리도 하게 되었습니다. 


이번 포스팅에서는 통계와 분석업무 하시는 분들을 독자로 가정하고 파이썬3의 클래스(Class in Python 3)에 대해서 소개하겠습니다. 


1. 객체 지향 프로그래밍은 무엇이며, 클래스는 무엇인가? 

2. 왜 객체지향 프로그램이 필요한가?

3. 클래스와 인스턴스는 어떻게 만드는가? 

4. 클래스 상속이란 무엇인가? 

5. 클래스와 함수의 차이점은 무엇인가


ps. Private member, 상속의 super(), 다중 상속, decorator, iterator, generator, 추상 기반 클래스(abstract base class), Overriding 등의 세부 심화 내용은 이번 포스팅에서는 제외하겠으며, 파이썬 프로그래밍 개발 관련 전문 서적이나 사이트를 참고하시기 바랍니다. 



  1. 객체 지향 프로그래밍은 무엇이며, 클래스는 무엇인가? 


위키피디아에서 객체 지향 프로그래밍을 어떻게 정의하고 있는지 찾아보았습니다. 


객체 지향 프로그래밍(Object-oriented programming, OOP)은 객체(Objects) 개념에 기반한 프로그래밍 패러다임으로서, 변수(field) 혹은 속성(attribute), 특성(property)이라고 알려진 데이터(data)와 프로시져(procedures) 또는 메소드(methods) 형태의 코드(code) 를 가지고 있다. (Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).)


객체의 프로시져를 통해 객체의 연관되어 있는 데이터 변수들에 접근하고 변경할 수 있는 특성이 있다. (객체는 "this" 또는 "self"의 표기를 가지고 있다) (A feature of objects is an object's procedures that can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self").)


객체 지향 프로그래밍에서 컴퓨터 프로그램은 서로 상호작용하는 객체들을 만듦으로써 설계한다. 객체 지향 프로그래밍 언어는 다양하지만 가장 일반적인 것은 클래스 기반(class-based) 의 언어이다. 클래스 기반이란 클래스의 인스턴스가 객체가 되고 유형(type)을 결정하는 것을 의미한다. (In OOP, computer programs are designed by making them out of objects that interact with one another. OOP languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types.)


* source: Wikipedia


말이 좀 어려운데요, 한번 더 정리를 해보자면, 

  • 객체(objects) = data (fields, attributes, properties) + code (procedures, methods)
  • code(procedures, methods) 를 통해 data(fields, attributes, properties)에 접근하고 변경 가능
  • class 로 data와 code를 겹합하며, 클래스를 구체화(실체화)한 인스턴스는 객체가 됨


Python도 객체 지향 프로그래밍을 지원하는 대표적인 언어이며, 위의 위키피디아 정의와 동일하게 객체는 속성(attribute)과 기능(method), 다른 말로 표현하면 객체는 변수(filed)와 함수(function)로 구성됩니다. 이들 속성(attribute)(또는 변수(field))와 기능(method)(또는 함수(function))을 클래스(class)를 사용해서 하나로 묶어줍니다. 클래스를 실체화하여 인스턴스(instance) 객체를 생성합니다. (아래 3번의 실제 예를 살펴보면 이해하기에 더 쉬울 것입니다)





  2. 왜 객체 지향 프로그램이 필요한가?


객체 지향 프로그래밍을 하면 객체(변수 + 함수) 내의 응집력은 강하지만 객체 간의 응집력은 약하게 하여 소프트웨어의 개발, 유지보수, 업그레이드를 보다 쉽게 할 수 있도록 해주는 장점이 있습니다. 


"객체 지향 프로그래밍은 프로그램을 유연하고 변경이 용이하게 만들기 때문에 대규모 소프트웨어 개발에 많이 사용된다. 또한 프로그래밍을 더 배우기 쉽게 하고 소프트웨어 개발과 보수를 간편하게 하며, 보다 직관적인 코드 분석을 가능하게 하는 장점을 갖고 있다."

* source: wikipedia


"같은 목적과 기능을 위해 객체로 묶인 코드 요소(변수, 함수)들은 객체 내부에서만 강한 응집력을 발휘하고 객체 외부에 주는 영향은 줄이게 됩니다. 코드가 객체 위주로 (또는 순수하게 객체로만) 이루어질 수 있도록 지향하는 프로그래머의 노력은 코드의 결합도를 낮추는 결과를 낳게 됩니다."

* source: '뇌를 자극하는 파이썬3', 박상현 지음, 한빛미디어


아래의 클래스를 어떻게 만들고, 상속(inheritance)은 무엇인지를 알면 응집력에 대한 위의 내용이 좀더 이해가 쉬울 것입니다. 



  3. 클래스(class)와 인스턴스(instance)는 어떻게 만드는가? 




-- 클래스 정의 (create a Class) -- 

(a) 클래스 정의는 class 키워드 (keyword) 로 시작하고, 다음에 클래스 이름을 써주며, 뒤에 콜론(':')을 써줍니다. 


(b) 클래스 이름은 보통 대문자(capital letter)로 시작합니다. 

            아래 예) class PersonalInfo:


(c) 클래스의 코드 블락에는 

    (c-1) 클래스 전체에 공통으로 사용하는 클래스 속성(class attribute), 

            아래 예) nationality = "Korean"

   (c-2) 인스턴스별로 객체를 초기화해서 사용하는 인스턴스 속성(instance attributes)

            아래 예) def __init__(self, name, age):

                             self.name = name

                             self.age = age

   (c-3) 기능을 수행하는 인스턴스 메소드(instance methods) 를 정의합니다. 

            아래 예) def getPersonalInfo(self):

                             print("Name:", self.name)


(d) 인스턴스 객체의 속성을 초기화해주는 역할은 def __init__(): 의 마법 메소드(magic method) 를 사용합니다. 


(e) 'self'는 메소드가 소속되어 있는 객체를 의미합니다. ( C++, C#, JAVA 의 'this'와 동일한 역할)


-- 인스턴스 객체 생성 (create an instance object) --

(g) 클래스 이름(변수 값) 생성자로 클래스를 구체화/ 상세화한 인스턴스(instance)를 정의합니다. 

          아래 예) personal_choi = PersonalInfo('CK Choi', 25)




# class definition starts with 'class' keyword

# class name starts with the CAPITAL LETTER usually

class PersonalInfo:

    

    # Class attribute

    nationality = "Korean"

    

    # Initalizer, Instance attributes

    def __init__(self, name, age):

        self.name = name

        self.age = age

        

    # instance method 1

    def getPersonalInfo(self):

        print("Name:", self.name)

        print("Age:", self.age)

        

    # instance method 2

    def ageGroup(self):

        if self.age < 30:

            return "under 30"

        else:

            return "over 30"

        

    # instance method 3

    def FirstName(self):

        print(self.name.split(' ')[0])

    

    # instance method 4

    def LastName(self):

        print(self.name.split(' ')[1])

 




아래 코드는 클래스 생성 시점에 메모리에 같이 저장이 되고, 클래스 전체에서 공유하는 속성인 클래스 속성(class attribute) 값을 조회한 결과입니다. '클래스이름.속성이름'  의 형태로 조회합니다. 



# get class attribute

PersonalInfo.nationality

[Out]:'Korean'

 




아래의 코드는 클래스를 상세화/구체화하여 인스턴스 객체를 생성한 예입니다. 인스턴스 객체의 속성과 메소드는 인스턴스 객체를 생성하는 시점에 메모리에 저장이 됩니다.  


인스턴스 객체를 생성할 때마다 __init__(self) 의 마법 메소드(magic method)가 속성 값을 초기화(initialization) 해줍니다. 인스턴스 속성은 '인스턴스 객체 이름'에 점('.')을 찍고 뒤에 '속성 이름'을 써주면 조회할 수 있습니다.  

    아래 예) personal_choi.name


인스턴스 메소드 호출은 인스턴스 객체 이름에 점('.')을 찍고 뒤에 '메소드 이름()'을 써주면 됩니다. 

    아래 예) personal_choi.getPersonalInfo()


인스턴스 객체 1 (instance object 1)

인스턴스 객체 2 (instance object 2)


# instance

personal_choi = PersonalInfo('CK Choi', 25)


# get instance attribute

personal_choi.name

[Out]: 'CK Choi'


# instance method 1

personal_choi.getPersonalInfo()

[Out]:
Name: CK Choi
Age: 25


# instance method 2

personal_choi.ageGroup()

[Out]: 'under 30'


# instance method 3

personal_choi.FirstName()

[Out]: CK


# instance method 4

personal_choi.LastName()

[Out]: Choi



# instance

personal_park = PersonalInfo('SJ Park', 33)


# get instance attribute

personal_park.name

[Out]: 'SJ Park'

# instance method 1 personal_choi.getPersonalInfo()

[Out]: 
Name: SJ Park
Age: 33


# instance method 2

personal_park.ageGroup()

[Out]: 'over 30'

# instance method 3

personal_park.FirstName()

[Out]: SJ


# instance method 4

personal_park.LastName()

[Out]: Park





  4. 클래스 상속이란 무엇인가? 





부모가 자식에게 재산을 상속하듯이, 클래스도 클래스가 다른 클래스에게 상속을 해줄 수 있습니다. 상속을 해주는 클래스를 부모 클래스(Parent Class) 혹은 기반 클래스(Base Class) 라고 하며, 상속을 받는 클래스를 자식 클래스(Child Class) 혹은 파생 클래스(Derived Class)라고 합니다. 도형으로 표기할 때는 바로 위 그림의 예시처럼 '자식 클래스'에서 시작해서 '부모 클래스'로 향하는 화살표를 그려줍니다. 


부모 클래스를 생성한 후에 자식 클래스 이름 끝의 괄호() 안에 부모 클래스의 이름을 적어주면 자식 클래스가 부모 클래스의 모든 데이터 속성과 메소드를 유산으로 상속받아 부모 클래스처럼 역할을 할 수가 있습니다. 

    위의 그림 예) class ChildClass(ParentClass): 

                             pass



위의 '3. 클래스는 어떻게 만드는가?'에서 들었던 예제 클래스 PersonalInfo 를 부모 클래스로 하고, 이를 상속받은 자식 클래스를 class ContactInfo(PersonalInfo):  로 아래에 만들어보겠습니다. 부모 클래스에서 상속받은 데이터 속성, 메소드 외에 def getContactInfo(self, cellphone, city): 로 인스턴스 메소드를 추가로 정의해주었습니다. 



# Child(derived) class (inherits from PersonalInfo() parent(base) class)

class ContactInfo(PersonalInfo):

    def getContactInfo(self, cellphone, city):

        print("Name:", self.name)

        print("Age:", self.age)

        print("Celluar Phone:", cellphone)

        print("City:", city)

 



아래에는 contact_lee = ContactInfo('SH Lee', 41) 으로 위의 자식 클래스를 상세화한 contact_lee 라는 이름의 인스턴스 객체를 만들었습니다. 아래에 보시다시피 getPersonalInfo(), ageGroup(), FirstName(), LastName() 등의 부모 클래스에서 정의했던 인스턴스 메소드를 자식 클래스로 부터 생성한 인스턴스에서도 동일하게 사용할 수 있음을 알 수 있습니다. 



contact_lee = ContactInfo('SH Lee', 41)


# instance method from Parent class

contact_lee.getPersonalInfo()

[Out]:

Name: SH Lee Age: 41


# instance method from Parent class

contact_lee.ageGroup()

[Out]: 'over 30'


# instance method from Parent class

contact_lee.FirstName()

[Out]: SH


# instance method from Parent class

contact_lee.LastName()

[Out]: Lee


# -- instance method from Child class

contact_lee.getContactInfo('011-1234-5678', 'Seoul')

[Out]: 
Name: SH Lee
Age: 41
Celluar Phone: 011-1234-5678
City: Seoul





  5. 클래스와 함수의 차이점은 무엇인가


코드의 재사용성(reusability) 측면에서는 클래스와 함수가 유사한 측면도 있습니다만, 클래스와 함수는 엄연히 다릅니다. 클래스의 이해를 돕기 위해 마지막으로 한번 더 정리하는 차원에서 클래스와 함수의 차이점을 비교해보자면요, 



  • 클래스(class)는 같은 목적과 기능을 위해 '데이터 속성(변수)'과 '기능(함수)'를 결합해 놓은 집합체/그룹으로서, 객체 지향 프로그래밍에서 인스턴스 객체를 생성하는데 사용이 됩니다. 
  • vs. 함수(function)는 특정 기능/과업을 수행하는 코드 블록입니다. (A function is a unit of code devoted to carrying out a specific task)

클래스(class)가 함수(function) 보다 더 광범위한 개념이며, 함수는 클래스의 부분집합입니다. 


많은 도움이 되었기를 바랍니다.

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



[Reference]

1. '뇌를 자극하는 파이썬3', 박상현 지음, 한빛미디어

2. Object-Oriented Programming in Python 3, by Real Python

3. Object-Oriented Programming, Wikipedia




728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 1. plt.subplot() 2. plt.subplots() 를 이용해서 


(1) (nrows=1, ncols=3)개의 복수의 하위 막대 그래프를 그리는 방법

     (multiple subplots of bar plots using plt.subplot())

(2) 복수의 막대 그래프 간에 y 축의 단위를 고정하는 방법 (fix y axis scale)

(3) 복수의 막대 그래프 간에 y 축의 이름을 공유하는 방법 (share y axis label)

(4) 복수의 하위 그래프 간 여백을 작게 하여 밀착해서 그리는 방법 (plots in tight layout)


을 소개하겠습니다. 


그리고 마지막에는 복수의 옆으로 누운 막대 그래프 (multiple horizontal bar plots) 를 그리는 방법을 소개하겠습니다. 


먼저 예제로 사용할 간단한 DataFrame을 만들어보겠습니다. 



import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


# make a sample DataFrame

grp = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']

col = ['x1', 'x2', 'x3'] * 3

val = [3.5, 4.2, 2.9, 0.5, 0.2, 0.3, 1.5, 2.5, 2.6]


df = pd.DataFrame({'grp': grp, 

                  'col': col, 

                  'val': val})


df

grpcolval
0ax13.5
1ax24.2
2ax32.9
3bx10.5
4bx20.2
5bx30.3
6cx11.5
7cx22.5
8cx32.6

 




  1. plt.subplot() 으로 복수의 막대 그래프 그리기


(1-1) (행 1개, 열 3개) 로 구성된 복수의 하위 막대 그래프 그리기 

        (multiple subplots of bar plots using plt.subplot())


plt.subplot(nrow, ncol, position) 의 형태로 행과 열의 위치에 여러개의 하위 그래프를 그릴 수 있습니다. 


아래의 예제에서는 고쳤으면 하는 3가지 문제점이 보이네요. 

첫째 문제는 Y 축의 단위(y axis scale)가 서로 다르다 보니 비교를 하기가 힘듭니다. 

둘째 문제는 X축과 Y축의 이름(label)과 단위 눈금(ticks)이 반복해서 나타나서 지저분해 보입니다. 

셋째 문제는 복수의 하위 그래프 간 간격이 떨어져 있어서 좀 작게 보입니다. 


아래에 차례대로 이들 문제를 해결해 보겠습니다. 



# (1-1) multiple bar plots with different y axis scale

plt.rcParams['figure.figsize'] = [12, 6]


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    plt.xlabel('X variable', fontsize=14)

    plt.ylabel('Value', fontsize=14)

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)






(1-2) 복수의 막대 그래프 간에 y 축의 단위를 고정하는 방법 (fix y axis scale)


plt.ylim(min, max) 의 형태로 y 축의 단위를 설정 또는 고정 (set/ fix y axis scale) 할 수 있습니다. 



 # (1-2) Set fixed y axis scale and Share it together

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    plt.xlabel('X variable', fontsize=14)

    plt.ylabel('Value', fontsize=14)

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    # set fixed y axis scale

    plt.ylim(0, max_val)





(1-3) 복수의 막대 그래프 간에  X, Y 축의 이름을 공유하는 방법 (share X and Y axis label)


if 조건문을 사용하여 X축의 이름(X label)은 중간 위치 (index = 1)의 하위 그래프만 나타나게 하고, Y축의 이름(Y label)은 첫번째 그래프(index = 0) 에만 나타나게 하면 X축과 Y축의 이름을 서로 공유한 것과 같은 효과를 낼 수 있습니다. 



# (1-3) Display only 1 X and Y label

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    

    # display only 1 X and Y label

    if i == 1:

        plt.xlabel('X variable', fontsize=14)

    if i == 0:

        plt.ylabel('Value', fontsize=14)

    if i != 0:

        plt.yticks([])

    

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    plt.ylim(0, max_val)





(1-4) 복수의 하위 그래프 간 여백을 작게 하여 밀착해서 그리는 방법 (plots in tight layout)


plt.tight_layout() 메소드를 이용하여 복수의 하위 그래프 간 여백 간격을 좁게하여 밀집된 형태로 좀더 크게 복수의 그래프를 그릴 수 있습니다. 



# (1-4) Display multiple plots in Tight Layout

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    

    # display only 1 X and Y label

    if i == 1:

        plt.xlabel('X variable', fontsize=14)

    if i == 0:

        plt.ylabel('Value', fontsize=14)

    if i != 0:

        plt.yticks([])

    

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    plt.ylim(0, max_val)


# display in tight layout

plt.tight_layout()





  2. plt.subplots() 로 복수의 막대 그래프 그리기


plt.subplots() 는 위의 plt.subplot() 보다 좀더 적은 코드로 깔끔하게 복수의 하위 막대그래프를 그릴 수 있습니다. 

  • (nrows, ncols) 로 하위 그래프를 그릴 행과 열을 지정해줍니다. 
  • sharey=True 로 y 축 공유, sharex=True 로 복수 그래프 간 x 축을 공유할 수 있습니다. (편리!)
  • if 조건문과 함께 사용해서 ax.set_ylabel(), ax.set_xlabel() 로 y축과 x축의 이름을 하나만 나타나게 하였습니다. 
  • plt.tight_layout() 로 복수의 하위 그래프 간 여백을 좁게 해서 그래프를 그렸습니다. 


# (2-1) Multiple bar plots

# (2-2) Use fixed y axis scale

# (2-3) Display only 1 X and Y label

# (2-4) Display multiple plots in Tight Layout

fig, axes = plt.subplots(nrows=1

                         , ncols=3

                         , sharey=True

                         , figsize=(12,6))


grp = ['a', 'b', 'c']


for i, ax in enumerate(axes):

    df_i = df[df['grp'] == grp[i]]

    ax.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    ax.set_title("Group: {grp}".format(grp=grp[i]), fontsize=18)

    if i == 0:

        ax.set_ylabel("Value", fontsize=14)

    if i == 1:

        ax.set_xlabel("X variable", fontsize=14)

        

plt.tight_layout()





  3. 복수의 옆으로 누운 막대 그래프 (multiple horizontal bar plot) 그리기


ax.barh() 를 사용하여 옆으로 누운 막대 그래프 (horizontal bar plot)을 그렸으며, 복수개의 하위 그래프 그리는 방법은 2번에서 소개한 plt.subplots() 함수를 차용하였습니다. 


이때 옆으로 누운 막대 그래프이기 때문에, 가독성을 높이기 위해서 그룹의 이름을 제목(title)이 아니라 Y 축 이름(y label) 에 표시를 하였습니다. 



# Horizontal Multiple Bar Plots using plt.subplots()

fig, axes = plt.subplots(nrows=3

                         , ncols=1

                         , sharex=True

                         , figsize=(8,10))


grp = ['a', 'b', 'c']


for i, ax in enumerate(axes):

    df_i = df[df['grp'] == grp[i]]

    ax.barh(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    #ax.set_title("Group: {grp}".format(grp=grp[i]), fontsize=18)

    ax.set_ylabel("Group: {grp}".format(grp=grp[i]), fontsize=18)

    if i == 2:

        ax.set_xlabel("Value", fontsize=14)

        

plt.tight_layout()


 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 선형회귀모형에 대한 각 관측치별 변수별 기여도 분석 (each variable contribution per each observations)에 대해서 소개하겠습니다. 


변수 중요도 (variable importance, feature importance)가 전체 관측치를 사용해 적합한 모델 단위의 변수별 (상대적) 중요도를 나타내는 것이라면, 이번 포스팅에서 소개하려는 관측치별 변수별 기여도(민감도)는 개별 관측치 단위에서 한개의 칼럼이 모델 예측치에 얼마나 기여를 하는지를 분석해보는 것입니다. 


선형회귀 모형을 예로 들면, 변수 중요도는 회귀모형의 회귀 계수라고 할 수 있겠구요, 관측치별 변수별 기여도는 특정 관측치의 특정 칼럼값만 그대로 사용하고 나머지 변수값에는 '0'을 대입해서 나온 예측치라고 할 수 있겠습니다. 변수 중요도가 Global 하게 적용되는 model weights 라고 한다면, 관측치별 변수별 기여도는 specific variable's weight * value 라고 할 수 있겠습니다. 




변수 중요도를 분석했으면 됐지, 왜 관측치별 변수별 기여도 분석을 할까 궁금할 수도 있을 텐데요, 관측치별 변수별 기여도 분석은 특정 관측치별로 개별적으로 어떤 변수가 예측치에 크게 영향을 미쳤는지 파악하기 위해서 사용합니다. (동어반복인가요? ^^;) 


모델의 변수별 가중치(high, low)와 각 관측치별 변수별 값(high, low)의 조합별로 아래와 같이 관측치별 변수별 예측치에 대한 기여도가 달라지게 됩니다. 가령, 관측치별 변수별 예측치에 대한 기여도가 높으려면 모델의 가중치(중요도)도 높아야 하고 동시에 개별 관측치의 해당 변수의 관측값도 역시 높아야 합니다. 



관측치별 변수별 기여도 분석은 복잡하고 어려운 이론에 기반을 둔 분석이라기 보다는 단순한 산수를 반복 연산 프로그래밍으로 계산하는 것이므로 아래의 예를 따라가다 보면 금방 이해할 수 있을 것이라고 생각합니다. 



예를 들어보기 위해 공개된 전복(abalone) 데이터를 사용하여 전체 무게(whole weight)를 예측하는 선형회귀모형을 적합하고, 관측치별로 각 변수별 예측치에 기여한 정도를 분석해보겠습니다. 



  1. 선형회귀모형에 대한 관측치별 변수별 기여도(민감도) 분석 

    (Sensitivity analysis for linear regression model)



(1) abalone dataset 을 pandas DataFrame으로 불러오기



import numpy as np

import pandas as pd


url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data'

abalone = pd.read_csv(url

                      , sep=','

                      , header=None

                      , names=['sex', 'length', 'diameter', 'height', 

                               'whole_weight', 'shucked_weight', 'viscera_weight',

                               'shell_weight', 'rings']

                     , index_col=None)


abalone.head()

[Out]:

sexlengthdiameterheightwhole_weightshucked_weightviscera_weightshell_weightrings
0M0.4550.3650.0950.51400.22450.10100.15015
1M0.3500.2650.0900.22550.09950.04850.0707
2F0.5300.4200.1350.67700.25650.14150.2109
3M0.4400.3650.1250.51600.21550.11400.15510
4I0.3300.2550.0800.20500.08950.03950.0557




(2) X, y 변수 데이터셋 생성 (creating X and y dataset)


예측하려는 목표변수 y 로는 '전체 무게(whole weight)' 변수를 사용하겠으며, 설명변수 X 에는 'sex', 'length', 'diameter', 'height', 'shell_weight', 'rings' 의 6개 변수를 사용하겠습니다. 이중에서 'sex' 변수는 범주형 변수이므로 가변수(dummy variable)로 변환하였습니다. 



# transformation of categorical variable to dummy variable

abalone['sex'].unique()

[Out]: array(['M', 'F', 'I'], dtype=object)


abalone['sex_m'] = np.where(abalone['sex'] == 'M', 1, 0)

abalone['sex_f'] = np.where(abalone['sex'] == 'F', 1, 0)


# get X variables

X = abalone[["sex_m", "sex_f", "length", "diameter", "height", "shell_weight", "rings"]]


import statsmodels.api as sm

X = sm.add_constant(X) # add a constant to model

print(X)


[Out]:
Index(['const', 'sex_m', 'sex_f', 'length', 'diameter', 'height',
       'shell_weight', 'rings'],
      dtype='object')

      const  sex_m  sex_f  length  diameter  height  shell_weight  rings
0       1.0      1      0   0.455     0.365   0.095        0.1500     15
1       1.0      1      0   0.350     0.265   0.090        0.0700      7
2       1.0      0      1   0.530     0.420   0.135        0.2100      9
3       1.0      1      0   0.440     0.365   0.125        0.1550     10
4       1.0      0      0   0.330     0.255   0.080        0.0550      7
...     ...    ...    ...     ...       ...     ...           ...    ...
4172    1.0      0      1   0.565     0.450   0.165        0.2490     11
4173    1.0      1      0   0.590     0.440   0.135        0.2605     10
4174    1.0      1      0   0.600     0.475   0.205        0.3080      9
4175    1.0      0      1   0.625     0.485   0.150        0.2960     10
4176    1.0      1      0   0.710     0.555   0.195        0.4950     12

[4177 rows x 8 columns]


# get y value

y = abalone["whole_weight"]

 



Train:Test = 8:2 의 비율로 데이터셋을 분할 (Split train and test set)하겠습니다. 



# train, test set split

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(X, 

                                                    y, 

                                                    test_size=0.2, 

                                                    random_state=2004)

print('Shape of X_train:', X_train.shape

print('Shape of X_test:', X_test.shape

print('Shape of y_train:', y_train.shape

print('Shape of y_test:', y_test.shape)

[Out]:

Shape of X_train: (3341, 8) Shape of X_test: (836, 8) Shape of y_train: (3341,) Shape of y_test: (836,)





(3) 선형회귀모형 적합 (fitting linear regression model)


이번 포스팅이 선형회귀모형에 대해서 설명하는 것이 목적이 아니기 때문에 회귀모형을 적합하는데 필요한 가정사항 진단 및 조치생략하고, 그냥 statsmodels.api 라이브러리를 이용해서 회귀모델을 적합해서 바로 민감도 분석(Sensitivity analysis)으로 넘어가겠습니다


이번 포스팅의 주제인 '관측치별 변수별 기여도 분석'에서 사용하는 모델은 어떤 알고리즘도 전부 가능하므로 개별 관측치별 변수별 영향력을 해석을 하는데 유용하게 사용할 수 있습니다. (가령, 블랙박스 모형인 Random Forest, Deep Learning 등도 가능)



# multivariate linear regression model

import statsmodels.api as sm


lin_reg = sm.OLS(y_train, X_train).fit()


lin_reg.summary()

OLS Regression Results
Dep. Variable:whole_weightR-squared:0.942
Model:OLSAdj. R-squared:0.942
Method:Least SquaresF-statistic:7738.
Date:Fri, 17 Jan 2020Prob (F-statistic):0.00
Time:20:06:12Log-Likelihood:2375.3
No. Observations:3341AIC:-4735.
Df Residuals:3333BIC:-4686.
Df Model:7
Covariance Type:nonrobust
coefstd errtP>|t|[0.0250.975]
const-0.36090.015-23.7950.000-0.391-0.331
sex_m0.03490.0066.0390.0000.0240.046
sex_f0.02170.0063.4930.0000.0100.034
length1.25390.10711.7240.0001.0441.464
diameter0.07490.1350.5540.580-0.1900.340
height0.38800.0884.4100.0000.2160.561
shell_weight2.43840.03864.8020.0002.3652.512
rings-0.01540.001-18.4230.000-0.017-0.014
Omnibus:698.165Durbin-Watson:1.978
Prob(Omnibus):0.000Jarque-Bera (JB):4041.763
Skew:0.866Prob(JB):0.00
Kurtosis:8.102Cond. No.866.


# prediction

predicted = lin_reg.predict(X_test)

actual = y_test


act_pred_df = pd.DataFrame({'actual': actual

                            , 'predicted': predicted

                            , 'error': actual - predicted})


act_pred_df.head()

[Out]:

actualpredictederror
34730.0455-0.0947030.140203
35230.09700.0206040.076396
18620.51850.690349-0.171849
29661.48201.3589500.123050
6590.95851.137433-0.178933



# Scatter Plot: Actual vs. Predicted

import matplotlib.pyplot as plt


plt.plot(act_pred_df['actual'], act_pred_df['predicted'], 'o')

plt.xlabel('actual', fontsize=14)

plt.ylabel('predicted', fontsize=14)

plt.show()


# RMSE (Root Mean Squared Error)

from sklearn.metrics import mean_squared_error

from math import sqrt


rmse = sqrt(mean_squared_error(actual, predicted))

rmse

[Out]: 0.11099248621173345





(4) 관측치별 변수별 예측모델 결과에 대한 기여도 분석 (contribution per each variables from specific observation)


아래 예에서는 전체 836개의 test set 관측치 중에서 '1번 관측치 (1st observation)' 의 8개 변수별 기여도를 분석해보겠습니다. 



X_test.shape

[Out]: (836, 8)


# get 1st observation's value as an example

X_i = X_test.iloc[0, :]

X_i

[Out]:

const           1.000
sex_m           0.000
sex_f           0.000
length          0.210
diameter        0.150
height          0.055
shell_weight    0.013
rings           4.000
Name: 3473, dtype: float64




관측치별 변수별 기여도(민감도, variable's contribution & sensitivity) 분석의 핵심 개념은 전체 데이터를 사용해서 적합된 모델에 특정 관측치의 변수별 값을 변수별로 순서대로 돌아가면서 해당 변수 측정값은 그대로 사용하고 나머지 변수들의 값은 '0'으로 대체해서 예측을 해보는 것입니다. 아래 코드의 for i, j in enumerate(X_i): X_mat[i, i] = j 부분을 유심히 살펴보시기 바랍니다. 



# all zeros matrix with shape of [col_num, col_num]

X_mat = np.zeros(shape=[X_i.shape[0], X_i.shape[0]])

X_mat

[Out]:
array([[0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0.]])


# fill only 1 variable's value and leave '0' for the others

for i, j in enumerate(X_i):

    X_mat[i, i] = j


X_mat

[Out]:
array([[1.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.21 , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.15 , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.   , 0.055, 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.013, 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 0.   , 4.   ]])




바로 위에서 만든 X_mat 행렬 (1번 관측치의 각 변수별로 하나씩 실제 값을 사용하고, 나머지는 '0'으로 대체)을 사용해서 (3)에서 적합한 선형회귀모형으로 y값을 예측(lin_reg.predict(X_mat))을 하면 우리가 원하는 '1번 관측치의 개별 변수별 y값 예측에 대한 기여도'를 구할 수 있습니다. 


개별 변수별 y 예측값에 대한 기여도를 기준으로 내림차순 정렬해서 DataFrame을 만들고, 가로로 누운 막대그래프도 그려보았습니다. 


# sensitivity analysis

sensitivity_df = pd.DataFrame({'x': X_test.iloc[0, :]

                               , 'contribution_x': lin_reg.predict(X_mat)}).\

                 sort_values(by='contribution_x', ascending=False)


sensitivity_df

[Out]:

xcontribution_x
length0.2100.263315
shell_weight0.0130.031699
height0.0550.021341
diameter0.1500.011237
sex_m0.0000.000000
sex_f0.0000.000000
rings4.000-0.061437
const1.000-0.360857



horizontal bar plot by column's contribution

sensitivity_df['contribution_x'].plot(kind='barh', figsize=(10, 5))

plt.title('Sensitivity Analysis', fontsize=16)

plt.xlabel('Contribution', fontsize=16)

plt.ylabel('Variable', fontsize=16)

plt.yticks(fontsize=14)

plt.show()


물론, 당연하게 관측치별 변수별 기여도 (민감도) 분석에서 나온 결과를 전부 다 합치면 애초의 모형에 해당 관측치의 전체 변수별 값을 넣어서 예측한 값과 동일한 결과가 나옵니다. 



# result from sum of contribution analysis

sum(sensitivity_df['contribution_x'])

[Out]: -0.09470251191012563


# result from linear regression model's prediction

lin_reg.predict(X_test.iloc[0, :].to_numpy())

[Out]: array([-0.09470251])

 




(5) 관측치별 변수별 예측 기여도 (민감도) 분석을 위한 사용자 정의 함수


관측치별 변수별 기여도 분석 결과를 --> pandas DataFrame으로 저장하고, --> 기여도별로 정렬된 값을 기준으로 옆으로 누운 막대그래프를 그려주는 사용자 정의함수를 만들어보겠습니다. 



# UDF for contribution(sensitivity) analysis per each variables

def sensitivity_analysis(model, X, idx, bar_plot_yn):

    

    import numpy as np

    import pandas as pd

    import matplotlib.pyplot as plt

    import statsmodels.api as sm

    pd.options.mode.chained_assignment = None

    

    # get one object's X values

    X_i = X.iloc[idx, :]

    

    # make a matrix with zeros with shape of [num_cols, num_cols]

    X_mat = np.zeros(shape=[X_i.shape[0], X_i.shape[0]])

    

    # fil X_mat with values from one by one columns, leaving the ohters zeros

    for i, j in enumerate(X_i):

        X_mat[i, i] = j

    

    # data frame with contribution of each X columns in descending order

    sensitivity_df = pd.DataFrame({'idx': idx

                                   , 'x': X_i

                                   , 'contribution_x': model.predict(X_mat)}).\

                    sort_values(by='contribution_x', ascending=True)

    

    # if bar_plot_yn == True then display it

    col_n = X_i.shape[0]

    if bar_plot_yn == True:

        sensitivity_df['contribution_x'].plot(kind='barh', figsize=(10, 0.7*col_n))

        plt.title('Sensitivity Analysis', fontsize=18)

        plt.xlabel('Contribution', fontsize=16)

        plt.ylabel('Variable', fontsize=16)

        plt.yticks(fontsize=14)

        plt.show()

    

    return sensitivity_df



# check UDF

sensitivity_df = sensitivity_analysis(model=lin_reg, X=X_test, idx=0, bar_plot_yn=True)

sensitivity_df




아래는 위에서 정의한 sensitivity_analysis() 사용자 정의 함수에서 bar_plot_yn=False 로 설정을 해서 옆으로 누운 막대그래프는 그리지 말고 기여도 분석 결과 DataFrame만 반환하라고 한 경우입니다. 



# without bar plot (bar_plot_yn=False)

sensitivity_df = sensitivity_analysis(model=lin_reg, X=X_test, idx=0, bar_plot_yn=False)

sensitivity_df

[Out]:

idxxcontribution_x
length00.2100.263315
shell_weight00.0130.031699
height00.0550.021341
diameter00.1500.011237
sex_m00.0000.000000
sex_f00.0000.000000
rings04.000-0.061437
const01.000-0.360857





(6) 다수의 관측치에 대해 '개별 관측치별 변수별 예측 기여도 분석'


위에서 부터 10개의 관측치를 선별해서, 개별 관측치별 각 변수별로 위의 (5)번에서 정의한 sensitivity_analysis() 사용자정의함수와 for loop 반복문을 사용해서 변수 기여도를 분석해보겠습니다. (단, 변수 기여도 막대 그래프는 생략)



# calculate sensitivity of each columns of the first 10 objects using for loop


# blank DataFrame to save the sensitivity results together

sensitivity_df_all = pd.DataFrame()

to_idx = 10


for idx in range(0, to_idx):

    sensitivity_df_idx = sensitivity_analysis(model=lin_reg

                                              , X=X_test

                                              , idx=idx

                                              , bar_plot_yn=False)

    

    sensitivity_df_all = pd.concat([sensitivity_df_all, sensitivity_df_idx], axis=0)

    

    print("[STATUS]", idx+1, "/", to_idx, "(", 100*(idx+1)/to_idx, "%) is completed...")


[STATUS] 1 / 10 ( 10.0 %) is completed...
[STATUS] 2 / 10 ( 20.0 %) is completed...
[STATUS] 3 / 10 ( 30.0 %) is completed...
[STATUS] 4 / 10 ( 40.0 %) is completed...
[STATUS] 5 / 10 ( 50.0 %) is completed...
[STATUS] 6 / 10 ( 60.0 %) is completed...
[STATUS] 7 / 10 ( 70.0 %) is completed...
[STATUS] 8 / 10 ( 80.0 %) is completed...
[STATUS] 9 / 10 ( 90.0 %) is completed...
[STATUS] 10 / 10 ( 100.0 %) is completed...

 



결과를 한번 확인해볼까요?



sensitivity_df_all[:20]

[Out]:

idxxcontribution_x
length00.21000.263315
shell_weight00.01300.031699
height00.05500.021341
diameter00.15000.011237
sex_m00.00000.000000
sex_f00.00000.000000
rings04.0000-0.061437
const01.0000-0.360857
length10.26000.326009
shell_weight10.03050.074372
height10.07000.027161
diameter10.20500.015357
sex_m10.00000.000000
sex_f10.00000.000000
rings14.0000-0.061437
const11.0000-0.360857
length20.52000.652018
shell_weight20.18400.448668
height20.11000.042682
diameter20.41000.030713




 2. 로지스틱 회귀모형에 대한 관측치별 변수별 민감도 분석

   (Sensitivity analysis for Logistic Regression model) 


제가 서두에서 민감도분석에 어떤 모델도 가능하도고 말씀드렸습니다. 그러면 이번에는 같은 abalone 데이터셋에 대해 목표변수로 전체무게(whole weight)가 평균보다 크면 '1', 평균보다 작으면 '0' 으로 범주를 구분한 후에, 이를 이진 분류(binary classification)할 수 있는 로지스틱 회귀모형(Logistic Regression)을 적합한 후에 --> 민감도 분석을 적용해보겠습니다. 


먼저, y_cat 이라는 범주형 변수를 만들고, train/ test set으로 분할을 한 후에, 로지스틱 회귀모형(Logistic Regression)을 적합후, 이진분류를 할 수 있는 확률을 계산(확률이 0.5보다 크면 '1'로 분류)해보겠습니다. 



# make a y_category variable: if y is greater or equal to mean, then 1

y_cat = np.where(abalone["whole_weight"] >= np.mean(abalone["whole_weight"]), 1, 0)

y_cat[:20]

[Out]: array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])


cat_class, counts = np.unique(y_cat, return_counts=True)

dict(zip(cat_class, counts))

[Out]: {0: 2178, 1: 1999}


# train, test set split

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, 

                                                    y_cat, 

                                                    test_size=0.2, 

                                                    random_state=2004)


# fitting logistic regression

import statsmodels.api as sm

pd.options.mode.chained_assignment = None


logitreg = sm.Logit(y_train, X_train)

logitreg_fit = logitreg.fit()


print(logitreg_fit.summary())

[Out]:

Optimization terminated successfully. Current function value: 0.108606 Iterations 11 Logit Regression Results ============================================================================== Dep. Variable: y No. Observations: 3341 Model: Logit Df Residuals: 3333 Method: MLE Df Model: 7 Date: Fri, 17 Jan 2020 Pseudo R-squ.: 0.8431 Time: 21:00:24 Log-Likelihood: -362.85 converged: True LL-Null: -2313.0 Covariance Type: nonrobust LLR p-value: 0.000 ============================================================================= coef std err z P>|z| [0.025 0.975] -------------------------------------------------------------------------------- const -35.3546 2.219 -15.935 0.000 -39.703 -31.006 sex_m 1.3064 0.250 5.217 0.000 0.816 1.797 sex_f 1.1418 0.258 4.420 0.000 0.635 1.648 length 36.6625 5.348 6.855 0.000 26.180 47.145 diameter 11.7960 6.160 1.915 0.055 -0.277 23.869 height 7.6179 2.011 3.789 0.000 3.677 11.559 shell_weight 38.0930 3.286 11.593 0.000 31.653 44.533 rings -0.1062 0.038 -2.777 0.005 -0.181 -0.031 =============================================================================


# prediction

test_prob_logitreg = logitreg_fit.predict(X_test)

test_prob_logitreg.head()

[Out]:
3473    9.341757e-12
3523    2.440305e-10
1862    1.147617e-02
2966    9.999843e-01
659     9.964952e-01
dtype: float64




다음으로 위의 선형회귀모형에 대한 민감도 분석 사용자정의 함수를 재활용하여 로지스틱 회귀모형에도 사용가능하도록 일부 수정해보았습니다. 


아래의 사용자 정의 함수는 로지스틱 회귀모형 적합 시 statsmodels 라이브러리를 사용한 것으로 가정하고 작성하였습니다. 만약 로지스틱 회귀모형의 model 적합에 from sklearn.linear_model import LogisticRegression 의 라이브러리를 사용하였다면 '#'으로 비활성화해놓은 부분을 해제하여 사용하면 됩니다. (predict_proba(X_mat)[:, 1] 의 부분이 달라서 그렇습니다)



# UDF for contribution(sensitivity) analysis per each variables

# task: "LinearReg" or "LogitReg"

def sensitivity_analysis_LinearReg_LogitReg(task, model, X, idx, bar_plot_yn):

    

    import numpy as np

    import pandas as pd

    import matplotlib.pyplot as plt

    import statsmodels.api as sm

    pd.options.mode.chained_assignment = None

    

    # get one object's X values

    X_i = X.iloc[idx, :]

    

    # make a matrix with zeros with shape of [num_cols, num_cols]

    X_mat = np.zeros(shape=[X_i.shape[0], X_i.shape[0]])

    

    # fil X_mat with values from one by one columns, leaving the ohters zeros

    for i, j in enumerate(X_i):

        X_mat[i, i] = j

        

    # data frame with contribution of each X columns in descending order

    sensitivity_df = pd.DataFrame({

        'idx': idx

        , 'task': task

        , 'x': X_i

        , 'contribution_x': model.predict(X_mat)     

    })

    

#     # ==== Remark =====

#     # if you used LogisticRegressionsklearn from sklearn.linear_model

#     # then use codes below

#     if task == "LinearReg":

#         sensitivity_df = pd.DataFrame({

#             'idx': idx

#             , 'task': task

#             , 'x': X_i

#             , 'contribution_x': model.predict(X_mat) 

#         })

        

#     elif task == "LogitReg":

#         sensitivity_df = pd.DataFrame({

#             'idx': idx

#             , 'task': task

#             , 'x': X_i

#             , 'contribution_x': model.predict_proba(X_mat)[:,1] 

#         })

#     else:

#         print('Please choose task one of "LinearReg" or "LogitReg"...')

    

    

    sensitivity_df = sensitivity_df.sort_values(by='contribution_x', ascending=True)

    

    # if bar_plot_yn == True then display it

    col_n = X_i.shape[0]

    if bar_plot_yn == True:

        sensitivity_df['contribution_x'].plot(kind='barh', figsize=(10, 0.7*col_n))

        plt.title('Sensitivity Analysis', fontsize=18)

        plt.xlabel('Contribution', fontsize=16)

        plt.ylabel('Variable', fontsize=16)

        plt.yticks(fontsize=14)

        plt.show()

    

    return sensitivity_df.sort_values(by='contribution_x', ascending=False)



# apply sensitivity analysis function on 1st observation for Logistic Regression

sensitivity_analysis_LinearReg_LogitReg(task="LogitReg"

                                        , model=logitreg_fit

                                        , X=X_test

                                        , idx=0

                                        , bar_plot_yn=True)




많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 

 - 시계열의 구성 요인 (time series component factors) (https://rfriend.tistory.com/509)과 

 - 시계열 분해 (time series decomposition) (https://rfriend.tistory.com/510

에 대해서 소개하였습니다. 

 

이번 포스팅에서는 직관적이어서 이해하기 쉽고 (ARIMA 대비 상대적으로요), 또 시계열 자료의 구성요소가 변동이 느리거나 매우 규칙적(periodic)일 때 예측 정확도도 높아서 시계열 자료 예측에 실무적으로 많이 사용하는 지수 평활법 (exponential smoothing) 에 대해서 소개하겠습니다. 

 

지수 평활법의 기본 개념은 "최근 관측한 값에 높은 가중치를 주고, 먼 과거의 관측치에는 낮은 관측치를 주어 가중 평균값을 구한는 것" 입니다. 이때 가중치는 현재로 부터 과거로 갈 수록 지수적으로 감소(exponential decay)시켜서 차등적으로 주는 평활 함수(smoothing function)을 사용하는 것에서 "지수(exponential)" 이름을 따왔습니다. 그리고 여러개의 관측치를 모아서 가중 평균을 사용하기 때문에 "평활(smoothing)"되는 효과가 있어서, 이 둘을 합쳐서 "지수 평활법(exponential smoothing)"이라고 하는 것입니다. 

 

 

[ 지수 분포의 확률밀도함수 곡선 ]

probability density function of exponential distribution

 

이전 Python 으로 차수 m인 단순이동평균(simple moving average) 구하는 방법을 포스팅한적이 있는데요, 단순이동평균은 가중치가 1/m 로 모두 같은 형태인 (m차 관측치만 이동해서 고려하는) 지수 평활법의 특수한 경우로 볼 수도 있겠습니다. 

 

지수 평활법의 기법을 결정하는데는 시도표 (time series plot)를 그려보고 아래의 3가지를 고려합니다. 

 

 

[ 시계열 특성에 따른 지수평활법 기법 결정 ]

 

(1) 시계열 자료에 추세(Trend)가 있는지, 추세가 있다면 1차 선형인지 아니면 비선형 인가? 

    - 추세 없음 (No Trend)                  --> Simple Exponential Smoothing

    - 1차 선형 추세 (Linear Trend)         --> Two Parameter Exponential Smoothing

    - 2차 비선형 추세 (Quadratic Trend) --> Three Parameter Smoothing

 

(2) 시계열 자료에 계절성(Seasonality)이 있는가? 

    - 계절성 없음 (No Seasonality)

    - 계정성 있음 (with Seasonality)  --> Winters' Method

 

(3) 시계열 자료의 계절성이 시간이 지남에 따라 고정(fixed)되어 있는지 아니면 확산(increasing)되는가? 

    - 고정(상수) 계절 변동 (fixed seasonal variation) --> Additive Model

    - 확산 계절 변동 (increasing seasonal variation)  --> Multiplicative Model

 

 

위의 설명을 각 시계열 자료 패턴별로 시도표 예시와 함께 decision tree 형태로 구분해서 지수 평활법 (exponential smoothing) 기법을 짝지어보면 아래와 같습니다. 

 

 

물론 위와 같이 분석가가 눈으로 시도표를 보고 나서 적합한 지수 평활법 기법을 선택하는 것이 아니라, 분석 시스템 성능이 뒷받침이 되고 분석 시간이 오래 소요되어도 문제가 없다면, 혹은 사람 개입 없이 자동화를 하고자 한다면 모든 지수 평활법 기법을 적용해보고 이들 모델 긴 적합도 평가 통계량(가령, RMSE, MAPE, MAE 등)을 비교해 본 후 적합이 가장 잘 된 것을 선택할 수도 있겠습니다.

 

 

[ 지수 평활법 기법 별 수식 ]

 

를 지수 평활법으로 예측하고자 하는 t 시점의 시계열 값이라고 하고, 

를 t 시점의 계절 요소값, 

를 t 시점에서의 오차라고 했을 때, 각 지수평활법 기법별로 모형의 수식을 표기해보면 아래와 같습니다. 지수평활법 기법 간 수식을 비교해보면 좀더 잘 이해할 수 있을 것입니다. 

 

  • 추세와 계절성이 모두 없는 단순 지수 평활법
    (Simple Exponential Smoothing)



  • 추세는 없고 고정계절변동이 있는 가법 윈터스 방법
     (Additive Winters' Method Exponential Smoothing with No Trend)



  • 추세는 없고 승법 윈터스 방법
    (Multiplicative Winters' Method Exponential Smoothing with No Trend)



  • 1차 선형 추세는 있고 계절성은 없이중 지수 평활법
    (Two Parameter Exponential Smoothing)



  • 1차 선형 추세와 고정계절변동이 있는 가법 윈터스 지수 평활법
    (Additive Winters' Method Exponential Smoothing with Linear Trend)



  • 1차 선형 추세와 확산계절변동이 있는 승법 원터스 지수 평활법
    (Multiplicative Winters' Method Exponential Smoothing with Linear Trend)



  • 2차 비선형 추세는 있고 계절성은 없는 삼중 지수 평활법
    (Three Parameter Exponential Smoothing)



  • 2차 비선형 추세와 고정계절변동이 있는 가법 윈터스 지수 평활법
    (Additive Winters' Method Exponential Smoothing with Quadratic Trend)



  • 2차 비선형 추세와 확산계절변동이 있는 승법 윈터스 지수 평활법
    (Multiplicative Winters' Method Exponential Smoothing with Quadratic Trend)

    : 

 

다음번 포스팅에서는 시계열 예측 모형의 적합도를 평가하는 지표(goodness-of-fit measures for time series models) (https://rfriend.tistory.com/667) 를 살펴보고, 이어서 Python 으로 단순 지수 평활법으로 시계열 자료를 예측하고 모델 성능을 비교평가하는 방법(https://rfriend.tistory.com/671)을 소개하겠습니다. 

 

많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~

'를 꾹 눌러주세요. :-)

 

 

728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 시계열 자료의 구성 요인 (time series component factors)으로서 추세 요인, 순환 요인, 계절 요인, 불규칙 요인에 대해서 소개하였습니다. 

 

지난번 포스팅에서는 가법 모형으로 가상의 시계열 자료를 만들었다면(time series composition), ==> 이번 포스팅에서는 반대로 시계열 분해(time series decomposition)를 통해 시계열 자료를 추세(순환)(Trend), 계절성(Seasonality), 잔차(Residual)로 분해를 해보겠습니다. 

 

시계열 분해는 직관적으로 이해하기 쉽고 구현도 쉬워서 시계열 자료 분석의 고전적인 방법론이지만 지금까지도 꾸준히 사용되는 방법론입니다. 시계열 분해의 순서와 방법은 대략 아래와 같습니다. 

 

(1) 시도표 (time series plot)를 보고 시계열의 주기적 반복/계절성이 있는지, 가법 모형(additive model, y = t + s + r)과 승법 모형(multiplicative model, y = t * s * r) 중 무엇이 더 적합할지 판단을 합니다. 

 

(가법 모형을 가정할 시)

(2) 시계열 자료에서 추세(trend)를 뽑아내기 위해서 중심 이동 평균(centered moving average)을 이용합니다. 

 

(3) 원 자료에서 추세 분해값을 빼줍니다(detrend). 그러면 계절 요인과 불규칙 요인만 남게 됩니다. 

 

(4) 다음에 계절 주기 (seasonal period) 로 detrend 이후 남은 값의 합을 나누어주면 계절 평균(average seasonality)을 구할 수 있습니다. (예: 01월 계절 평균 = (2020-01 + 2021-01 + 2022-01 + 2023-01)/4, 02월 계절 평균 = (2020-02 + 2021-02 + 2022-02 + 2023-02)/4). 

 

(5) 원래의 값에서 추세와 계절성 분해값을 빼주면 불규칙 요인(random, irregular factor)이 남게 됩니다. 

 

 

시계열 분해 후에 추세와 계절성을 제외한 잔차(residual, random/irregular factor) 가 특정 패턴 없이 무작위 분포를 띠고 작은 값이면 추세와 계절성으로 모형화가 잘 되는 것이구요, 시계열 자료의 특성을 이해하고 예측하는데 활용할 수 있습니다. 만약 시계열 분해 후의 잔차에 특정 패턴 (가령, 주기적인 파동을 그린다거나, 분산이 점점 커진다거나 등..) 이 존재한다면 잔차에 대해서만 다른 모형을 추가로 적합할 수도 있겠습니다. 

 

 

예제로 사용할 시계열 자료로서 '1차 선형 추세 + 4년 주기 순환 + 1년 단위 계절성 + 불규칙 noise' 의 가법 모형 (additive model)으로 시계열 데이터를 만들어보겠습니다. 

 



import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


dates = pd.date_range('2020-01-01', periods=48, freq='M')
dates
[Out]:
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30', '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31', '2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30', '2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31', '2021-09-30', '2021-10-31', '2021-11-30', '2021-12-31', '2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30', '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31', '2022-09-30', '2022-10-31', '2022-11-30', '2022-12-31', '2023-01-31', '2023-02-28', '2023-03-31', '2023-04-30', '2023-05-31', '2023-06-30', '2023-07-31', '2023-08-31', '2023-09-30', '2023-10-31', '2023-11-30', '2023-12-31'], dtype='datetime64[ns]', freq='M')


# additive model: trend + cycle + seasonality + irregular factor


timestamp = np.arange(len(dates))
trend_factor = timestamp*1.1
cycle_factor = 10*np.sin(np.linspace(0, 3.14*2, 48))
seasonal_factor = 7*np.sin(np.linspace(0, 3.14*8, 48))
np.random.seed(2004)
irregular_factor = 2*np.random.randn(len(dates))


df = pd.DataFrame({'timeseries': trend_factor + cycle_factor + seasonal_factor + irregular_factor, 
                   'trend': trend_factor, 
                   'cycle': cycle_factor, 
                   'trend_cycle': trend_factor + cycle_factor,
                   'seasonal': seasonal_factor, 
                   'irregular': irregular_factor},
                   index=dates)


df
[Out]:
  timeseries trend cycle trend_cycle seasonal irregular
2020-01-31 2.596119 0.0 0.000000 0.000000 0.000000 2.596119
2020-02-29 6.746160 1.1 1.332198 2.432198 3.565684 0.748278
2020-03-31 8.112100 2.2 2.640647 4.840647 6.136825 -2.865371
2020-04-30 8.255941 3.3 3.902021 7.202021 6.996279 -5.942358
2020-05-31 16.889655 4.4 5.093834 9.493834 5.904327 1.491495
2020-06-30 16.182357 5.5 6.194839 11.694839 3.165536 1.321981
2020-07-31 14.128087 6.6 7.185409 13.785409 -0.456187 0.798865
2020-08-31 11.943313 7.7 8.047886 15.747886 -3.950671 0.146099
2020-09-30 9.728095 8.8 8.766892 17.566892 -6.343231 -1.495567
2020-10-31 12.483489 9.9 9.329612 19.229612 -6.966533 0.220411
2020-11-30 12.141808 11.0 9.726013 20.726013 -5.646726 -2.937480
2020-12-31 15.143334 12.1 9.949029 22.049029 -2.751930 -4.153764
2021-01-31 21.774516 13.2 9.994684 23.194684 0.910435 -2.330604
2021-02-28 28.432892 14.3 9.862164 24.162164 4.318862 -0.048134
2021-03-31 32.350583 15.4 9.553832 24.953832 6.522669 0.874082
2021-04-30 30.596556 16.5 9.075184 25.575184 6.907169 -1.885797
2021-05-31 32.510523 17.6 8.434753 26.034753 5.365118 1.110653
2021-06-30 30.425519 18.7 7.643955 26.343955 2.326624 1.754939
2021-07-31 24.300958 19.8 6.716890 26.516890 -1.360813 -0.855119
2021-08-31 20.450917 20.9 5.670082 26.570082 -4.668691 -1.450475
2021-09-30 18.870881 22.0 4.522195 26.522195 -6.674375 -0.976939
2021-10-31 21.326310 23.1 3.293690 26.393690 -6.818438 1.751059
2021-11-30 22.902448 24.2 2.006469 26.206469 -5.060699 1.756678
2021-12-31 26.620578 25.3 0.683478 25.983478 -1.891426 2.528526
2022-01-31 27.626499 26.4 -0.651696 25.748304 1.805404 0.072791
2022-02-28 31.858923 27.5 -1.975253 25.524747 4.998670 1.335506
2022-03-31 35.930469 28.6 -3.263598 25.336402 6.797704 3.796363
2022-04-30 30.177870 29.7 -4.493762 25.206238 6.700718 -1.729087
2022-05-31 30.016165 30.8 -5.643816 25.156184 4.734764 0.125217
2022-06-30 26.591729 31.9 -6.693258 25.206742 1.448187 -0.063200
2022-07-31 21.118481 33.0 -7.623379 25.376621 -2.242320 -2.015820
2022-08-31 16.636031 34.1 -8.417599 25.682401 -5.307397 -3.738973
2022-09-30 17.682613 35.2 -9.061759 26.138241 -6.892132 -1.563496
2022-10-31 21.163298 36.3 -9.544375 26.755625 -6.554509 0.962182
2022-11-30 22.455672 37.4 -9.856844 27.543156 -4.388699 -0.698786
2022-12-31 26.919529 38.5 -9.993595 28.506405 -0.998790 -0.588086
2023-01-31 33.964623 39.6 -9.952191 29.647809 2.669702 1.647112
2023-02-28 37.459776 40.7 -9.733369 30.966631 5.593559 0.899586
2023-03-31 40.793766 41.8 -9.341031 32.458969 6.957257 1.377540
2023-04-30 43.838415 42.9 -8.782171 34.117829 6.380433 3.340153
2023-05-31 41.301780 44.0 -8.066751 35.933249 4.023975 1.344556
2023-06-30 39.217866 45.1 -7.207526 37.892474 0.545147 0.780245
2023-07-31 35.125502 46.2 -6.219813 39.980187 -3.085734 -1.768952
2023-08-31 33.841926 47.3 -5.121219 42.178781 -5.855940 -2.480916
2023-09-30 38.770511 48.4 -3.931329 44.468671 -6.992803 1.294643
2023-10-31 37.371216 49.5 -2.671356 46.828644 -6.179230 -3.278198
2023-11-30 46.587633 50.6 -1.363760 49.236240 -3.642142 0.993536
2023-12-31 46.403326 51.7 -0.031853 51.668147 -0.089186 -5.175634

 

 

 

  (1) Python을 이용한 시계열 분해 (Time series decomposition using Python)

 

Python의 statsmodels 라이브러리를 사용해서 가법 모형(additive model) 가정 하에 시계열 분해를 해보겠습니다. 

 



from statsmodels.tsa.seasonal import seasonal_decompose


ts = df.timeseries
result = seasonal_decompose(ts, model='additive')


plt.rcParams['figure.figsize'] = [12, 8]
result.plot()
plt.show()



 

 

 

원래의 시계열 구성요소(추세+순환, 계절성, 불규칙 요인)와 시계열 분해(time series decomposition)를 통해 분리한 추세(&순환), 계절성, 잔차(불규칙 요인)를 겹쳐서 그려보았습니다. (즉, 원래 데이터의 추세요인과 시계열 분해를 통해 분리한 추세를 겹쳐서 그려보고, 원래 데이터의 계절요인과 시계열 분해를 통해 분리한 계절을 겹쳐서 그려보고, 원래 데이터의 불규칙 요인과 시계열 분해를 통해 분리한 잔차를 겹쳐서 그려봄) 

 

원래의 데이터와 얼추 비슷하게, 그럴싸하게 시계열 분해를 한 것처럼 보이지요?   

 



# ground truth & timeseries decompostion all together
# -- observed data
plt.figure(figsize=(12, 12))
plt.subplot(4,1, 1)
result.observed.plot()
plt.grid(True)
plt.ylabel('Observed', fontsize=14)


# -- trend & cycle factor
plt.subplot(4, 1, 2)
result.trend.plot()        # from timeseries decomposition
df.trend_cycle.plot()     # ground truth
plt.grid(True)
plt.ylabel('Trend', fontsize=14)


# -- seasonal factor
plt.subplot(4, 1, 3)
result.seasonal.plot()  # from timeseries decomposition
df.seasonal.plot()        # ground truth
plt.grid(True)
plt.ylabel('Seasonality', fontsize=14)


# -- irregular factor (noise)
plt.subplot(4, 1, 4)
result.resid.plot()    # from timeseries decomposition
df.irregular.plot()    # ground truth
plt.grid(True)
plt.ylabel('Residual', fontsize=14)


plt.show()

 

 

 

원래의 관측치(observed), 추세(trend), 계절성(seasonal), 잔차(residual) 데이터 아래처럼 시계열 분해한 객체에서 obsered, trend, seasonal, resid 라는 attributes 를 통해서 조회할 수 있습니다. 

 

Observed  Trend ( & Cycle)
 print(result.observed)
[Out]:
2020-01-31 2.596119
2020-02-29 6.746160
2020-03-31 8.112100
2020-04-30 8.255941
2020-05-31 16.889655
2020-06-30 16.182357
2020-07-31 14.128087
2020-08-31 11.943313
2020-09-30 9.728095
2020-10-31 12.483489
2020-11-30 12.141808
2020-12-31 15.143334
2021-01-31 21.774516
2021-02-28 28.432892
2021-03-31 32.350583
2021-04-30 30.596556
2021-05-31 32.510523
2021-06-30 30.425519
2021-07-31 24.300958
2021-08-31 20.450917
2021-09-30 18.870881
2021-10-31 21.326310
2021-11-30 22.902448
2021-12-31 26.620578
2022-01-31 27.626499
2022-02-28 31.858923
2022-03-31 35.930469
2022-04-30 30.177870
2022-05-31 30.016165
2022-06-30 26.591729
2022-07-31 21.118481
2022-08-31 16.636031
2022-09-30 17.682613
2022-10-31 21.163298
2022-11-30 22.455672
2022-12-31 26.919529
2023-01-31 33.964623
2023-02-28 37.459776
2023-03-31 40.793766
2023-04-30 43.838415
2023-05-31 41.301780
2023-06-30 39.217866
2023-07-31 35.125502
2023-08-31 33.841926
2023-09-30 38.770511
2023-10-31 37.371216
2023-11-30 46.587633
2023-12-31 46.403326
Freq: M, Name: timeseries,

dtype: float64
print(result.trend)
[Out]
2020-01-31 NaN
2020-02-29 NaN
2020-03-31 NaN
2020-04-30 NaN
2020-05-31 NaN
2020-06-30 NaN
2020-07-31 11.994971
2020-08-31 13.697685
2020-09-30 15.611236
2020-10-31 17.552031
2020-11-30 19.133760
2020-12-31 20.378094
2021-01-31 21.395429
2021-02-28 22.173782
2021-03-31 22.909215
2021-04-30 23.658616
2021-05-31 24.475426
2021-06-30 25.402005
2021-07-31 26.124056
2021-08-31 26.510640
2021-09-30 26.802553
2021-10-31 26.934270
2021-11-30 26.812893
2021-12-31 26.549220
2022-01-31 26.256876
2022-02-28 25.965319
2022-03-31 25.756854
2022-04-30 25.700551
2022-05-31 25.675143
2022-06-30 25.668984
2022-07-31 25.945528
2022-08-31 26.442986
2022-09-30 26.878992
2022-10-31 27.650819
2022-11-30 28.690242
2022-12-31 29.686565
2023-01-31 30.796280
2023-02-28 32.096818
2023-03-31 33.692393
2023-04-30 35.246385
2023-05-31 36.927214
2023-06-30 38.744537
2023-07-31 NaN
2023-08-31 NaN
2023-09-30 NaN
2023-10-31 NaN
2023-11-30 NaN
2023-12-31 NaN 

Freq: M, Name: timeseries,
dtype: float64 

 

 

 Seasonality   Residual (Noise)
print(result.seasonal)
[Out]:
2020-01-31 1.501630
2020-02-29 5.701170
2020-03-31 8.768065
2020-04-30 6.531709
2020-05-31 5.446174
2020-06-30 2.002476
2020-07-31 -1.643064
2020-08-31 -6.011071
2020-09-30 -7.807785
2020-10-31 -5.858728
2020-11-30 -5.849710
2020-12-31 -2.780867
2021-01-31 1.501630
2021-02-28 5.701170
2021-03-31 8.768065
2021-04-30 6.531709
2021-05-31 5.446174
2021-06-30 2.002476
2021-07-31 -1.643064
2021-08-31 -6.011071
2021-09-30 -7.807785
2021-10-31 -5.858728
2021-11-30 -5.849710
2021-12-31 -2.780867
2022-01-31 1.501630
2022-02-28 5.701170
2022-03-31 8.768065
2022-04-30 6.531709
2022-05-31 5.446174
2022-06-30 2.002476
2022-07-31 -1.643064
2022-08-31 -6.011071
2022-09-30 -7.807785
2022-10-31 -5.858728
2022-11-30 -5.849710
2022-12-31 -2.780867
2023-01-31 1.501630
2023-02-28 5.701170
2023-03-31 8.768065
2023-04-30 6.531709
2023-05-31 5.446174
2023-06-30 2.002476
2023-07-31 -1.643064
2023-08-31 -6.011071
2023-09-30 -7.807785
2023-10-31 -5.858728
2023-11-30 -5.849710
2023-12-31 -2.780867
Freq: M, Name: timeseries,

dtype: float64
print(result.resid)
[Out]:
2020-01-31 NaN
2020-02-29 NaN
2020-03-31 NaN
2020-04-30 NaN
2020-05-31 NaN
2020-06-30 NaN
2020-07-31 3.776179
2020-08-31 4.256699
2020-09-30 1.924644
2020-10-31 0.790186
2020-11-30 -1.142242
2020-12-31 -2.453893
2021-01-31 -1.122544
2021-02-28 0.557940
2021-03-31 0.673303
2021-04-30 0.406231
2021-05-31 2.588922
2021-06-30 3.021039
2021-07-31 -0.180034
2021-08-31 -0.048653
2021-09-30 -0.123887
2021-10-31 0.250769
2021-11-30 1.939265
2021-12-31 2.852225
2022-01-31 -0.132007
2022-02-28 0.192434
2022-03-31 1.405550
2022-04-30 -2.054390
2022-05-31 -1.105152
2022-06-30 -1.079730
2022-07-31 -3.183983
2022-08-31 -3.795884
2022-09-30 -1.388594
2022-10-31 -0.628793
2022-11-30 -0.384861
2022-12-31 0.013830
2023-01-31 1.666713
2023-02-28 -0.338212
2023-03-31 -1.666692
2023-04-30 2.060321
2023-05-31 -1.071608
2023-06-30 -1.529146
2023-07-31 NaN
2023-08-31 NaN
2023-09-30 NaN
2023-10-31 NaN
2023-11-30 NaN
2023-12-31 NaN 

Freq: M, Name: timeseries,
dtype: float64 

 

 


# export to csv file
df.to_csv('ts_components.txt', sep=',', index=False)
 

 

 

 

 

  (2) R을 이용한 시계열 분해 (Time series Decomposition using R)

 

위에서 가법 모형을 적용해서 Python으로 만든 시계열 자료를 text 파일로 내보낸 후, 이를 R에서 읽어서 시계열 분해 (time series decomposition)를 해보겠습니다. 

 

ts_components.txt
다운로드

 



# read time series text file
df <- read.table('ts_components.txt', sep=',', header=T)
head(df)
A data.frame: 6 × 6
timeseries trend cycle trend_cycle seasonal irregular
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
2.596119 0.0 0.000000 0.000000 0.000000 2.5961193
6.746160 1.1 1.332198 2.432198 3.565684 0.7482781
8.112100 2.2 2.640647 4.840647 6.136825 -2.8653712
8.255941 3.3 3.902021 7.202021 6.996279 -5.9423582
16.889655 4.4 5.093834 9.493834 5.904327 1.4914946
16.182357 5.5 6.194839 11.694839 3.165536 1.3219812
 

 

 

이렇게 불러와서 만든 df DataFrame의 칼럼 중에서 시계열 분해를 할 'timeseries' 칼럼만을 가져와서 ts() 함수를 사용하여 1년 12개월 이므로 frequency =  12로 설정해 R의 시계열 자료 형태로 변환합니다. 

 

그 다음에 decompose() 함수를 사용하여 시계열 분해를 하는데요, 이때 가법 모형 (additive model)을 적용할 것이므로 decompose(ts, "additive") 라고 설정해줍니다. 

 

시계열 분해를 한 결과를 모아놓은 리스트 ts_decompose 객체를 프린트해보면 원래의 값 $x, 계절 요인 $seasonal, 추세(&순환) 요인 $trend, 불규칙 요인 $random 분해값이 순서대로 저장되어 있음을 알 수 있습니다. 

 



# transforming data to time series with 12 months frequency
ts <- ts(df$timeseries, frequency = 12) # 12 months


# time series decomposition
ts_decompose <- decompose(ts, "additive") # additive model


# decomposition results
ts_decompose
[Out]:
$x Jan Feb Mar Apr May Jun Jul
1 2.596119 6.746160 8.112100 8.255941 16.889655 16.182357 14.128087
2 21.774516 28.432892 32.350583 30.596556 32.510523 30.425519 24.300958
3 27.626499 31.858923 35.930469 30.177870 30.016165 26.591729 21.118481
4 33.964623 37.459776 40.793766 43.838415 41.301780 39.217866 35.125502

Aug Sep Oct Nov Dec
1 11.943313 9.728095 12.483489 12.141808 15.143334
2 20.450917 18.870881 21.326310 22.902448 26.620578
3 16.636031 17.682613 21.163298 22.455672 26.919529
4 33.841926 38.770511 37.371216 46.587633 46.403326

$seasonal Jan Feb Mar Apr May Jun Jul
1 1.501630 5.701170 8.768065 6.531709 5.446174 2.002476 -1.643064
2 1.501630 5.701170 8.768065 6.531709 5.446174 2.002476 -1.643064
3 1.501630 5.701170 8.768065 6.531709 5.446174 2.002476 -1.643064
4 1.501630 5.701170 8.768065 6.531709 5.446174 2.002476 -1.643064

Aug Sep Oct Nov Dec
1 -6.011071 -7.807785 -5.858728 -5.849710 -2.780867
2 -6.011071 -7.807785 -5.858728 -5.849710 -2.780867
3 -6.011071 -7.807785 -5.858728 -5.849710 -2.780867
4 -6.011071 -7.807785 -5.858728 -5.849710 -2.780867

$trend Jan Feb Mar Apr May Jun Jul Aug
1 NA NA NA NA NA NA 11.99497 13.69769
2 21.39543 22.17378 22.90922 23.65862 24.47543 25.40200 26.12406 26.51064
3 26.25688 25.96532 25.75685 25.70055 25.67514 25.66898 25.94553 26.44299
4 30.79628 32.09682 33.69239 35.24639 36.92721 38.74454 NA NA

Sep Oct Nov Dec
1 15.61124 17.55203 19.13376 20.37809
2 26.80255 26.93427 26.81289 26.54922
3 26.87899 27.65082 28.69024 29.68657
4 NA NA NA NA

$random Jan Feb Mar Apr May Jun
1 NA NA NA NA NA NA
2 -1.12254398 0.55793990 0.67330316 0.40623129 2.58892205 3.02103851
3 -0.13200705 0.19243403 1.40555039 -2.05439035 -1.10515213 -1.07973023
4 1.66671294 -0.33821202 -1.66669164 2.06032097 -1.07160802 -1.52914637

Jul Aug Sep Oct Nov Dec
1 3.77617915 4.25669908 1.92464365 0.79018590 -1.14224232 -2.45389325
2 -0.18003389 -0.04865275 -0.12388741 0.25076875 1.93926482 2.85222467
3 -3.18398336 -3.79588443 -1.38859434 -0.62879275 -0.38486059 0.01383049
4 NA NA NA NA NA NA

$figure
[1] 1.501630 5.701170 8.768065 6.531709 5.446174 2.002476 -1.643064
[8] -6.011071 -7.807785 -5.858728 -5.849710 -2.780867

$type
[1] "additive" attr(,"class")
[1] "decomposed.ts"

 

 

 

 

위의 분해 결과가 숫자만 잔뜩 들어있으니 뭐가 뭔지 잘 눈에 안들어오지요?  그러면 이제 원래의 값 (observed)과 시계열 분해된 결과인 trend, seasonal, random 을 plot() 함수를 사용하여 다같이 시각화해보겠습니다. 

 



# change plot in jupyter
library(repr)


# Change plot size to 12 x 10
options(repr.plot.width=12, repr.plot.height=10)


plot(ts_decompose)
 

 

 

위의 분해 결과를 Trend (ts_decompose$trend), Seasonal (ts_decompose$seasonal), Random (ts_decompose$random) 의 각 요소별로 나누어서 시각화해볼 수도 있습니다. 

 



# change the plot size
options(repr.plot.width=12, repr.plot.height=5)


# Trend
plot(as.ts(ts_decompose$trend))


# Seasonality
plot(as.ts(ts_decompose$seasonal))
# Random (Irregular factor)
plot(as.ts(ts_decompose$random))


 

 

 

많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~

'를 꾹 눌러주세요. :-)

 

 

 

728x90
반응형
Posted by Rfriend
,

그동안 여러 포스팅에 나누어서 Python pandas 라이브러리에서 사용할 수 있는 시계열 데이터 처리 함수, 메소드, attributes 들에 대해서 소개했습니다. 


이번 포스팅에서는 시계열(Time series)의 4가지 구성 요인(추세 요인, 순환 요인, 계절 요인, 불규칙 요인)에 대해서 소개하겠습니다. 


시계열 구성요인 간의 결합 방식에 따라서 (1) 구성요인 간 독립적이라고 가정하여 각 구성요인을 더하는 가법 모형 (additive model)과, (2) 구성요인 간 독립적이지 않고 상호작용을 한다고 가정하여 구성요인 간 곱해주는 승법 모형 (multiplicative model)으로 구분할 수 있습니다. 


  • 시계열 가법 모형 (time series additive model) 
    = 추세 요인(trend factor) + 순환 요인(cycle factor) + 계절 요인(seasonal factor)
     + 불규칙 요인(irregular/random factor) 


  • 시계열 승법 모형 (time series multiplicative model)
    = 추세 요인 * 순환 요인 x 계절 요인 x 불규칙 요인

    :



이번 포스팅에서는 이해하기 쉬운 가법 모형(additive model)을 가상으로 만든 예제 데이터를 가지고 설명해보겠습니다. 



[ 시계열 구성 요인 (Time Series Component Factors) ]





시계열의 4가지 구성 요인인 추세 요인, 순환 요인, 계절 요인, 불규칙 요인을 차례대로 설명해보겠습니다.  

(* Reference: '한국의 경기순환 분석', 김혜원, 2004, http://kostat.go.kr/attach/journal/9-1-4.PDF)


(1) 추세 요인 (Trend factor) 은 인구의 변화, 자원의 변화, 자본재의 변화, 기술의 변화 등과 같은 요인들에 의해 영향을 받는 장기 변동 요인으로서, 급격한 충격이 없는 한 지속되는 특성이 있습니다. "10년 주기의 세계경제 변동 추세" 같은 것이 추세 요인의 예라고 할 수 있습니다. 


(2) 순환 요인 (Cycle factor) 은 경제활동의 팽창과 위축과 같이 불규칙적이며 반복적인 중기 변동요인을 말합니다. 주식투자가들이 "건설업/반도체업/조선업 순환주기"를 고려해서 투자한다고 말하는게 좋은 예입니다. 

만약 관측한 데이터셋이 10년 미만일 경우 추세 요인과 순환 요인을 구분하는 것이 매우 어렵습니다. 그래서 관측기간이 길지 않을 경우 추세와 순환 요인을 구분하지 않고 그냥 묶어서 추세 요인이라고 분석하기도 합니다. 


(3) 계절 요인 (Seasonal factor)  12개월(1년)의 주기를 가지고 반복되는 변화를 말하며, 계절의 변화, 공휴일의 반복, 추석 명절의 반복 등 과 같은 요인들에 의하여 발생합니다. 


(4) 불규칙 요인 (Irregular / Random factor, Noise) 은 일정한 규칙성을 인지할 수 없는 변동의 유형을 의미합니다. 천재지변, 전쟁, 질병 등과 같이 예 상할 수 없는 우연적 요인에 의해 발생되는 변동을 총칭합니다. 불규칙변동 은 경제활동에 미미한 영향을 미치기도 하지만 때로는 경제생활에 지대한 영향을 주기도 합니다. 



위의 설명에 대한 이해를 돕기 위하여 Python으로 위의 추세 요인, 순환 요인, 계절 요인, 불규칙 요인을 모두 더한 가법 모형의 시계열 자료(Yt = Tt + Ct + St + It)를 가상으로 만들어보겠습니다. 



import numpy as np

import pandas as pd


# DatetiemIndex

dates = pd.date_range('2020-01-01', periods=48, freq='M')

dates

[Out]:
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30',
               '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31',
               '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31',
               '2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30',
               '2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31',
               '2021-09-30', '2021-10-31', '2021-11-30', '2021-12-31',
               '2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
               '2022-05-31', '2022-06-30', '2022-07-31', '2022-08-31',
               '2022-09-30', '2022-10-31', '2022-11-30', '2022-12-31',
               '2023-01-31', '2023-02-28', '2023-03-31', '2023-04-30',
               '2023-05-31', '2023-06-30', '2023-07-31', '2023-08-31',
               '2023-09-30', '2023-10-31', '2023-11-30', '2023-12-31'],
              dtype='datetime64[ns]', freq='M')



# additive model: trend + cycle + seasonality + irregular factor

timestamp = np.arange(len(dates))

trend_factor = timestamp*1.1

cycle_factor = 10*np.sin(np.linspace(0, 3.14*2, 48))

seasonal_factor = 7*np.sin(np.linspace(0, 3.14*8, 48))

np.random.seed(2004)

irregular_factor = 2*np.random.randn(len(dates))


df = pd.DataFrame({'timeseries': trend_factor + cycle_factor + seasonal_factor + irregular_factor, 

                   'trend': trend_factor, 

                   'cycle': cycle_factor, 

                   'seasonal': seasonal_factor, 

                   'irregular': irregular_factor},

                   index=dates)


df

[Out]:

timeseriestrendcycleseasonalirregular
2020-01-312.5961190.00.0000000.0000002.596119
2020-02-296.7461601.11.3321983.5656840.748278
2020-03-318.1121002.22.6406476.136825-2.865371
2020-04-308.2559413.33.9020216.996279-5.942358
2020-05-3116.8896554.45.0938345.9043271.491495
2020-06-3016.1823575.56.1948393.1655361.321981
2020-07-3114.1280876.67.185409-0.4561870.798865
2020-08-3111.9433137.78.047886-3.9506710.146099
2020-09-309.7280958.88.766892-6.343231-1.495567
2020-10-3112.4834899.99.329612-6.9665330.220411
2020-11-3012.14180811.09.726013-5.646726-2.937480
2020-12-3115.14333412.19.949029-2.751930-4.153764
2021-01-3121.77451613.29.9946840.910435-2.330604
2021-02-2828.43289214.39.8621644.318862-0.048134
2021-03-3132.35058315.49.5538326.5226690.874082
2021-04-3030.59655616.59.0751846.907169-1.885797
2021-05-3132.51052317.68.4347535.3651181.110653
2021-06-3030.42551918.77.6439552.3266241.754939
2021-07-3124.30095819.86.716890-1.360813-0.855119
2021-08-3120.45091720.95.670082-4.668691-1.450475
2021-09-3018.87088122.04.522195-6.674375-0.976939
2021-10-3121.32631023.13.293690-6.8184381.751059
2021-11-3022.90244824.22.006469-5.0606991.756678
2021-12-3126.62057825.30.683478-1.8914262.528526
2022-01-3127.62649926.4-0.6516961.8054040.072791
2022-02-2831.85892327.5-1.9752534.9986701.335506
2022-03-3135.93046928.6-3.2635986.7977043.796363
2022-04-3030.17787029.7-4.4937626.700718-1.729087
2022-05-3130.01616530.8-5.6438164.7347640.125217
2022-06-3026.59172931.9-6.6932581.448187-0.063200
2022-07-3121.11848133.0-7.623379-2.242320-2.015820
2022-08-3116.63603134.1-8.417599-5.307397-3.738973
2022-09-3017.68261335.2-9.061759-6.892132-1.563496
2022-10-3121.16329836.3-9.544375-6.5545090.962182
2022-11-3022.45567237.4-9.856844-4.388699-0.698786
2022-12-3126.91952938.5-9.993595-0.998790-0.588086
2023-01-3133.96462339.6-9.9521912.6697021.647112
2023-02-2837.45977640.7-9.7333695.5935590.899586
2023-03-3140.79376641.8-9.3410316.9572571.377540
2023-04-3043.83841542.9-8.7821716.3804333.340153
2023-05-3141.30178044.0-8.0667514.0239751.344556
2023-06-3039.21786645.1-7.2075260.5451470.780245
2023-07-3135.12550246.2-6.219813-3.085734-1.768952
2023-08-3133.84192647.3-5.121219-5.855940-2.480916
2023-09-3038.77051148.4-3.931329-6.9928031.294643
2023-10-3137.37121649.5-2.671356-6.179230-3.278198
2023-11-3046.58763350.6-1.363760-3.6421420.993536
2023-12-3146.40332651.7-0.031853-0.089186-5.175634



위에서 추세 요인(trend factor) + 순환 요인(cycle factor) + 계절 요인(seasonal factor) + 불규칙 요인(irregular factor, noise) 을 더해서 만든 시계열 가법 모형 (time series additive model) 자료를 아래에 시각화보았습니다. 


아래의 시도표 (time series plot)를 보면 '양(+)의 1차 선형 추세 (linear trend)', '1년 단위의 계절성(seasonality)', 그리고 불규칙한 잡음(noise)을 눈으로 확인할 수 있습니다. (기간이 4년으로서 길지 않다보니 추세와 순환 요인을 구분하기는 쉽지가 않지요?)



# Time series plot

import matplotlib.pyplot as plt


plt.figure(figsize=[10, 6])

df.timeseries.plot()

plt.title('Time Series (Additive Model)', fontsize=16)

plt.ylim(-12, 55)

plt.show()




(1) 위에서 가법 모형으로 가상의 시계열 자료를 만들 때 사용했던 '1차 선형 추세 요인 (trend factor)' 데이터를 시각화하면 아래와 같습니다. 



# -- Trend factor

#timestamp = np.arange(len(dates))

#trend_factor = timestamp*1.1


plt.figure(figsize=[10, 6])

df.trend.plot()

plt.title('Trend Factor', fontsize=16)

plt.ylim(-12, 55)

plt.show()



(2) 위의 가법 모형으로 가상의 시계열 자료를 만들 때 사용했던 '4년 주기의 순환 요인 (cycle factor) '자료를 시각화하면 아래와 같습니다. 



# -- Cycle factor

#cycle_factor = 10*np.sin(np.linspace(0, 3.14*2, 48))


plt.figure(figsize=[10, 6])

df.cycle.plot()

plt.title('Cycle Factor', fontsize=16)

plt.ylim(-12, 55)

plt.show()



(3) 위에서 가법 모형으로 가상의 시계열 자료를 만들 때 사용했던 '1년 주기의 계절 요인 (seasonal factor)' 자료를 시각화하면 아래와 같습니다. 



# -- Seasonal factor

#seasonal_factor = 7*np.sin(np.linspace(0, 3.14*8, 48))


plt.figure(figsize=[10, 6])

df.seasonal.plot()

plt.title('Seasonal Factor', fontsize=16)

plt.ylim(-12, 55)

plt.show()




(4) 위에서 가법 모형으로 가상의 시계열 자료를 만들 때 사용했던 '불규칙 요인 (irregular factor)' 자료를 시각화하면 아래와 같습니다. 



# -- Irregular/ Random factor

#np.random.seed(2004)

#irregular_factor = 2*np.random.randn(len(dates))


plt.figure(figsize=[10, 6])

df.irregular.plot()

plt.title('Irregular Factor', fontsize=16)

plt.ylim(-12, 55)

plt.show()


 



추세 요인(trend factor), 순환 요인 (cycle factor), 계절 요인(seasonal factor), 불규칙 요인(irregular factor)와 이를 모두 합한 시계열 자료를 모두 모아서 하나의 그래프에 시각화하면 아래와 같습니다. 



# All in one: Time series = Trend factor + Cycle factor + Seasonal factor + Irregular factor


from pylab import rcParams

rcParams['figure.figsize'] = 12, 8

df.plot()

plt.ylim(-12, 55)

plt.show()

 



다음 포스팅에서는 위에서 만든 가상의 시계열 데이터를 Python과 R을 사용해서 시계열 구성요인 별로 분해(time series decomposition)를 해보겠습니다. (https://rfriend.tistory.com/509)


많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 Python pandas에서 resampling 중 Downsampling 으로 집계할 때에 왼쪽과 오른쪽 중에서 어느쪽을 포함(inclusive, closed)할 지와 어느쪽으로 라벨 이름(label)을 쓸지(https://rfriend.tistory.com/507)에 대해서 알아보았습니다. 


이번 포스팅에서는 pandas의 resampling 중 Upsampling으로 시계열 데이터 주기(frequency)를 변환(conversion) 할 때 생기는 결측값을 처리하는 두 가지 방법을 소개하겠습니다. 


(1) Upsampling 으로 주기 변환 시 생기는 결측값을 채우는 방법 (filling forward/backward)

(2) Upsampling 으로 주기 변환 시 생기는 결측값을 선형 보간하는 방법 (linear interpolation)






예제로 사용할 간단할 2개의 칼럼을 가지고 주기(frequency)가 5초(5 seconds)인 시계열 데이터 DataFrame을 만들어보겠습니다. 



import pandas as pd

import numpy as np


rng = pd.date_range('2019-12-31', periods=3, freq='5S')

rng

[Out]:

DatetimeIndex(['2019-12-31 00:00:00', '2019-12-31 00:00:05', '2019-12-31 00:00:10'], dtype='datetime64[ns]', freq='5S')


ts = pd.DataFrame(np.array([0, 1, 3, 2, 10, 3]).reshape(3, 2), 

                  index=rng

                  columns=['col_1', 'col_2'])

ts

[Out]:

col_1col_2
2019-12-31 00:00:0001
2019-12-31 00:00:0532
2019-12-31 00:00:10103




이제 pandas resample() 메소드를 사용해서 주기가 5초(freq='5S')인 원래 데이터를 주기가 1초(freq='1S')인 데이터로 Upsampling 변환을 해보겠습니다. 그러면 아래처럼 새로 생긴 날짜-시간 행에 결측값(missing value)이 생깁니다ㅣ  



ts_upsample = ts.resample('S').mean()

ts_upsample

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:01NaNNaN
2019-12-31 00:00:02NaNNaN
2019-12-31 00:00:03NaNNaN
2019-12-31 00:00:04NaNNaN
2019-12-31 00:00:053.02.0
2019-12-31 00:00:06NaNNaN
2019-12-31 00:00:07NaNNaN
2019-12-31 00:00:08NaNNaN
2019-12-31 00:00:09NaNNaN
2019-12-31 00:00:1010.03.0

 



위에 Upsampling을 해서 생긴 결측값들을 (1) 채우기(filling), (2) 선형 보간(linear interpolation) 해보겠습니다. 



  (1) Upsampling 으로 주기 변환 시 생기는 결측값을 채우기 (filling missing values)


(1-1) 앞의 값으로 뒤의 결측값 채우기 (Filling forward)



# (1) filling forward

ts_upsample.ffill()

ts_upsample.fillna(method='ffill')

ts_upsample.fillna(method='pad')

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:010.01.0
2019-12-31 00:00:020.01.0
2019-12-31 00:00:030.01.0
2019-12-31 00:00:040.01.0
2019-12-31 00:00:053.02.0
2019-12-31 00:00:063.02.0
2019-12-31 00:00:073.02.0
2019-12-31 00:00:083.02.0
2019-12-31 00:00:093.02.0
2019-12-31 00:00:1010.03.0





(1-2) 뒤의 값으로 앞의 결측값 채우기 (Filling backward)



# (2)filling backward

ts_upsample.bfill()

ts_upsample.fillna(method='bfill')

ts_upsample.fillna(method='backfill')

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:013.02.0
2019-12-31 00:00:023.02.0
2019-12-31 00:00:033.02.0
2019-12-31 00:00:043.02.0
2019-12-31 00:00:053.02.0
2019-12-31 00:00:0610.03.0
2019-12-31 00:00:0710.03.0
2019-12-31 00:00:0810.03.0
2019-12-31 00:00:0910.03.0
2019-12-31 00:00:1010.03.0





(1-3) 특정 값으로 결측값 채우기



# (3)fill Missing value with '0'

ts_upsample.fillna(0)

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:010.00.0
2019-12-31 00:00:020.00.0
2019-12-31 00:00:030.00.0
2019-12-31 00:00:040.00.0
2019-12-31 00:00:053.02.0
2019-12-31 00:00:060.00.0
2019-12-31 00:00:070.00.0
2019-12-31 00:00:080.00.0
2019-12-31 00:00:090.00.0
2019-12-31 00:00:1010.03.0

 




(1-4) 평균 값으로 결측값 채우기



# (4) filling with mean value

# mean per column

ts_upsample.mean()

[Out]:
col_1    4.333333
col_2    2.000000
dtype: float64


ts_upsample.fillna(ts_upsample.mean())

[Out]:

col_1col_2
2019-12-31 00:00:000.0000001.0
2019-12-31 00:00:014.3333332.0
2019-12-31 00:00:024.3333332.0
2019-12-31 00:00:034.3333332.0
2019-12-31 00:00:044.3333332.0
2019-12-31 00:00:053.0000002.0
2019-12-31 00:00:064.3333332.0
2019-12-31 00:00:074.3333332.0
2019-12-31 00:00:084.3333332.0
2019-12-31 00:00:094.3333332.0
2019-12-31 00:00:1010.0000003.0

 




(1-5) 결측값 채우는 행의 개수 제한하기



# (5) limit the number of filling observation

ts_upsample.ffill(limit=1)

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:010.01.0
2019-12-31 00:00:02NaNNaN
2019-12-31 00:00:03NaNNaN
2019-12-31 00:00:04NaNNaN
2019-12-31 00:00:053.02.0
2019-12-31 00:00:063.02.0
2019-12-31 00:00:07NaNNaN
2019-12-31 00:00:08NaNNaN
2019-12-31 00:00:09NaNNaN
2019-12-31 00:00:1010.03.0
 




  (2) Upsampling 으로 주기 변환 시 생기는 결측값을 선형 보간하기 (linear interpolation)



# (6) Linear interpolation by values

ts_upsample.interpolate(method='values') # by default

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:010.61.2
2019-12-31 00:00:021.21.4
2019-12-31 00:00:031.81.6
2019-12-31 00:00:042.41.8
2019-12-31 00:00:053.02.0
2019-12-31 00:00:064.42.2
2019-12-31 00:00:075.82.4
2019-12-31 00:00:087.22.6
2019-12-31 00:00:098.62.8
2019-12-31 00:00:1010.03.0



ts_upsample.interpolate(method='values').plot()





# (7) Linear interpolation by time

ts_upsample.interpolate(method='time')

[Out]:

col_1col_2
2019-12-31 00:00:000.01.0
2019-12-31 00:00:010.61.2
2019-12-31 00:00:021.21.4
2019-12-31 00:00:031.81.6
2019-12-31 00:00:042.41.8
2019-12-31 00:00:053.02.0
2019-12-31 00:00:064.42.2
2019-12-31 00:00:075.82.4
2019-12-31 00:00:087.22.6
2019-12-31 00:00:098.62.8
2019-12-31 00:00:1010.03.0

 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)


728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 분기 단위의 기간 날짜 범위 만들기, 그리고 period와 timestamp 간 변환하기 (https://rfriend.tistory.com/506)에 대해서 소개하였습니다. 


Python pandas의 resample() 메소드를 사용하면 


(a) 더 세부적인 주기(higher frequency)의 시계열 데이터를 더 낮은 주기로 집계/요약을 하는 Downsampling (예: 초(seconds) --> 10초(10 seconds), 일(day) --> 주(week), 일(day) --> 월(month) 등)과, 


(b) 더 낮은 주기의 시계열 데이터를 더 세부적인 주기의 데이터로 변환하는 Upsampling (예: 10초 --> 1초, 주 --> 일, 월 --> 주, 년 --> 일 등)을 할 수 있습니다.  



이번 포스팅에서는 pandas의 resample() 메소드로 Downsampling 을 할 때 (예: 1초 단위 주기 --> 10초 단위/ 1분 단위/ 1시간 단위 주기로 resampling)


(1) 왼쪽과 오른쪽 중에서 포함 위치 설정 (closed)

(2) 왼쪽과 오른쪽 중에서 라벨 이름 위치 설정 (label)


하는 방법을 소개하겠습니다. 


포함 위치와 라벨 이름 설정 시 왼쪽과 오른쪽 중에서 어디를 사용하느냐에 대한 규칙은 없구요, (a) 명확하게 인지하고 있고 (특히, 여러 사람이 동시에 협업하여 작업할 경우), (b) product의 코드 전반에 걸쳐서 일관되게(consistant) 사용하는 것이 필요합니다.  (SQL로 DB에서 두 그룹으로 나누어서 시계열 데이터 전처리 작업을 하다가 나중에서야 포함 여부와 라벨 규칙이 서로 다르다는 것을 확인하고, 이를 동일 규칙으로 수정하느라 시간을 소비했던 경험이 있습니다. -_-;;;)







예제로 사용하기 위해 1분 단위 주기의 6개 데이터 포인트를 가지는 간단한 시계열 데이터 pandas Series 를 만들어보겠습니다. 



import pandas as pd


# generate dates range

dates = pd.date_range('2020-12-31', periods=6, freq='min') # or freq='T'

dates

[Out]:

DatetimeIndex(['2020-12-31 00:00:00', '2020-12-31 00:01:00', '2020-12-31 00:02:00', '2020-12-31 00:03:00', '2020-12-31 00:04:00', '2020-12-31 00:05:00'], dtype='datetime64[ns]', freq='T')

# create Series

ts_series = pd.Series(range(len(dates)), index=dates)

ts_series

[Out]:
2020-12-31 00:00:00    0
2020-12-31 00:01:00    1
2020-12-31 00:02:00    2
2020-12-31 00:03:00    3
2020-12-31 00:04:00    4
2020-12-31 00:05:00    5
Freq: T, dtype: int64



이제 '1 분 단위 주기'(freq='min')인 시계열 데이터를 '2초 단위 주기'(freq='2min' or freq='2T')로 resample() 메소드를 이용해서 Downsampling을 해보도록 하겠습니다. 


이때 포함 위치 (a) closed='left' (by default) 또는 (b) closed='right' 과 라벨 이름 위치 (c) label='left' (by default) 또는 label='right' 의 총 4개 조합별로 나누어서 Downsampling 결과를 비교해보겠습니다. 집계 함수는 sum()을 공통으로 사용하겠습니다. 



  (1) By default: Downsampling 시 closed='left', label='left'


Downsampling 할 때 왼쪽과 오른쪽 중에서 한쪽은 포함(inclusive, default: 'left')되고 나머지 한쪽은 포함되지 않습니다. 그리고 Downsampling으로 resampling 된 후의 라벨 이름의 경우 default는 가장 왼쪽(label='left')의 라벨을 사용합니다. 



ts_series = pd.Series(range(len(dates)), index=dates)

ts_series

[Out]:
2020-12-31 00:00:00    0
2020-12-31 00:01:00    1
2020-12-31 00:02:00    2
2020-12-31 00:03:00    3
2020-12-31 00:04:00    4
2020-12-31 00:05:00    5
Freq: T, dtype: int64


# by default, left side of bin interval is closed

# by default, left side of bin inverval is labeled

ts_series.resample('2min').sum()

[Out]:

2020-12-31 00:00:00 1 2020-12-31 00:02:00 5 2020-12-31 00:04:00 9 Freq: 2T, dtype: int64


# same result with above

ts_series.resample('2min', closed='left', label='left').sum()

[Out]:
2020-12-31 00:00:00    1
2020-12-31 00:02:00    5
2020-12-31 00:04:00    9
Freq: 2T, dtype: int64





  (2) Downsampling 시 closed='right', label='left'



ts_series = pd.Series(range(len(dates)), index=dates)

ts_series

[Out]:
2020-12-31 00:00:00    0
2020-12-31 00:01:00    1
2020-12-31 00:02:00    2
2020-12-31 00:03:00    3
2020-12-31 00:04:00    4
2020-12-31 00:05:00    5
Freq: T, dtype: int64


# right side of bin interval is closed using closed='right'

ts_series.resample('2min', closed='right', label='left').sum()

[Out]:

2020-12-30 23:58:00 0 2020-12-31 00:00:00 3 2020-12-31 00:02:00 7 2020-12-31 00:04:00 5 Freq: 2T, dtype: int64

 




  (3) Downsampling 시 closed='left', label='right'



ts_series = pd.Series(range(len(dates)), index=dates)

ts_series

[Out]:
2020-12-31 00:00:00    0
2020-12-31 00:01:00    1
2020-12-31 00:02:00    2
2020-12-31 00:03:00    3
2020-12-31 00:04:00    4
2020-12-31 00:05:00    5
Freq: T, dtype: int64


# right side of bin inverval is labeled using label='right'

ts_series.resample('2min', closed='left', label='right').sum()

[Out]:
2020-12-31 00:02:00    1
2020-12-31 00:04:00    5
2020-12-31 00:06:00    9
Freq: 2T, dtype: int64





  (4) Downsampling 시 closed='right', label='right'


아래의 예는 디폴트와 정반대로 시계열 구간의 오른쪽을 포함시키고(closed='right') 라벨 이름도 오른쪽 구간 값(label='right')을 가져다가 Downsampling 한 경우입니다. 



ts_series = 
pd.Series(range(len(dates)), index=dates)

ts_series

[Out]:
2020-12-31 00:00:00    0
2020-12-31 00:01:00    1
2020-12-31 00:02:00    2
2020-12-31 00:03:00    3
2020-12-31 00:04:00    4
2020-12-31 00:05:00    5
Freq: T, dtype: int64


# right side of bin interval is closed using closed='right'

# right side of bin inverval is labeled using label='right'

ts_series.resample('2min', closed='right', label='right').sum()

2020-12-31 00:00:00    0
2020-12-31 00:02:00    3
2020-12-31 00:04:00    7
2020-12-31 00:06:00    5 

Freq: 2T, dtype: int64






  (5) 시계열 pandas DataFrame에 대해 Downsaumpling 시 포함(closed), 라벨(label) 위치 설정하기



지금까지 위의 (1), (2), (3), (4)는 pandas Series를 대상으로 한 예제였습니다. DatatimeIndex를 index로 가지는 시계열 데이터 pandas DataFrame 도 Series와 동일한 방법으로 Downsampling 하면서 포함, 라벨 위치를 설정합니다. 



import pandas as pd


# generate dates range

dates = pd.date_range('2020-12-31', periods=6, freq='min')

dates

[Out]:
DatetimeIndex(['2020-12-31 00:00:00', '2020-12-31 00:01:00',
               '2020-12-31 00:02:00', '2020-12-31 00:03:00',
               '2020-12-31 00:04:00', '2020-12-31 00:05:00'],
              dtype='datetime64[ns]', freq='T')

# create timeseries DataFrame

ts_df = pd.DataFrame({'val': range(len(dates))}, index=dates)

ts_df

[Out]:
val
2020-12-31 00:00:000
2020-12-31 00:01:001
2020-12-31 00:02:002
2020-12-31 00:03:003
2020-12-31 00:04:004
2020-12-31 00:05:005



# (a) Downsampling using default setting

ts_df.resample('2min').sum()

[Out]:

val
2020-12-31 00:00:001
2020-12-31 00:02:005
2020-12-31 00:04:009


# (b) Downsampling using closed='right'

ts_df.resample('2min', closed='right').sum()

[Out]:

val
2020-12-30 23:58:000
2020-12-31 00:00:003
2020-12-31 00:02:007
2020-12-31 00:04:005


# (c) Downsampling using label='right'

ts_df.resample('2min', label='right').sum()

[Out]:

val
2020-12-31 00:02:001
2020-12-31 00:04:005
2020-12-31 00:06:009


# (d) Downsampling using closed='right', label='right'

ts_df.resample('2min', closed='right', label='right').sum()

[Out]:

val
2020-12-31 00:00:000
2020-12-31 00:02:003
2020-12-31 00:04:007
2020-12-31 00:06:005




많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요.



728x90
반응형
Posted by Rfriend
,

지난번 포스팅에서는 Python pandas에서 시간대를 확인, 설정, 변경하는 방법(https://rfriend.tistory.com/505)을 소개하였습니다. 


이번 포스팅에서는 Python pandas에서 


(1) 분기 단위의 기간 주기 만들기 (quarterly period frequencies)

(2) 분기 단위의 기간 날짜-범위 만들기 (quarterly period date-range)

(3) 분기 단위의 기간과 timestamp 간 변환하기 (conversion between quarterly period and timestamp)

(4) 분기 단위 기간으로 집계하기 (quarterly period group by aggregation)


에 대해서 소개하겠습니다. 


이번 포스팅은 특히, 금융, 회계 분야에서 분기 단위(fiscal year quarters) 실적 집계, 분석할 때 pandas로 하기에 유용한 기능들입니다. 


[ 그림1. pandas 분기 단위의 기간 범위 만들기 (Quarterly Period Range) ]




  (1) 분기 단위의 기간 주기 만들기 (quarterly period frequencies)


pandas Period() 함수를 사용해서 the Fiscal Year 2020 4 Quarter 를 만들어보겠습니다. 회기년도 '2020-Q4'는 위의 [그림 1] 에서 보는 바와 같이, 2019.3월~5월(2020- Q1), 2019.6월~8월(2020-Q2), 2019.9월~11월(2020-Q3), 2019.12월~2020.2월(2020-Q4) 의 기간으로 구성되어 있습니다. (회계년도 2020 에 2019년의 3월~12월이 포함되어서 좀 이상하게 보일 수도 있는데요, 그냥 이렇습니다. ^^') 



import pandas as pd

import numpy as np


p = pd.Period('2020Q4', freq='Q-FEB')

p

[Out]: Period('2020Q4', 'Q-FEB')




pandas의 asfreq() 메소드를 사용하면 pandas Period 객체를 원하는 주기(Period frequency)로 변환할 수 있습니다. 위의 2020-Q4 의 분기 단위의 기간(Quarterly Period)를 asfreq() 메소드를 사용해 (a) 분기별 시작 날짜(starting date)와 끝 날짜(ending date), (b) 분기별 공휴일이 아닌 시작 날짜(staring business date)와 공휴일이 아닌 끝 날짜 (ending business date)로 변환해 보겠습니다. 


(a) converting from Period to Date: 'D'

(b) converting from Period to Business Date: 'B'

# starting date

p.asfreq('D', how='start')

[Out]: Period('2019-12-01', 'D')


# ending date

p.asfreq('D', how='end')

[Out]: Period('2020-02-29', 'D') 

 

# starting business date

p.asfreq('B', how='start')

[Out]: Period('2019-12-02', 'B')


# ending business date

p.asfreq('B', how='end')

[Out]: Period('2020-02-28', 'B')



 asfreq() 메소드를 chain으로 연속으로 이어서 

  (a) 분기별 ending business date를 선택하고 --> (b) starting(how-='start) minutes (freq='T' or freq='min')의 주기(frequency)로 변환한다거나, 

  (c) 분기별 ending business date를 선택하고 --> 이를 (d) ending minutes('T', or 'min') 로 변환하거나, 

  (e) 분기별 ending business date를 선택하고 --> 이를 (f) ending seconds 로 변환


하는 것이 모두 가능합니다. 



# (a) from ending Business date --> (b) to starting Minutes

p.asfreq('B', how='end').asfreq('T', how='start')

[Out]: Period('2020-02-28 00:00', 'T')


# (c) from ending Business date --> (d) to ending Minutes

p.asfreq('B', how='end').asfreq('T', how='end')

[Out]: Period('2020-02-28 23:59', 'T')



# (e) from Business date --> (f) to Seconds

p.asfreq('B', how='end').asfreq('S', how='end')

[Out]: Period('2020-02-28 23:59:59', 'S')






  (2) 분기 단위의 기간 범위 만들기 (quarterly period range)


pandas의 date_range() 함수로 날짜-시간 범위의 DatetimeIndex 객체를 만들 듯이, pandas의 period_range('start', 'end', freq='Q-[ending-month]') 함수를 사용해서 분기 단위의 기간 범위(quarterly period range)를 만들 수 있습니다.  (참고로 freq='A-DEC' 는 12월을 마지막으로 가지는 년 단위 기간(yearly period)라는 뜻이며, freq='Q-FEB'는 2월달을 마지막으로 가지는 분기 단위 기간(quarterly period)라는 뜻입니다)


아래 예는 2020-Q1 ~ 2020-Q4 기간(pd.period_range('2020Q1', '2020Q4')의 2월달을 마지막으로 하는 분기 단위의 기간(freq='Q-FEB')을 만든 것입니다. 



p_rng = pd.period_range('2020Q1', '2020Q4', freq='Q-FEB')

p_rng

[Out]:PeriodIndex(['2020Q1', '2020Q2', '2020Q3', '2020Q4'], dtype='period[Q-FEB]',

freq='Q-FEB')




asfreq()  메소드를 사용해서 위에서 생성한 '2020-Q1' ~ '2020-Q4' 기간(period with a Quarter ending at February)공휴일이 아닌 시작 날짜(staring business date)와 끝 날짜(ending business date)로 변환해보겠습니다



# convert period into deisred frequency using asfreq() methods

# starting business day per quarter 'Q-FEB'

p_rng.asfreq('B', how='start')

[Out]: 
PeriodIndex(['2019-03-01', '2019-06-03', '2019-09-02', '2019-12-02'], 
dtype='period[B]', freq='B')


# ending business day per quarter 'Q-FEB'

p_rng.asfreq('B', how='end')

[Out]: 
PeriodIndex(['2019-05-31', '2019-08-30', '2019-11-29', '2020-02-28'], 
dtype='period[B]', freq='B')

 




기간(Period) 객체를 frequency로 변환한 후에 산술 연산(arithmetic operation)이 가능합니다. 아래 예는 2월달에 끝나는 4 분기의 ending business date에 1 day 를 더한것입니다. 



# arithmatic operation: plus one day

p_rng.asfreq('B', how='end') + 1

[Out]: 
PeriodIndex(['2019-06-03', '2019-09-02', '2019-12-02', '2020-03-02'], 
dtype='period[B]', freq='B')


 



아래의 예는 period object를 ending business date로 먼저 변환하고, 이를 다시 starting hour frequency로 변환한 후에 여기에 12 hours 를 더한 것입니다. 



# period ending Business day, starting Hour

p_rng.asfreq('B', how='end').asfreq('H', how='start')

[Out]: 
PeriodIndex(['2019-05-31 00:00', '2019-08-30 00:00', '2019-11-29 00:00',
             '2020-02-28 00:00'],
            dtype='period[H]', freq='H')


# plus 12 hours

p_12h_rng = p_rng.asfreq('B', how='end').asfreq('H', how='start') + 12

p_12h_rng

[Out]:
PeriodIndex(['2019-05-31 12:00', '2019-08-30 12:00', '2019-11-29 12:00',
             '2020-02-28 12:00'],
            dtype='period[H]', freq='H')






  (3) 분기 단위의 기간과 timestamp 간 변환하기 

       (conversion between quarterly period and timestamp)



pandas date_range() 로 만든 날짜-시간 DatetimeIndex를 pandas.to_period()  메소드를 사용해서 PeriodIndex로 변환할 수 있습니다. 



import pandas as pd


# generate dates range with 12 Months

ts = pd.date_range('2020-01-01', periods = 12, freq='M')

ts

[Out]:

DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30', '2020-05-31', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-31', '2020-11-30', '2020-12-31'], dtype='datetime64[ns]', freq='M')

 


# convert from DatetimeIndex to PeriodIndex

p = ts.to_period()

p

[Out]:

PeriodIndex(['2020-01', '2020-02', '2020-03', '2020-04', '2020-05', '2020-06', '2020-07', '2020-08', '2020-09', '2020-10', '2020-11', '2020-12'],

dtype='period[M]', freq='M')




반대로,  pandas.to_timestamp() 메소드를 사용해서 PeriodIndex를 DatetimeIndex로 변환할 수 있습니다. 



# convert from PeriodIndex to DatetimeIndex with starting month('M')

p.asfreq('B', how='end').asfreq('M', how='start').to_timestamp()

[Out]:
DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
               '2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
               '2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01'],
              dtype='datetime64[ns]', freq='MS')



# convert from PeriodIndex to DatatimeIndex with ending minutes('T')

p.asfreq('B', how='end').asfreq('T', how='end').to_timestamp()

DatetimeIndex(['2020-01-31 23:59:00', '2020-02-28 23:59:00',
               '2020-03-31 23:59:00', '2020-04-30 23:59:00',
               '2020-05-29 23:59:00', '2020-06-30 23:59:00',
               '2020-07-31 23:59:00', '2020-08-31 23:59:00',
               '2020-09-30 23:59:00', '2020-10-30 23:59:00',
               '2020-11-30 23:59:00', '2020-12-31 23:59:00'],
              dtype='datetime64[ns]', freq='BM')

 




  (4) 분기 기간 단위 집계 (quarterly period group by aggregation) 


간단한 월 단위 pandas Series 를 분기 단위 Period Index를 가진 Series로 변환한 후에, 분기 단위로 평균을 집계해보겠습니다. 



ts = pd.date_range('2020-01-01', periods = 12, freq='M')

ts_series = pd.Series(range(len(ts)), index=ts)

ts_series

[Out]:

2020-01-31 0 2020-02-29 1 2020-03-31 2 2020-04-30 3 2020-05-31 4 2020-06-30 5 2020-07-31 6 2020-08-31 7 2020-09-30 8 2020-10-31 9 2020-11-30 10 2020-12-31 11 Freq: M, dtype: int64


# convert from DatatimeIndex to Quarterly PeriodIndex

ts_series.index = ts.to_period(freq='Q-FEB')

ts_series

[Out]:
2020Q4     0
2020Q4     1
2021Q1     2
2021Q1     3
2021Q1     4
2021Q2     5
2021Q2     6
2021Q2     7
2021Q3     8
2021Q3     9
2021Q3    10
2021Q4    11 

Freq: Q-FEB, dtype: int64


# quarterly groupby mean aggregation

ts_series.groupby(ts_series.index).mean()

[Out]:
2020Q4     0.5
2021Q1     3.0
2021Q2     6.0
2021Q3     9.0
2021Q4    11.0
Freq: Q-FEB, dtype: float64




참고로, 아래는 resample() 메소드로 downsampling 해서 분기 단위로 평균을 집계해본 것인데요, 위의 to_period(freq='Q-FEB')로 frequency를 변환해서 groupby()로 집계한 것과 년도(2020 vs. 2021)가 서로 다릅니다. 



ts = pd.date_range('2020-01-01', periods = 12, freq='M')

ts_series = pd.Series(range(len(ts)), index=ts)

ts_series.resample('Q-FEB').mean()

[Out]:
2020-02-29     0.5
2020-05-31     3.0
2020-08-31     6.0
2020-11-30     9.0
2021-02-28    11.0
Freq: Q-FEB, dtype: float64

 



resample 시 kind='period' 옵션을 설정해주면 ts.to_period(freq='Q-FEB') 를 groupby 한 결과와 동일한 값을 얻을 수 있습니다. 



ts_series.resample('Q-FEB', kind='period').mean()

[Out]:
2020Q4     0.5
2021Q1     3.0
2021Q2     6.0
2021Q3     9.0
2021Q4    11.0
Freq: Q-FEB, dtype: float64

 




많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,