import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline #notebook이 실행된 브라우저에서 그림을 곧바로 볼 수 있게 해주는 코드

!pip install seaborn
# seaborn에서 제공하는 데이터셋 확인하기
sns.get_dataset_names()

# 데이터 가져올 땐? iris = sns.load_dataset('iris')

 

 

기본적인 데이터 탐색
 
 
# tabular 데이터 로딩하기
# read_csv 함수를 사용하여 파일명.csv 파일을 읽어 변수명 df에 할당
df = pd.read_csv('파일명.csv')
 
 
# 데이터프레임 인덱스 확인하기
df.index
 
# 데이터프레임 컬럼 확인하기
df.columns
 
# 데이터프레임 값(value) 확인하기
df.values
 
# 데이터프레임의 계산 가능한 값(숫자형 변수)들에 대한 통계치를 확인하기
df.describe() #df.describe(include='all'): 사용하면 문자형 변수들에 대한 통계치도 함께 확인할 수 있음.
 
 
 
 
 
# object 컬럼명 수집, cal_cols로 저장, 출력

cal_cols = df.select_dtypes('object').columns.values
cal_cols
 
 
 
 
 
# DataFrame 컬럼별로 Null값 존재 확인하기
df.isnull().sum()
 
# 원하는 컬럼 데이터별 건수 보기(값 분포 확인)
df['컬럼명'].value_counts()
 
# 그룹별 집계
df.groupby('그룹화_대상_컬럼명')['집계_대상_컬럼명'].mean() #mean, max, min 등 집계 함수 기입
 
# [참고] 원하는 컬럼 데이터별 건수에 대한 비율 보기 (정규화된 형태로 출력)
df['컬럼명'].value_counts(normalize=True)
 
 
 
# [참고] 간단하게 차트 그리기
 
# [참고] 간단하게 차트 그리기
iris['species'].value_counts().plot(kind='bar') # 컬럼 내 고유값들의 빈도를 기준으로 bar 차트 그리기
iris.plot(kind='scatter', x='sepal_length', y='petal_length') # 두 컬럼을 기준으로 산점도 그리기(hue='species' 처럼 색깔 부여할 수도 있음)
titanic['age'].plot(kind='box')  # 대상 컬럼을 box 차트로 그리기
sns.boxplot(data=titanic, x='pclass', y='age') # 대상 컬럼'들'을 box 차트로 그리기(box가 여러개 들어감), hue='컬럼명' 지정 가능

 

 

 

2. 데이터 전처리 하기

 

# 컬럼 삭제
df = df.drop(columns=['컬럼1', '컬럼2', ..., '컬럼n'], axis=1) # columns에는 삭제 대상인 컬럼 리스트 기입

# 다른 방법
df.drop(columns=['컬럼1', '컬럼2', ..., '컬럼n'], axis=1, inplace=True)

# 값 변경 후 저장
df.replace('바뀌기 전', '바뀐 후', inplace=True) # 전체 데이터 대상
df['컬럼명'].replace('바뀌기 전', '바뀐 후', inplace=True) # 해당 컬럼의 데이터 대상

# 결측치 처리 후 저장
df['컬럼명'].fillna('결측치 대신 기입할 값', inplace=True)

# 대상 컬럼의 type 확인: object 여부
df['컬럼명'].dtypes #dtype('O'): object를 의미

# 대상 컬럼의 데이터 타입 변경
df['컬럼명'] = df['컬럼명'].astype(int) #int, float, object... 등




## Tip) 최빈값으로 값 대체 시, value_counts로 최빈값 확인 후 수행

# 데이터 타입에 따른 컬럼 선택
df_object = df.select_dtypes(include='object')



# 라벨 인코딩하기
# LabelEncoder 객체를 초기화하고 le 변수에 저장
# cat_cols 데이터프레임에서 'cust_clas_itg_cd' 열의 범주형 데이터를 숫자로 인코딩하고, 그 결과를 'le_cust_clas_itg_cd' 열에 저장



from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df_object['컬럼명(라벨 인코딩 할 대상)'] = le.fit_transform(df_object['컬럼명(라벨 인코딩 할 대상)'])



# 원핫 인코딩하기
one_hot_columns = ['컬럼1', '컬럼2', ..., '컬럼n']
df_one_hot = pd.get_dummies(data=df, columns=one_hot_columns, drop_first=True)

 

 

3. 머신러닝

 

 

# 훈련 데이터셋과 테스트 데이터셋으로 분리하기
from sklearn.model_selection import train_test_split

