728x90

분류 전체보기 93

[Deep Learning]tensorflow로 Dnn 모델 쌓기

- Import & 환경설정 import tensorflow as tf from tensorflow.keras import utils import gc # 연산방식 설정 # GPU 또는 CPU multi training 가능하게 설정 from tensorflow.compat.v1 import ConfigProto # Configproto의 session을 default session으로 설정 from tensorflow.compat.vq impofg InteractiveSession config = ConfigProto() # gpu memory 0.1씩 할당 config.gpu_option.per_process_gpu_memory_fraction = 0.1 session = InteractiveSess..

[Python]for문 전역 변수 일괄 적용, globals()

for문 진행 시 전역 변수를 일괄로 적용할 수 있는 함수입니다. 저는 주로 특정 문자로 시작하는 변수들로 이루어져 있는 데이터 프레임을 만들 때 사용합니다. 먼저, titanic 데이터를 불러옵니다. from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split, StratifiedKFold import pandas as pd # data load df = pd.read_csv('train.csv') df.head() 이제, 전역 변수를 설정합니다. 저는 A, P, C로 시작하는 변수들로 이루어진 데이터..

Code/Python 2022.11.04

[Python] list 합집합, 교집합, 차집합, 대칭차집합

데이터 원소 li1 = ['A', 'B', 'C', 'D'] li2 = ['C', 'D', 'E', 'F'] 1) 합집합 union = list(set(li1 + li2)) print(union) union1 = list(set(li1) | set(li2)) print(union1) union2 = list(set().union(li1, li2)) print(union2) 2) 교집합 inter = list(set(li1) & set(li2)) print(inter) inter1= list(set(li1).intersection(li2)) print(inter1) 3) 차집합 comp = list(set(li1) - set(li2)) print(comp) comp1 = list(set(li1).diffe..

Code/Python 2022.11.03

[Python]DataFrame 정렬, sort_values() / sort_index(), 다중 정렬

DataFrame을 Data 기준에 대해 알아보겠습니다. - sort_values() titanic 데이터를 load 해줍니다. import pandas as pd # data load df = pd.read_csv('train.csv') Pclass 기준으로 데이터를 정렬합니다. option으로 by를 사용하여 기준 컬럼을 설정할 수 있습니다. df.sort_values(by = 'Pclass') Pclass 기준으로 오름차순 정렬이 됐습니다. Pclass 기준으로 내림차순 정렬을 하겠습니다. ascending = False로 설정하여 내림차순을 할 수 있습니다. df.sort_values(by = 'Pclass', ascending = False) - 다중 정렬 by option을 통해 다중 정렬도 ..

Code/Python 2022.11.02

[Machin Learning]변수 중요도를 기준으로 Kfold 교차검증 진행하기

변수 중요도를 기준으로 변수를 하나씩 늘려가며 Kfold 교차검증을 진행하는 코드입니다. 모듈 import 및 data load from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split, StratifiedKFold import pandas as pd # data load df = pd.read_csv('train.csv') trian set 설정 # object column 제외 df = df.select_dtypes(exclude= 'object') # Nan 값 평균 값으로 처리 df.filln..

Machine Learning 2022.11.01

[Machine Learning]변수 중요도 출력(feature importance)

학습 데이터 중 모든 변수를 사용하면 노이즈 데이터가 섞여서 모델 성능이 잘 나오지 않을 수 있습니다. 모델 성능을 높이기 위해 변수를 선택하는 과정을 거쳐야 하는데, 변수 중요도를 활용하여 선택하는 방법이 있습니다. RandomForest를 이용하여 titanic데이터를 학습한 후 변수 중요도를 출력하겠습니다. 먼저, titanic 데이터를 불러와줍니다. import pandas as pd df = pd.read_csv('train.csv') df.head() RandomForest를 이용하여 학습을 진행합니다. 학습을 위해 object column은 사용하지 않습니다. Nan 값은 평균값으로 처리해줍니다. # object column 제외 df = df.select_dtypes(exclude= 'ob..

Machine Learning 2022.11.01

[Python]중복값 확인(데이터가 동일한 row, column 찾기)

- 동일한 row 찾기 row 같은 경우에는 아래 코드를 사용하여 중복된 row를 확인할 수 있습니다. df[df.duplicated()] *option으로 keep = ['first', 'last', 'False']를 설정할 수 있습니다. first: 중복된 row 중 첫번째 row를 남깁니다. last: 중복된 row 중 마지막 row를 남깁니다. False: 중복된 row 전체를 남깁니다. - 동일한 column 찾기 하지만 동일한 데이터 값을 가진 컬럼을 찾는 것은 별도로 함수가 없기때문에 전치를 해준 후 DataFrame.duplicated() 통해 찾을 수 있습니다. import pandas as pd import numpy as np # 전치는 .T 또는 np.transpose()를 사용하면..

Code/Python 2022.10.31
728x90