Tools (126)

반응형


다양한 R 스타일 가이드


좋은 코딩 스타일은 정확한 구두점을 찍는 것과 비슷합니다. 구두점이 없더라도 의미를 알 수는 있지만 구두점은 의미 전달을 더욱 쉽게 하고 글을 읽기 쉽게 만듭니다. R 에는 아래와 같이 유명한 스타일이 존재합니다.


Bioconductor’s coding standards (https://bioconductor.org/developers/how-to/coding-style/)

- Hadley Wickham’s style guide (http://stat405.had.co.nz/r-style.html )

- Google’s R style guide (http://google-styleguide.googlecode.com/ svn/trunk/google-r-style.html )

- Colin Gillespie’s R style guide (http://csgillespie.wordpress.com/2010/ 11/23/r-style-guide/)

The State of Naming Conventions in R (by Rasmus Bååth)


일관적으로 코딩하는 것은 1) 사람들과 소통하기 쉽도록하고, 2) 스스로도 읽기 쉬운 코드를 작성할 수 있습니다. 코드를 작성한지 오랜 시간이 지나고, 다시 그 코드를 보았을 때, 의미를 알기 힘든 경우가 많습니다. 하지만 최대한 비슷한 스타일로 코딩을 한다면, 코드 이해에 드는 부담을 줄일 수 있습니다. 또한 많은 사람들이 비슷한 코딩 규약을 사용한다면 코딩을 통한 소통이 편리할 것입니다.


해들리 위컴의 R 코딩 스타일 가이드


본 포스팅에서는 tidyverse (tidyr, dplyr, ggplot2 등) 패키지로 유명한 해들리 위컴의 R 스타일 가이드를 정리해보도록 하겠습니다. 해들리 위컴의 R 스타일은 Google 의 R 스타일 가이드 를 약간 변형한 스타일입니다.  


이름짓기

- 파일 이름

1. 파일 이름은 의미가 있어야합니다.

# Good
fit-models.R
utility-functions.R

# Bad
foo.r
stuff.r

2. 만약 파일이 순서대로 실행되어야 한다면, 이와 같이 숫자로 prefix 를 붙이는 것도 좋습니다. 

0-download.R
1-parse.R
2-explore.R

- 객체 이름 

“There are only two hard things in Computer Science: cache invalidation and naming things.”
Phil Karlton

변수명과 함수이름은 소문자를 사용하며, 단어를 분리할 때는 underscore(_) 를 사용합니다. 일반적으로 변수는 명사, 함수는 동사를 사용하는 것이 좋습니다. 이름은 최대한 간결하며 의미를 잘 전달할 수 있습니다. (이것은 때론 쉽지 않습니다.)

# Good
day_one
day_1

# Bad
first_day_of_the_month
DayOne
dayone
djm1

c, mean 과 같은 이미 사용되고 있는 객체에는 새로운 변수나 함수를 할당하는 것이 좋지 않습니다. 

# Bad
T <- FALSE
c <- 10
mean <- function(x) sum(x)

문법

Spacing

infix operators (=, +, -, <-, etc.) 에는 space 를 양옆으로 위치시킵니다. = 에서도 마찬가지입니다. comma 뒤는 한 칸 띄우며, comma 전에는 띄우지 않습니다. (이는 영어, 한글에서도 마찬가지입니다.)

# Good
average <- mean(feet / 12 + inches, na.rm = TRUE)

# Bad
average<-mean(feet/12+inches,na.rm=TRUE)

하지만 : 나 ::는 spacing을 하지 않습니다. 

# Good
x <- 1:10
base::get

# Bad
x <- 1 : 10
base :: get

if, for 문 등에서 ( 전에 한 칸을 띄웁니다. 하지만 함수를 호출할 때는 띄우지 않습니다. 

# Good
if (debug) do(x)
plot(x, y)

# Bad
if(debug)do(x)
plot (x, y)

아래처럼 정렬을 위해 space 를 두 번 이상하는것을 허용합니다. 

list(
  total   = a + b + c, 
  mean  = (a + b + c) / n
)

중괄호나 대괄호에는 comma 가 있는 경우가 아니라면 space 를 넣지 않습니다. 

# Good
if (debug) do(x)
diamonds[5, ]

# Bad
if ( debug ) do(x)  # No spaces around debug
x[1,]   # Needs a space after the comma
x[1 ,]  # Space goes after comma not before

중괄호

중괄호의 시작 ( { ) 이 있는 경우, 항상 개행을 해야합니다. 중괄호의 끝( } ) 이 있는 경우, if 문의 else 가 뒤따라오지 않는다면 그 줄에는 } 만 오도록 합니다. 물론 중괄호 안에서의 indentation 은 기본입니다. 

# Good

if (y < 0 && debug) {
  message("Y is negative")
}

if (y == 0) {
  log(x)
} else {
  y ^ x
}

# Bad

if (y < 0 && debug)
message("Y is negative")

if (y == 0) {
  log(x)
} 
else {
  y ^ x
}

매우 짧은 if 문의 경우 한 줄에 작성하는 것을 허용합니다. 

if (y < 0 && debug) message("Y is negative")

한 줄의 길이

한 줄의 길이를 80 자가 넘지 않도록 하는 것이 좋습니다. 이는 일반적인 크기의 글꼴로 프린트를 했을 때 용지에 잘 맞도록 하기 위함입니다. 

Indentation

Indentation 을 할 때, space 만 사용하는 것이 좋습니다. 다만 function 을 정의할 때, parameter 의 수가 많아 줄이 길어진다면 함수의 정의가 시작되는 부분에 맞게 indentation 을 합니다. 

long_function_name <- function(a = "a long argument", 
                               b = "another argument",
                               c = "another long argument") {
  # As usual code is indented by two spaces.
}

할당

변수 할당에는 = 를 사용하지 않고 <- 를 사용합니다. 

# Good
x <- 5
# Bad
x = 5

주석 달기

코드에 주석을 다는 것이 좋습니다. 주석은 무엇을 (what) 이 아닌 왜(why) 에 대한 내용을 설명해야 합니다. what 에 대한 내용은 코드 자체로 충분히 이해할 수 있도록 하는것이 좋습니다. 아래처럼 주석을 입력한 줄에 - 또는 = 을 사용해 읽기 쉽도록 분리하는 것도 좋습니다. 

# Load data ---------------------------

# Plot data ---------------------------


출처 - http://adv-r.had.co.nz/Style.html



반응형
반응형


PEP 은 무엇인가?


PEP 8 -- Style Guide for Python Code

PEP:8
Title:Style Guide for Python Code
Author:Guido van Rossum <guido at python.org>, Barry Warsaw <barry at python.org>, Nick Coghlan <ncoghlan at gmail.com>
Status:Active
Type:Process
Created:05-Jul-2001
Post-History:05-Jul-2001, 01-Aug-2013

PEP8은  Python Enhancement Proposals (PEP) 의 8 번째 내용으로, 더 나은 파이썬 코드를 작성하기 위한 하나의 코딩 규약 (Style Guide for Python Code)입니다. (https://www.python.org/dev/peps/pep-0008/)


Python PEP8 code convention cheat sheet


아래 파이썬 코드는 PEP8 style 을 활용한 코딩 예입니다. 일종의 cheat sheet로 이 코드만 알면 PEP8 에서 지향하는 파이썬 코딩 스타일의 대부분을 알 수 있습니다!


내용을 간단하게 요약하면 아래와 같습니다.


1. 모듈과 패키지 이름은 짧고 lower_case_with_underscore 이다. 

2. 다른 모듈에서 import 할 때 보일 필요가 없는 함수, 변수는 변수명 앞에 _를 추가한다. 

3. 상수는 A_CONSTANT 

4. 함수는 naming_convention

5. 클래스는 NamingConvention

6. 한 라인의 길이가 79 길이가 넘지 않도록 한다. (이것은 대략적인 A4 용지 사이즈이다.)


#! /usr/bin/env python # -*- coding: utf-8 -*- """This module's docstring summary line. This is a multi-line docstring. Paragraphs are separated with blank lines. Lines conform to 79-column limit. Module and packages names should be short, lower_case_with_underscores. Notice that this in not PEP8-cheatsheet.py Seriously, use flake8. Atom.io with https://atom.io/packages/linter-flake8 is awesome! See http://www.python.org/dev/peps/pep-0008/ for more PEP-8 details """ import os # STD lib imports first import sys # alphabetical import some_third_party_lib # 3rd party stuff next import some_third_party_other_lib # alphabetical import local_stuff # local stuff last import more_local_stuff import dont_import_two, modules_in_one_line # IMPORTANT! from pyflakes_cannot_handle import * # and there are other reasons it should be avoided # noqa # Using # noqa in the line above avoids flake8 warnings about line length! _a_global_var = 2 # so it won't get imported by 'from foo import *' _b_global_var = 3 A_CONSTANT = 'ugh.' # 2 empty lines between top-level funcs + classes def naming_convention(): """Write docstrings for ALL public classes, funcs and methods.     Functions use snake_case.     """ if x == 4: # x is blue <== USEFUL 1-liner comment (2 spaces before #) x, y = y, x # inverse x and y <== USELESS COMMENT (1 space after #) c = (a + b) * (a - b) # operator spacing should improve readability. dict['key'] = dict[0] = {'x': 2, 'cat': 'not a dog'} class NamingConvention(object): """First line of a docstring is short and next to the quotes.     Class and exception names are CapWords.     Closing quotes are on their own line     """ a = 2 b = 4 _internal_variable = 3 class_ = 'foo' # trailing underscore to avoid conflict with builtin # this will trigger name mangling to further discourage use from outside # this is also very useful if you intend your class to be subclassed, and # the children might also use the same var name for something else; e.g. # for simple variables like 'a' above. Name mangling will ensure that # *your* a and the children's a will not collide. __internal_var = 4 # NEVER use double leading and trailing underscores for your own names __nooooooodontdoit__ = 0 # don't call anything (because some fonts are hard to distiguish): l = 1 O = 2 I = 3 # some examples of how to wrap code to conform to 79-columns limit: def __init__(self, width, height, color='black', emphasis=None, highlight=0): if width == 0 and height == 0 and \ color == 'red' and emphasis == 'strong' or \ highlight > 100: raise ValueError('sorry, you lose') if width == 0 and height == 0 and (color == 'red' or emphasis is None): raise ValueError("I don't think so -- values are %s, %s" % (width, height)) Blob.__init__(self, width, height, color, emphasis, highlight) # empty lines within method to enhance readability; no set rule short_foo_dict = {'loooooooooooooooooooong_element_name': 'cat', 'other_element': 'dog'} long_foo_dict_with_many_elements = { 'foo': 'cat', 'bar': 'dog' } # 1 empty line between in-class def'ns def foo_method(self, x, y=None): """Method and function names are lower_case_with_underscores.         Always use self as first arg.         """ pass @classmethod def bar(cls): """Use cls!""" pass # a 79-char ruler: # 34567891123456789212345678931234567894123456789512345678961234567897123456789 """ Common naming convention names: snake_case MACRO_CASE camelCase CapWords """ # Newline at end of file


출처 - https://gist.github.com/RichardBronosky/454964087739a449da04



반응형
반응형

R - dplyr 을 통한 데이터 변형과 장점


본 포스팅에서는 tidyverse의 핵심 구성원이라고 할 수 있는 dplyr 을 통해 데이터를 변형하는 기본적인 방법을 basic R과 비교하여 설명하겠습니다


dplyr 의 5가지 핵심 함수

  • -값을 기준으로 선택하라 (filter)
  • -행을 재정렬하라 (arrange)
  • -이름으로 변수를 선택하라 (select)
  • -기존 변수들의 함수로 새로운 변수를 생성하라 (mutate)
  • -많은 값을 하나의 요약값으로 합쳐라 (summarize)

사용할 데이터셋은 뉴욕시에서 2013년에 출발한 336,776 개의 모든 항공편이 포함된 데이터 (nycflights13 패키지의 flights 데이터셋)

library(nycflights13)
library(tidyverse)
nrow(flights)
## [1] 336776

Filter

  • -1월 1일만 고르기
jan <- flights[flights$month == 1 & flights$day == 1, ]
nrow(jan)
## [1] 842
jan <- filter(flights, month == 1, day == 1)
nrow(jan)
## [1] 842
  • -출발 지연 시간이 120 미만인 항공편 고르기
  • -여기서 basic R과 dplyr 의 차이가 드러나는데, basic R 의 경우 arr_delay 변수가 NA 인 경우, 모든 column이 NA인 row를 포함한 dataframe을 반환한다.
  • -basic R의 경우 filter 를 구현할 때, NA 인 경우도 고려해야하므로, dplyr이 더 간단하게 원하는 목적을 달성할 수 있다고 볼 수 있다. 
'under_two <- flights[(flights$arr_delay < 120), ]
nrow(under_two) # arr_delay가 NA인 row가 모든 column이 NA인 row로 추가된다. .
## [1] 326576
sum(is.na(under_two$arr_delay))
## [1] 9430
sum(is.na(under_two$dep_delay))
## [1] 9430
under_two <- filter(flights, arr_delay < 120) 
nrow(under_two) 
## [1] 317146
sum(is.na(under_two$arr_delay))
## [1] 0
sum(is.na(under_two$dep_delay)) 
## [1] 0
# R 기본문법으로 dplyr의 filter를 구현하려면 이렇게 함. 
under_two <- flights[(flights$arr_delay < 120) & !is.na(flights$arr_delay), ]
nrow(under_two) 
## [1] 317146
sum(is.na(under_two$arr_delay))
## [1] 0
sum(is.na(under_two$dep_delay))
## [1] 0
  • -출발 혹은 도착에서 2시간 이상 지연되지 않은 항공편을 모두 찾기
  • -이 때, 두 조건절이 모두 NA 인 경우, basic R 에서는 모든 컬럼이 NA 인 행을 dataframe에 추가하여 반환한다. 
under_two <- flights[!((flights$arr_delay > 120) | (flights$dep_delay > 120)), ]
nrow(under_two) # 둘다 NA인 행 9430개의 행이, 포함되어있음
## [1] 325354
sum(is.na(under_two$arr_delay))
## [1] 9304
sum(is.na(under_two$dep_delay))
## [1] 9304
under_two <- filter(flights, !(arr_delay > 120 | dep_delay > 120)) 
nrow(under_two) # 둘다 NA인 행 제외. 이게 정상적인 결과.
## [1] 316050
sum(is.na(under_two$arr_delay))
## [1] 0
sum(is.na(under_two$dep_delay)) 
## [1] 0
  • -출발 시간, 도착 시간 둘 중 하나가 2시간 이상 지연된 항공편을 모두 찾기
over_two <- flights[((flights$arr_delay > 120) | (flights$dep_delay > 120)), ]
nrow(over_two) # 둘중 하나가 NA 이면서, delay > 120 인 행 포함, 근데 둘 다 NA인 경우 모든 컬럼이 NA인 행을 포함함 (9403 개)
## [1] 20726
sum(is.na(over_two$arr_delay)) # 9430 -> 둘중 하나가 NA인 수
## [1] 9430
sum(is.na(over_two$dep_delay)) #9304 -> 둘다 NA인 수 
## [1] 9304
over_two <- filter(flights, ((arr_delay > 120) | (dep_delay > 120))) # 둘중 하나가 NA 이면서, delay > 120인 행 포함
nrow(over_two) # 정상적인 결과 
## [1] 11422
sum(is.na(over_two$arr_delay)) 
## [1] 126
sum(is.na(over_two$dep_delay)) 
## [1] 0
  • -일반 데이터 프레임의 문제점. 조건이 NA인 경우 모든 컬럼이 NA인 행이 출력된다. 
  • -dplyr 의 filter 는 이러한 문제 없이 원하는 결과를 출력해준다. 
flights[NA,]
## # A tibble: 336,776 x 19
##     year month   day dep_time sched_dep_time dep_delay arr_time
##    <int> <int> <int>    <int>          <int>     <dbl>    <int>
##  1    NA    NA    NA       NA             NA        NA       NA
##  2    NA    NA    NA       NA             NA        NA       NA
##  3    NA    NA    NA       NA             NA        NA       NA
##  4    NA    NA    NA       NA             NA        NA       NA
##  5    NA    NA    NA       NA             NA        NA       NA
##  6    NA    NA    NA       NA             NA        NA       NA
##  7    NA    NA    NA       NA             NA        NA       NA
##  8    NA    NA    NA       NA             NA        NA       NA
##  9    NA    NA    NA       NA             NA        NA       NA
## 10    NA    NA    NA       NA             NA        NA       NA
## # ... with 336,766 more rows, and 12 more variables: sched_arr_time <int>,
## #   arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## #   origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## #   minute <dbl>, time_hour <dttm>

Arrange

  • -날짜별로 오름차순 정렬하기
  • -정렬의 경우, 마찬가지로 dplyr 이 훨씬 직관적이다.
arranged <- flights[order(flights$year, flights$month, flights$day),]
head(arranged)
## # A tibble: 6 x 19
##    year month   day dep_time sched_dep_time dep_delay arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>
## 1  2013     1     1      517            515         2      830
## 2  2013     1     1      533            529         4      850
## 3  2013     1     1      542            540         2      923
## 4  2013     1     1      544            545        -1     1004
## 5  2013     1     1      554            600        -6      812
## 6  2013     1     1      554            558        -4      740
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## #   carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>
arranged <- arrange(flights, year, month, day)
head(arranged)
## # A tibble: 6 x 19
##    year month   day dep_time sched_dep_time dep_delay arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>
## 1  2013     1     1      517            515         2      830
## 2  2013     1     1      533            529         4      850
## 3  2013     1     1      542            540         2      923
## 4  2013     1     1      544            545        -1     1004
## 5  2013     1     1      554            600        -6      812
## 6  2013     1     1      554            558        -4      740
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## #   carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>
  • -내림차순 정렬하기
desc <- flights[order(-flights$arr_delay),]
head(desc)
## # A tibble: 6 x 19
##    year month   day dep_time sched_dep_time dep_delay arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>
## 1  2013     1     9      641            900      1301     1242
## 2  2013     6    15     1432           1935      1137     1607
## 3  2013     1    10     1121           1635      1126     1239
## 4  2013     9    20     1139           1845      1014     1457
## 5  2013     7    22      845           1600      1005     1044
## 6  2013     4    10     1100           1900       960     1342
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## #   carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>
desc <- arrange(flights, desc(arr_delay))
head(desc)
## # A tibble: 6 x 19
##    year month   day dep_time sched_dep_time dep_delay arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>
## 1  2013     1     9      641            900      1301     1242
## 2  2013     6    15     1432           1935      1137     1607
## 3  2013     1    10     1121           1635      1126     1239
## 4  2013     9    20     1139           1845      1014     1457
## 5  2013     7    22      845           1600      1005     1044
## 6  2013     4    10     1100           1900       960     1342
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## #   carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## #   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## #   time_hour <dttm>

Select

  • -select 함수는 열을 선택하는 함수이다.
selected <- flights[, c("year", "month", "day")]
head(selected)
## # A tibble: 6 x 3
##    year month   day
##   <int> <int> <int>
## 1  2013     1     1
## 2  2013     1     1
## 3  2013     1     1
## 4  2013     1     1
## 5  2013     1     1
## 6  2013     1     1
selected <- select(flights, year, month, day)
head(selected)
## # A tibble: 6 x 3
##    year month   day
##   <int> <int> <int>
## 1  2013     1     1
## 2  2013     1     1
## 3  2013     1     1
## 4  2013     1     1
## 5  2013     1     1
## 6  2013     1     1

Mutate

  • -mutate는 새로운 열을 추가하는 함수이다.
  • -dplyr은 select를 할 때 end_with function 을 통해 해당 문자로 끝나는 컬럼을 선택할 수도 있고, : 를 통해 두 문자의 사이에 있는 컬럼을 선택할 수도 있다. 
  • -따라서 열 선택에 있어 더욱 많은 기능을 짧은 코드로 구현할 수 있으며, 새로운 변수를 정의할 때도, 한 문장에 정의할 수 있어 코드의 길이가 짧아지고 가독성이 좋아진다. 
flights_sml <- select(flights, year:day, ends_with("delay"), distance, air_time)
flights_sml_new <- mutate(flights_sml, gain=arr_delay - dep_delay, speed = distance / air_time * 60)
nrow(flights_sml_new)
## [1] 336776
  • -새 변수만을 남기고 싶다면 transmute를 사용한다. 
flights_transmute <- transmute(flights, gain = arr_delay - dep_delay,
                               hours = air_time / 60, 
                               gain_per_hour = gain / hours)
nrow(flights_transmute)
## [1] 336776

Summarize

  • -데이터 프레임을 하나의 행으로 축약한다. 
# dplyr을 사용한 방법
summarize(flights, delay = mean(dep_delay, na.rm = TRUE), delay_sd = sd(dep_delay, na.rm=TRUE))
## # A tibble: 1 x 2
##   delay delay_sd
##   <dbl>    <dbl>
## 1  12.6     40.2
  • -summarize 함수는 group_by (dplyr 에서 제공하는 함수) 와 보통 함께 사용하는 경우가 많다. 이것은 dplyr 의 이전 버전이라고 할 수 있는 plyr에서도 잘 구현되어 있었는데, dplyr 이 되면서 조금 더 이해하기 쉽게 문법이 바뀌었다. 
by_day <- group_by(flights, year, month, day)
summarize(by_day, delay = mean(dep_delay, na.rm = TRUE))
## # A tibble: 365 x 4
## # Groups:   year, month [12]
##     year month   day delay
##    <int> <int> <int> <dbl>
##  1  2013     1     1 11.5 
##  2  2013     1     2 13.9 
##  3  2013     1     3 11.0 
##  4  2013     1     4  8.95
##  5  2013     1     5  5.73
##  6  2013     1     6  7.15
##  7  2013     1     7  5.42
##  8  2013     1     8  2.55
##  9  2013     1     9  2.28
## 10  2013     1    10  2.84
## # ... with 355 more rows

파이프로 여러 작업 결합하기

  • -파이프 명령어인 %>% 를 통해 dplyr 을 통한 데이터 변형 작업을 더욱 직관적이고, 가독성있게 수행할 수 있다. 
  • -각 위치에 대해 거리와 평균 지연 사이의 관계를 탐색하고 싶은 경우, 파이프를 사용하지 않는 방법은 아래와 같다. 
by_dest <- group_by(flights, dest)
delay <- summarize(by_dest, count = n(),
                   dist = mean(distance, na.rm = TRUE),
                   delay = mean(arr_delay, na.rm = TRUE))
delay <- filter(delay, count > 20, dest != "HNL")
ggplot(data = delay, mapping = aes(x = dist, y = delay)) + 
  geom_point(aes(size = count), alpha = 1/3) + 
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

  • -파이프를 사용하면? 
  • -아래와 같이 임시 변수를 따로 놓지 않으면서 단 한 번의 logic 으로 똑같은 목적을 달성할 수 있다! 
  • -이러한 파이프 방법은 일련의 데이터 변형 작업을 의미 단위로 묶어 코드가 모듈화가 되기 때문에 유저가 코드를 쉽게 구현하고 이해할 수 있도록 도와준다. 
  • -다만 파이프가 너무 길어지면 유용한 임시변수를 두는 방법이 효율적일 수 있다. 
delay <- flights %>% 
  group_by(dest) %>%
  summarize(count = n(),
            dist = mean(distance, na.rm=TRUE),
            delay = mean(arr_delay, na.rm=TRUE)
            ) %>% 
  filter(count > 20, dest != "HNL")
ggplot(data = delay, mapping = aes(x = dist, y = delay)) + 
  geom_point(aes(size = count), alpha = 1/3) + 
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'


반응형
반응형

윈도우에서 R 업데이트 하기 


아래 방법으로 최신 버전의 R을 설치할 수 있습니다. 


install.packages("installr")

library(installr)

updateR()


이전 버전의 패키지 가져오기


1. 이전 R 버전 폴더의 모든 라이브러리들을 새로운 R 버전 폴더로 copy and paste 합니다.

저는 R 3.4.* 버전에서 R 3.5.3. 버전으로 업데이트하였는데, 경로는 아래와 같습니다. 


C:\Users\Username\Documents\R\win-library\3.4

C:\Users\Username\Documents\R\win-library\3.5


3.4 폴더에 있는 모든 라이브러리 폴더들을 3.5 폴더로 복사 붙여넣기하면 됩니다. 하지만 이렇게만 하면 패키지 버전이 호환되지 않기 때문에 제대로 로드가 안될 수도 있습니다. 따라서 3.5 폴더에 있는 라이브러리들을 업데이트 시켜줘야합니다.


2. 패키지 업데이트


update.packages(checkBuilt=TRUE)


를 입력하면 팝업 박스가 나오면서 yes no 를 선택할 수 있는데 yes 를 계속해서 클릭해줍니다. 


패키지 ‘ucminf’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘triebeard’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘ordinal’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘magic’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘lpSolve’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘RcppProgress’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘desc’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다

패키지 ‘xopen’를 성공적으로 압축해제하였고 MD5 sums 이 확인되었습니다


...... 


그러면 이런식으로 패키지를 최신 버전으로 업데이트를 알아서 해줍니다. 


3. 패키지가 모두 제대로 설치가 되었는지 확인합니다. 


version

packageStatus()


이제 최신 버전의 R을 이용할 수 있습니다. 

반응형
반응형

R - 다중회귀분석 (Multiple Linear Regression)


R 을 통해 다중회귀분석을 수행하는 기본적인 절차를 포스팅합니다. 


라이브러리 임포트


datarium 의 marketing 이라고 하는 데이터셋을 이용하여 다중회귀분석을 수행한다.

library(devtools)
# install_github("kassambara/datarium")

library(tidyverse)
library(datarium)

data("marketing", package = "datarium")
head(marketing, 4)



탐색적 자료분석 - 데이터 분포 살펴보기


가장 먼저 할 일은 데이터의 분포를 살펴보는 것이다. 

ggplot(data = marketing, aes(x = newspaper, y = sales)) +
  geom_point(size=5)




위와 같이 하나하나 산점도를 볼 수 있지만, 아래처럼 모든 변수들의 분포 (Variation)와 두 변수의 관계 (Covariation) 한 번에 볼 수 있도록 하는 라이브러리들이 많이 존재한다. 예를 들어, PerformanceAnalytics 라이브러리를 이용하면 아래와같이 한 번에 분포와 상관계수까지 구할 수 있다. 


library("PerformanceAnalytics")
chart.Correlation(marketing, histogram=TRUE, pch=19)



모델 만들기


sales = b0 + b1youtube + b2facebook + b3*newspaper 


분포를 봤을 때, youtube, facebook, newspaper 가 sales와 선형관계를 갖고 있다고 가정하고 위와 같이 다중 회귀 모델을 만들 수 있으며, 아래 R 코드를 통해 모델을 적합시킬 수 있다. 

model <- lm(sales ~ youtube + facebook + newspaper, data = marketing) summary(model)

Call:
lm(formula = sales ~ youtube + facebook + newspaper, data = marketing)

Residuals:
     Min       1Q   Median       3Q      Max 
-10.5932  -1.0690   0.2902   1.4272   3.3951 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.526667   0.374290   9.422   <2e-16 ***
youtube      0.045765   0.001395  32.809   <2e-16 ***
facebook     0.188530   0.008611  21.893   <2e-16 ***
newspaper   -0.001037   0.005871  -0.177     0.86    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.023 on 196 degrees of freedom
Multiple R-squared:  0.8972,	Adjusted R-squared:  0.8956 
F-statistic: 570.3 on 3 and 196 DF,  p-value: < 2.2e-16


결과 해석


Regression 결과를 해석하는데, 가장 먼저할 것은 F-statistics 를 확인하는 것이다. 가장 밑에 F-statistics 를 보면, p-value가 매우 작아, 최소 한 변수가 유의하다는 것을 알 수 있다. 어떤 변수가 유의한지를 확인하기 위해, 아래 coefficient table 을 보면 된다. 이 때, t-value와 p-value 는 귀무가설 평균=0 로 놓고 구한 값으로, 귀무가설로부터 얼마나 벗어난지에 관한 지표이다. 


X의 estimate 값은 다른 변수가 고정되어있을 때, X가 한 단위 변화했을 때, Y의 평균적인 변화량이다. 예를 들어, youtube의 estimate 는 0.04 인데, 이는 youtube 가 1 변화했을 때, sales의 평균적인 변화량이다. newpaper 의 경우, p-value > 0.05 이므로 이러한 estimate 의 값이 유의하지 않다는 것을 알 수 있다. 


t value 와 p-value 는 무엇인가?


$$ H_0 : \beta = 0 $$

$$ t = \frac{\hat{\beta}}{se(\hat{\beta})} $$ 


이 때 위의 t 값은 자유도가 n-2 인 t 분포를 따른다. (이렇게 되는 이유)

p-value는 이렇게 계수 추정치가 t 분포를 따른다는 사실을 이용해서 구한 값이다. 


결정계수와 F-stat 


다음과 같이 정의해보자.


$$ SSE = \sum^{n}_{i=1}{(y_i - \hat{y})^2} $$

$$ SSR = \sum^{n}_{i=1}{(\bar{y} - \hat{y_i})^2} $$

$$ SST = \sum^{n}_{i=1}{(y_i - \bar{y})^2} $$


이 때, 각각의 통계량에 대한 자유도는 아래와 같다. 


df(SSE) : n-p

df(SSR) : p-1

df(SST) : n-1


이 때, p는 intercept를 포함한 모델의 총 파라미터의 갯수이며, n은 샘플 수이다. 


$$ MSE = \sum^{n}_{i=1}{(y_i - \hat{y})^2} / (n-p) $$

$$ MSR = \sum^{n}_{i=1}{(\bar{y} - \hat{y_i})^2} / (p-1) $$

$$ MST = \sum^{n}_{i=1}{(y_i - \bar{y})^2} / (n-1) $$


귀무가설을 아래와 같이 설정하자


$$ H_0 : \beta_1 = \beta_2 = \beta_3 = ... = \beta_{p-1} $$ 


이 때, 


$$ \frac{MSE}{MSR} \sim F(n-p, p-1) $$


이를 통해 F-stat 과 p-value를 구할 수 있다. 이 값은 (explained variance) / (unexplained variance) 가 F-stat 을 따른다는 사실을 이용한 검정 방법이라고 할 수 있다. 왜 이렇게 되는지에 대해서는 이 링크를 참고.



결정계수 (Coefficient of determination, Rsquare)


결정계수는 아래와 같이 정의되며, 전체 분산 중 모델에의해 설명되는 분산의 양을  의미한다. 


$$ R^2 = \frac{SSR}{SST} $$


adjusted 결정계수를 사용하기도 하는데, 이는 아래와 같이 정의된다. 


$$ \bar{R^2} = 1-(1-R^2) \frac {n-1}{n-p} $$ 


적합된 모델의 계수

summary(model)$coefficient


새로운 모델을 아래와 같이 구할 수 있다. sales = 3.5 + 0.045youtube + 0.187facebook.


새로운 모델

model  <- lm(sales ~ youtube + facebook, data = marketing)
summary(model)

Call:
lm(formula = sales ~ youtube + facebook, data = marketing)

Residuals:
     Min       1Q   Median       3Q      Max 
-10.5572  -1.0502   0.2906   1.4049   3.3994 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  3.50532    0.35339   9.919   <2e-16 ***
youtube      0.04575    0.00139  32.909   <2e-16 ***
facebook     0.18799    0.00804  23.382   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.018 on 197 degrees of freedom
Multiple R-squared:  0.8972,	Adjusted R-squared:  0.8962 
F-statistic: 859.6 on 2 and 197 DF,  p-value: < 2.2e-16


신뢰구간

confint(model)

2.5 %97.5 %
(Intercept)2.808411594.20222820
youtube0.043012920.04849671
facebook0.172138770.20384969


위와 같이 계수의 신뢰구간을 출력할 수도 있다. 이는 t 분포를 이용한다. 

반응형
반응형


Tensorflow GPU 버전을 설치



설치버전

Cuda 9.0v

Cudnn 7.0.5v

tensorflow-gpu 1.9.0


GPU 

1080TI


운영체제

Ubuntu 16.04


설치방법

아래포스트를 기본으로 오류를 수정해나가면서 설치함

https://medium.com/@zhanwenchen/install-cuda-and-cudnn-for-tensorflow-gpu-on-ubuntu-79306e4ac04e


설치 팁

- Cuda, Cudnn을 먼저 설치하고, (이 때, Cuda Cudnn의 버전이 tensorflow 에서 지원하는지를 확인해야 함. 최신버전의 경우 tensorflow가 지원을 안하는 경우가 있을 수도 있음.) 이에 맞는 tensorflow-gpu를 설치하는 것이 좋다.

- Cuda, Cudnn, tensorflow-gpu는 서로 의존적이므로, 버전에 특히 신경 써야하고, 함부로 업그레이드 하지 않아야하고 현재 버전을 기록해 두어야한다. 

- 굳이 하나하나 Tool 별로 Document를 찾아가면서 설치하는 것보다, 먼저 설치한 사람의 설치 방법을 보고 설치하는 것이 훨씬 빠르고 안전하다. 

- 위 포스트에서 cuda와 cudnn이 설치 되었다는 것을 확인하는 방법으로 sample 코드를 실행하는 방법을 사용하였는데, 확실한 설치 여부를 알 수 있으므로 좋다. 


설치할 때 발생한 오류

1. libstdc++6 의 문제 

해결 : https://askubuntu.com/questions/671791/lib32stdc6-package-depends-on-gcc-base-but-my-installed-version-is-newer


2. tensorflow-gpu 버전 문제

pip install tensorflow-gpu를 통해 설치하니 tensorflow-gpu 1.12.0 버전이 설치됨. 근데, 이 버전에서는 구버전의 cudnn을 쓰지 않는 문제가 발생. 따라서 tensorflow-gpu를 1.9.0 버전으로 다운그레이드 함. 


설치 완료 후, Multi GPU 환경에서 default로 0번 gpu만 사용하는 문제


tensorflow를 import하기 전에 아래 문구를 삽입함으로써, 특정 gpu를 사용하도록 강제할 수 있다.


import os

os.environ["CUDA_VISIBLE_DEVICES"]= "2"   



또한 CPU를 사용하도록 강제하는 방법은 tensorflow를 import하기 전 아래 문구를 삽입하면 된다.

import os

os.environ["CUDA_VISIBLE_DEVICES"]= ""  



tensorflow의 이전 방법에서는 아래 문구를 통해 특정 gpu를 사용할 수 있었는데 1.9.0 버전에서는 아래 문구가 안 먹힌다. 


import keras.backend.tensorflow_backend as K
with K.tf.device('/gpu:2'):


반응형
반응형

R igraph 설치 오류 해결


문제 : libgfortran.so.4 오류로 인해 igraph 패키지가 설치되지 않음


해결 : 

sudo apt-get install gcc-4.8-base libgfortran4


library(devtools)

install_github("igraph/rigraph")

반응형
반응형

R Default library path 바꾸기 (공용 라이브러리 경로 설정)


.libPaths() 를 하면 현재 사용하고 있는 라이브러리 경로들이 나온다.


[1] "C:/Users/사용자이름/Documents/R/win-library/3.3"

[2] "C:/Program Files/R/R-3.3.2/library"


R은 여기서 첫 번째 element를 Default로 쓰는데, 이것은 보통 개인 폴더로 잡혀 있다. 

하지만 다중 사용자 환경에서는 내가 설치한 라이브러리를 남이 사용하도록 해야할 경우도 있는데 이때는 아래와 같이 하면 된다. 


1. R_LIBS 환경변수를 공용 라이브러리 경로로 설정함 

2. R에서 Sys.getenv("R_LIBS")를 통해 환경변수가 잘 잡혔는지를 볼 수 있다.

3. .libPaths()를 다시 입력하면 공용 라이브러리 경로가 1번으로 잡혀있는 것을 확인할 수 있다. 

 

[1] "C:/Program Files/R/R-3.3.2/library" 

[2] "C:/Users/사용자이름/Documents/R/win-library/3.3"


이후에는 패키지를 설치하면 모든 사용자가 이용할 수 있게 된다. 

반응형
반응형


Python PDPbox 패키지 설치시 문제 해결


문제


 PDPbox 패키지를 설치하던 도중 아래 에러가 발생하면서 설치가 완료되지 않음


/usr/bin/ld: cannot find -lpython3.5m

 collect2: error: ld returned 1 exit status

 error: command 'gcc' failed with exit status 1


해결


sudo apt install python3.5-dev



구글링해도 안나와서 올립니다.


도움을 얻은 페이지

https://github.com/giampaolo/psutil/issues/1143

반응형
반응형



Python 중고급 속성 정리 (3) 가변길이 인수목록 받기(*args, **kargs)


이번 포스팅에서는 알아두면 정말 유용한 가변길이 인수목록 받기를 정리해보겠습니다. 파이썬 코드를 보다보면 가끔 *args, **kargs를 보실 수 있습니다. 이것은 함수의 인자(parameter 또는 arguments)가 가변 길이일 때 사용합니다. args, kargs는 원하는 이름대로 쓸 수 있고, *, **는 각각 non-keworded arguments, keworded arguments를 뜻합니다. 


예를 들어 아래 파이썬 코드를 보시면 쉽게 이해하실 수 있습니다. 


*args


def test_var_args(f_arg, *args):
    print("first normal arg:", f_arg)
    for arg in args:
        print("another arg through *argv:", arg)

test_var_args('yasoob', 'python', 'eggs', 'test')

first normal arg: yasoob

another arg through *argv: python

another arg through *argv: eggs

another arg through *argv: test


f_arg를 통해 하나의 인자를 전달 받고, *args를 통해 가변길이 인자를 전달 받습니다. 전달받은 인자들은 함수내에서 list처럼 다룰 수 있습니다. 


또한 아래 코드처럼, 인자들을 미리 정의한 후 전달할 수도 있습니다. 이를 통해 파라미터 정의부함수 실행부를 분리할 수 있다는 장점이 있습니다. 조금 더 깔끔하게 파이썬 코드를 관리할 수 있겠죠.


param = ['yasoob', 'python', 'eggs', 'test']
test_var_args('hi', *param)


*kargs


다음으로는 **, 별표가 2개 붙은 keworded argments 사용법입니다. **kwargs로 전달받은 인자는 함수 내에서 dictionary 처럼 다룰 수 있습니다.

def greet_me(**kwargs):
    print(kwargs.items())
    for key, value in kwargs.items():
        print("{0} = {1}".format(key, value))
        
greet_me(name="yasoob", school="snu")

dict_items([('name', 'yasoob'), ('school', 'snu')])

name = yasoob

school = snu


kwargs = {"name": 'yasoob', 'school' :"snu"}
greet_me(**kwargs)

이런식으로 dict로 미리 정의한 후에 ** 를 통해 함수의 인자로 넘길 수 있습니다. 매우 유용하죠.



Decorator와 결합하여 사용하기


*args와 **kargs는 decorator와 결합하여 사용하면 더 유용하게 사용할 수 있습니다. 예를 들어, 함수를 실행할 때, 그 함수의 이름을 출력하고 함수를 실행하는 decorator를 만든다고 해봅시다. 근데, decorator 함수에서는 함수의 인자를 지정해줘야하기 때문에 decoration하는 함수의 인자의 수가 모두 같아야합니다. 이럴 때, *args와 **kargs를 이용하면 decoration 하는 함수의 인자의 길이에 상관없이 decorator를 만들 수 있습니다. 


logging_decorator는 함수를 실행할 때, 그 함수의 이름을 출력하는 기능을 갖는 decorator입니다. 이 decorator 함수에 *args, **kargs를 지정하고 이것을 그대로 원래 함수로 넘겨주면 인자의 길이에 상관없이 decorator를 구현할 수 있습니다.


Decorator와 *args, **kargs 사용 예제


이 예제에서 squared_number와 add_number는 인자의 수가 다른데 이를 *args를 이용하여 해결하였습니다. 만약 *args를 활용하지 않는다면, 각각에 대하여 decorator를 만들어야하죠. 또한 마지막 함수(add_numbers) 의 경우 함수 안에 keword를 지정하고 있습니다. 또 함수 실행 시 **kargs 를 통하여 인자를 넘기는데, 이 때 decorator에서 **kargs를 정의해 놓으면 keworded arguments 들을 그대로 decoration 함수를 전달할 수 있기 때문에 가변길이 인자를 처리할 수 있게 됩니다.

from functools import wraps

def logging_decorator(f) : 
    @wraps(f)
    def wrapper_function(*args, **kargs) : 
        print(f.__name__ + " was called") 
        print("결과 : ", f(*args, **kargs))
    return wrapper_function

@logging_decorator
def square_number(x) :
    return x**2

square_number(2)

@logging_decorator
def add_number(x,y,z=0) :
    return x+y

add_number(2,3)

@logging_decorator
def add_numbers(x=3, y=4) : 
    return x+y

add_numbers()

kwargs = {"x": 3, "y": 5}

add_numbers(**kwargs)

square_number was called

결과 :  4

add_number was called

결과 :  5

add_numbers was called

결과 :  7

add_numbers was called

결과 :  8

반응형
반응형