X = df.drop(columns=['Y변수컬럼'], axis=1).values
y = df['Y변수컬럼'].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=0)
## test_size: 테스트 데이터셋의 비율
## stratify : 지정한 Data의 비율을 유지한다. 예를 들어, Label Set인 Y가 25%의 0과 75%의 1로 이루어진 Binary Set일 때, stratify=Y로 설정하면 나누어진 데이터셋들도 0과 1을 각각 25%, 75%로 유지한 채 분할




# 데이터 정규화, 표준화 스케일
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler

mms = MinMaxScaler()

X_train = mms.fit_transform(X_train)
X_test = mms.transform(X_test)



# 머신러닝 모델링 준비
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import classification_report




# 로지스틱 회귀
model = LogisticRegression(C=1.0,max_iter=2000) # c: 규제 강도, max_iter: 계산에 사용할 작업수
model.fit(X_train, y_train) # 학습 시키기
model.score(X_test, y_test) # 만든 모델로 테스트 데이터(X_test)에 대한 예측 정확도를 계산하기

# 모델 성능평가
y_pred = model.predict(X_test) # 예측값 도출하기

print(classification_report(y_test, y_pred))



# 그래프 출력
## 혼동 행렬 만들기
cf_matrix = confusion_matrix(y_test, y_pred) 
sns.heatmap(cf_matrix, annot=True, fmt='d')



# 추가 모델
## DecisionTree 모델을 만들고 학습 진행하기.(max_depth는 10, random_state는 42, 변수명은 clf로 설정)

from sklearn.tree import DecisionTreeClassifier
clf = DecisionTreeClassifier(max_depth=10, random_state=42)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)



## RandomForest 모델을 만들고 학습 진행하기.(n_estimators=100, random_state=42, 변수명은 rfc로 설정)

from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100, random_state=42)
rfc.fit(X_train, y_train)
rfc.score(X_test, y_test)




## XGBoost 모델을 만들고 학습 진행하기.(단, n_estimators=5, xgb_model로 변수명 저장)
!pip install xgboost # xgboost 라이브러리 설치

import xgboost as xgb
xgb_model = xgb.XGBClassifier(n_estimators=5)
xgb_model.fit(X_train, y_train)
xgb_model.score(X_test, y_test)




## Light GBM 모델을 만들고 학습 진행하기.(단, n_estimators=3, lgbm_model로 변수명 저장)
!pip install lightgbm # lightgbm 라이브러리 설치

from lightgbm import LGBMClassifier
lgbm_model = LGBMClassifier(n_estimators=3)
lgbm_model.fit(X_train, y_train)
lgbm_model.score(X_test, y_test)

 

 

 

4. 딥러닝

 

 

import tensorflow as tf
from tensorflow import keras

from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.utils import to_categorical


# 문항을 풀기 전에 아래 코드를 실행하기

import absl.logging
absl.logging.set_verbosity(absl.logging.ERROR)

# 하이퍼파라미터 설정하기: batch_size = 1024, epochs = 30로 주어졌을 경우에는 아래와 같이 작성

batch_size = 1024
epochs = 30

# 모델 입력(features) 갯수 확인

X_train.shape # 입력할 훈련용 데이터 셋 중 독립변수

# 모델 출력(label) 갯수 확인

y_train.shape # 입력할 훈련용 데이터 셋 중 종속변수

# 분류 딥러닝 모델 만들기

model = Sequential() # Sequential 모델 선언

model.add(Dense(64, activation='relu', input_shape=(34,))) #첫 번째 은닉층(Hidden Layer), 노드 개수를 64로~, activation_function은 'relu'가 국룰
model.add(Dropout(0.2)) # 각 은닉층(Hidden Layer)마다 Dropout을 0.2로 기입하기
## Dropout이란? overfitting을 해결하기 위한 테크닉으로, 훈련 때 은닉층의 뉴런을 무작위로 골라 삭제하면서 학습하는 방법!

model.add(Dense(32, activation='relu')) # 두번째 은닉층(Hidden Layer), 노드 개수를 32로~, activation_function은 'relu'가 국룰
model.add(Dropout(0.2)) # 각 은닉층(Hidden Layer)마다 Dropout을 0.2로 기입하기

model.add(Dense(16, activation='relu')) # 세번째 은닉층(Hidden Layer), 노드 개수를 32로~, activation_function은 'relu'가 국룰
model.add(Dropout(0.2)) # 각 은닉층(Hidden Layer)마다 Dropout을 0.2로 기입하기

