[python] 데이터 분석할때 필요한 코드 / 함수 정리

2022. 2. 20. 03:04·python
728x90
반응형
from IPython.core.interactiveshell import InteractiveShell # 값을 연속적으로 출력해줌
InteractiveShell.ast_node_interactivity = 'all'

import warnings # 버전 차이로 인해 출력되는 에러 문구를 무시
warnings.filterwarnings("ignore")

from IPython.display import set_matplotlib_formats # 시각화 그래프 내의 글자를 선명하게 해줌
set_matplotlib_formats("retina")

pd.options.display.max_rows=100 # 데이터프레임의 행과 열을 몇 개까지 출력해서 확인 할 것인지
pd.options.display.max_columns=100

plt.rc("font", family="Malgun Gothic") # 그래프 시각화 한글 폰트
plt.rc("axes", unicode_minus=False) # 그래프에 마이너스를 표기

from matplotlib import font_manager, rc # matplotlib 한글 깨짐 방지
font_path = "C:/Windows/Fonts/NGULIM.TTF"
font = font_manager.FontProperties(fname=font_path).get_name()
rc('font', family=font)
##### csv 파일 read_csv #####

import pandas as pd
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
submission = pd.read_csv('sample_submission.csv')

##### train_test_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 = 11)

##### OneHotEncoder #####

# sparse=False는 희소 행렬(sparse matrix)이 아닌 일반적인 배열(array) 형태로 변환된 결과를 반환하도록 지정
# handle_unknown='ignore'는 알 수 없는 카테고리가 들어왔을 때 에러를 발생시키지 않고 무시하도록 지정.  즉, 변환 시 학습 데이터에 없던 새로운 카테고리 값이 들어오는 경우, 해당 카테고리는 모두 0으로 처리

from sklearn.preprocessing import OneHotEncoder

ohe = OneHotEncoder(sparse=False, handle_unknown = 'ignore')
onehot_train = ohe.fit_transform(train_result[['Bid_class']])
onehot_frame = pd.DataFrame(onehot_train, columns = ohe.categories_[0])
train_result = pd.concat([train_result, onehot_frame], axis = 1)
train_result = train_result.drop(['Bid_class', '일괄'], axis = 1)

##### LabelEncoding #####

from sklearn.preprocessing import LabelEncoder
import numpy as np

label_col = ['Auction_class', 'addr_do', 'addr_san', 'Share_auction_YorN', 'Auction_results_left', 'Apartment_usage']

for col in label_col:
    le = LabelEncoder()
    train_result[col] = le.fit_transform(train_result[col])
    
    for label in np.unique(test_result[col]): 
        if label not in le.classes_: 
            le.classes_ = np.append(le.classes_, label)
    test_result[col] = le.transform(test_result[col])
    
##### Scaling ##### 

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
train = scaler.fit_transform(train)
test= scaler.transform(test)
728x90
반응형

'python' 카테고리의 다른 글

[python] object 형 => numerical 형태로 변경해주는 to_numeric 함수  (0) 2022.03.22
[python] 결정 트리 시각화 Graphviz 설치  (0) 2022.03.20
[python] apply, lammda 함수의 활용  (0) 2022.03.18
[python] 카테고리형(categorical) => 수치형(numerical) 데이터로 변경하는 2가지 방법  (0) 2022.03.18
[python] 파이썬 클래스 python class  (0) 2022.02.22
'python' 카테고리의 다른 글
  • [python] 결정 트리 시각화 Graphviz 설치
  • [python] apply, lammda 함수의 활용
  • [python] 카테고리형(categorical) => 수치형(numerical) 데이터로 변경하는 2가지 방법
  • [python] 파이썬 클래스 python class
ISFP의 블로그
ISFP의 블로그
이건 첫 번째 레슨, 업무에서 마주친 문제 해결 경험 공유하기 이건 두 번째 레슨, 개인적으로 공부한 데이터/AI 지식을 기록하기 이건 세 번째 레슨, 다른 사람과 비교하지 말고 오직 어제의 나와 비교하기
  • ISFP의 블로그
    resultofeffort
    ISFP의 블로그
  • 전체
    오늘
    어제
    • 분류 전체보기 (117)
      • python (25)
      • pythonML (27)
      • Linux (0)
      • 오류Error (8)
      • information (7)
      • Deep learning (5)
      • pytorch (29)
      • 코딩테스트 (4)
      • 밑바닥 DL (4)
      • 논문 리뷰 (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    machinelearning
    머신러닝
    Python
    분류
    konlpy
    오블완
    deeplearning
    인공지능
    Pandas
    자연어처리
    nlp
    티스토리챌린지
    텍스트전처리
    pytorch
    Deep Learning
    딥러닝
    데이터분석
    cnn
    Ai
    토큰화
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
ISFP의 블로그
[python] 데이터 분석할때 필요한 코드 / 함수 정리
상단으로

티스토리툴바