본문 바로가기

python3

(9)
[ML] XGBClassifier sample code # First XGBoost model for Pima Indians dataset from numpy import loadtxt from xgboost import XGBClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # load data dataset = loadtxt('pima-indians-diabetes.csv', delimiter=",") # split data into X and y X = dataset[:,0:8] Y = dataset[:,8] # split data into train and test sets seed = 7 test_size = ..
[pandas] MinMaxScaling sample code import pandas as pd a = pd.read_csv('data/mtcars.csv', index_col=0) # case1 min_data = a['qsec'].min() max_data = a['qsec'].max() a['qsec2'] = (a['qsec'] - min_data) / (max_data - min_data) result = len(a['qsec2'] > 0.5) print(result) # case2 from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() a['qsec3'] = scaler.fit_transform(a[['qsec']]) result2 = len(a['qsec3']>0.5) print(r..
[pandas] dataframe filter import pandas as pd df = df[df.col1 == 'value1']
[json] pandas dataframe to json file / json file read, write, append 1. pandas dataframe -> json file df.to_json("filename.json", default_handler=str) df.to_json("filename.json", oriend='records', default_handler=str) 2. json file -> pandas dataframe df = pd.read_json('filename.json') ** large file ** import sys maxparse = sys.maxsize while True: try: df = pd.read_json("filename.json", dtype=object) break except OverflowError: maxparse = int(maxparse/10) 3. json ..
[pandas] dataframe get row value / 데이터 전처리 pandas dataframe for idx, member in df.iterrows(): print(idx, member) for i, member in df.iterrows(): base_dt = datetime.datetime.strptime(df.loc[i]['base_dt'],"%Y%m%d").strftime("%Y-%m-%d") count = int(df.loc[i]['COUNT(*)'].toString())
[반복문] skip 시 pass/continue pass : 실행할 코드가 없는 경우 continue : 다음 순번 loop 로 넘어감 for i in list: if "data" in i: pass print(i) -> i에 "data"가 포함되어도 print(i) 출력됨 for i in list: if "data" in i: continue print(i) -> 에 "data"가 포함되면 다음 loop가 돌게됨
[type] java.math.BigDecimal to integer pandas dataframe column type : java.math.BigDecimal intValue = int(df['col'][0].toString())
[pandas] columns rename df.columns=['new_nm1','new_nm2'] df.rename(columns = {'old_nm' : 'new_nm'), inplace = True)
[pandas] 행 데이터 가져오기 loc, iloc / get value 특정값 추출 loc : 인덱스 기준으로 행 읽기 iloc : 행번호 기준으로 행 읽기 df.loc[0] #인덱스 0인 행 데이터 df.iloc[-1] #마지막 행 데이터 df.loc[0,1,2] #여러개의 행 데이터 df.loc[[0,1],['col1','col2']] #특정 행 데이터의 특정 열 데이터 df.loc[1][col2] : 인덱스1번 행의 col2 값