model.add(Dense(1,activation='sigmoid')) # 이진 분류이므로, 최종 노드 수는 1, activation_function은 'sigmoid'로!

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) # optimizer는 adam이 국룰, 이진분류이므로 binary_crossentropy, acc
## 평가기준은?
## - loss : 훈련 손실값
## - acc : 훈련 정확도
## - val_loss : 검증 손실값
## - val_acc : 검증 정확도

es = EarlyStopping(monitor='val_loss', patience=4, mode='min', verbose=1)
## monitor: 평가항목
## mode? 평가항목의 판단 기준 => loss류에선 mode가 'min', acc류에선 mode가 'max'(손실은 최소여야하며, 정확도는 최대여야 하므로!)
## patience: 학습을 진행하며 모델 성능의 개선되지 않을 경우, 몇 번의 횟수를 지켜볼 지에 대한 횟수

mc = ModelCheckpoint('my_checkpoint.ckpt', monitor='val_loss', save_best_only=True, verbose=1) #save_best_only: 가장 성능이 좋은 모델을 저장
## ModelCheckpoint: 콜백함수로, 모델이 학습하면서 정의한 조건을 만족했을 때 Model의 weight 값을 중간 저장해줌.
## 학습시간이 오래 걸릴 때, 모델이 개선된 validation score를 도출해낼 때마다 weight를 중간 저장하여, 혹여나 발생할 수 있는 memory overflow나 crash에서도 다시 weight를 불러와서 학습을 이어나가게 해주어 시간 세이브 가능

history = model.fit(X_train, y_train, 
                    batch_size=batch_size, 
                    epochs=epochs, 
                    callbacks=[es, mc],
                    validation_data=(X_test, y_test), 
                    verbose=1) # 학습정보를 history 변수에 저장
                    
                    
                    
# [참고] 문제 유형별 딥러닝 모델 작성 방법
## 다중 분류라면? 
## 마지막 층: model.add(Dense(N, activation='softmax')): N에는 몇개로 분류하고 싶은지(2 이상: 다중 분류 이므로) 개수 기입, activation은 'softmax'
## 컴파일: model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
## 컴파일에서 loss는 데이터 셋을 라벨 인코딩 했을 경우, loss='sparse_categorical_crossentropy' 사용하고, 라벨 인코딩 했을 경우, loss='categorical_crossentropy' 사용하기!

## 회귀 문제라면? 마지막 층 activation='relu'!!



plt.figure(figsize=(10,5)) # 그림 크기 설정
plt.plot(history.history['loss'], label='Train_Loss', 'r') # 학습정보가 저장된 history 변수에서 학습정보 꺼내기 => history.history에는 학습을 진행하며 도출한 'loss', 'acc', 'val_loss', 'val_acc'이 들어있음.
plt.plot(history.history['val_loss'], label='Validaton_Loss', 'b') #'r', 'b'는 색깔 지정
plt.xlabel('Epochs') # 시도 횟수
plt.ylabel('Loss')
plt.title('Train_Loss and Validaton_Loss')
plt.legend() # plt.legend(["Train_Loss", "Validaton_Loss"]) => 이렇게 기입해줘도 OK
plt.show()

                  


# [참고] 문제 유형별 딥러닝 모델 작성 방법
## 다중 분류라면? 
## 마지막 층: model.add(Dense(N, activation='softmax')): N에는 몇개로 분류하고 싶은지(2 이상: 다중 분류 이므로) 개수 기입, activation은 'softmax'
## 컴파일: model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
## 컴파일에서 loss는 데이터 셋을 라벨 인코딩 했을 경우, loss='sparse_categorical_crossentropy' 사용하고, 라벨 인코딩 했을 경우, loss='categorical_crossentropy' 사용하기!

## 회귀 문제라면? 마지막 층 activation='relu'!!



plt.figure(figsize=(10,5)) # 그림 크기 설정
plt.plot(history.history['loss'], label='Train_Loss', 'r') # 학습정보가 저장된 history 변수에서 학습정보 꺼내기 => history.history에는 학습을 진행하며 도출한 'loss', 'acc', 'val_loss', 'val_acc'이 들어있음.
plt.plot(history.history['val_loss'], label='Validaton_Loss', 'b') #'r', 'b'는 색깔 지정
plt.xlabel('Epochs') # 시도 횟수
plt.ylabel('Loss')
plt.title('Train_Loss and Validaton_Loss')
plt.legend() # plt.legend(["Train_Loss", "Validaton_Loss"]) => 이렇게 기입해줘도 OK
plt.show()

 

 

 

오사카블루베리파이김치모코코