Certificate/data analytics-Google

Input validation, validate data, EDA, label encoding, dummy encoding, duplicated(), drop_duplicates(), replace, loc

Olivia-BlackCherry 2023. 7. 11. 12:42

목차

    Input Validation

    The practice of thoroughly analyzing and double-checking to make sure data is complete, error-free, and high-quality

    카레를 만들기 위해 야채를 산다고 했을 때, 살 때만 야채의 신선도를 확인하는 것이 아니라 냉장고에 넣을 때, 꺼내서 요리할 때, 며칠 후 남은 야채의 양을 확인할 때 등 수시로 야채가 신선한지 아닌지 계속 확인한다. 데이터도 마찬가지이다. 계속적으로 데이터의 상태를 확인해야한다. 데이터가 깨끗한지, 윤리적인지, 올바른지를 체크한다.

     

    Why validate data?

    왜 데이터를 확인해야할까?

    - 비즈니스에서 정확한 의사결정을 돕는다.

    - 모델성능을 향상시킨다.

    - 차후 문제 발생 소지를 줄인다.

     

    Questions to ask while validating data?

    무엇을 확인할까?

    - 입력형식이 동일한 형식일까?

    - 같은 범위 안에 있을까?

    - 데이터 타입이 같을까?

    - join할 것이 있을까?

     

     

    companies df

     

    중복

    1) duplicated(subset=?, keep=False)

     중복인 행  확인하기

    duplicated() 함수는 pd나 series에서 중복된 row를 찾는데 사용한다.

    keep 매개변수는 세 가지 가능한 값을 갖는다. 

    - keep='first' 첫 번째 나타난 값 유지, 나머지 중복 값을 제거

    - keep='last' 마지막으로 나타난 값을 유지하고, 나머지 중복값을 제거

    - keep=False 모든 중복값을 제거

     

     

     

     

    2) drop_duplicates()

     중복된 행을 삭제한다. 

    subset은 company이고, keep은 first이다.

    companies=companies.drop_duplicates(subset=['Company'], keep='first')

     

     

    대체

    1) replace() 

    # List provided by the company of the expected industry labels in the data
    industry_list = ['Artificial intelligence', 'Other','E-commerce & direct-to-consumer', 'Fintech',\
           'Internet software & services','Supply chain, logistics, & delivery', 'Consumer & retail',\
           'Data management & analytics', 'Edtech', 'Health', 'Hardware','Auto & transportation', \
            'Travel', 'Cybersecurity','Mobile & telecommunications']

     

    집합의  차집합을 이용해서 어떤 이름이 다른가를 확인한다. 

    set(companies['Industry']).difference(set(industry_list))

     

    replace(a, b)로 바꾸거나, 

    companies['Industry']=companies['Industry'].replace('Artificial Intelligence','Artificial intelligence')

    딕셔너리를 만들어서 바꾼다. 

    replacement_dict={'Data management and analytics':'Data management & analytics', 'FinTech':'Fintech'}
    companies['Industry']=companies['Industry'].replace(replacement_dict)

     

     

    2)  loc 쓰기

    값을 바로 부여할 수도 있다.

    companies.loc[companies['Company']=='InVision', 'Year Founded']=2011

     

     

     

    범주형 데이터> 숫자형 데이터로 변환

    1. Label encoding(순서형)

    순서가 중요한 경우이다.

    ex) class 카테고리의 값이 freshman, sophomore, junnior, senior이라면 이것은 순서가 중요하다. 

     

    2. Label encoding(명목형)

    순서가 중요하지 않다.

    ex) subject 카테고리에 math, science, korean이 있을 때이다. 

     

    3) Dummy encoding(원핫인코딩)

    각 범주에 대한 0,1로만 이루어진 숫자가 나열된다.

     

     

     

    데이터분석, EDA, 데이터시각화