전체 글(116)
-
리스트 순서 유지하면서 중복 제거 방법
arr = [6, 5, 6, 4, 4, 1, 1, 2, 3, 9, 8, 7, 9, 8, 7] result1 = dict.fromkeys(arr) # 리스트 값들을 key 로 변경 print(result1) result2 = list(result1) # list(dict.fromkeys(arr)) print(result2) dict.fromkeys(리스트 자료형)을 이용해서 중복이 제거된 키 값들로 변환된 것을 볼 수 있습니다. list(dict.fromkeys(리스트 자료형)을 이용해서 키 값으로 변환된 것들을 다시 리스트로 변환하는 것을 볼 수 있습니다. 이방법 또한 순서가 유지되는 것을 볼 수 있습니다.
2022.05.12 -
딕셔너리를 활용해서 변수 생성
import pandas as pd df = pd.DataFrame({'col1': [1, 2, 2, 3, 1], 'col2': ['negative', 'positive', 'neutral', 'neutral', 'positive']}) conversion_dict = {'negative': -1, 'neutral': 0, 'positive': 1} df['converted_column'] = df['col2'].replace(conversion_dict) print(df.head()) 결과) col1 col2 converted_column 0 1 negative -1 1 2 positive 1 2 2 neutral 0 3 3 neutral 0 4 1 positive 1
2022.04.05 -
Optuna 파라미터 튜닝
import optuna from optuna.samplers import TPESampler def objective(trial): param = { "random_state":42, 'learning_rate' : trial.suggest_loguniform('learning_rate', 0.01, 0.2), 'bagging_fraction' :trial.suggest_float('bagging_fraction', 0.0, 1.0), "n_estimators":trial.suggest_int("n_estimators", 1, 1000), "max_depth":trial.suggest_int("max_depth", 1, 16), "num_leaves":trial.suggest_int("num_leave..
2021.12.13 -
class balanced weight (Cost-sensitive learning)
Cost sensitive learning은 데이터 자체를 생성하진 않는다. 다만 머신러닝을 학습할 때 소수의 클래스에 대한 cost값에 가중치를 더 많이 주어 균형 잡힌 학습이 가능하게 하는 방법이다. 이것저것 다 사용해본 결과 어떤 때는 Cost-sensitvie learning이 더 효과가 좋았고, 어떨 때는 알고리즘을 이용한 오버 샘플링이 좋았다. 둘 다 사용해보고 상황에 맞는 결정을 해야 할 듯하다. 아래와 같이 랜덤 포레스트를 실행해서 가중치를 준 것과 안 준 것을 비교해보자. 크지는 않지만 가중치를 주었을 때 정확도가 더 높다! 랜덤 포레스트도 뿐만 아니라 xgboost에서는 scale_pos_weight 나 weight 같은 파라미터로 지정해 줄 수 있다.
2021.12.13 -
중복 데이터 확인
print(set(x['주구매상품'].unique()) - set(validation['주구매상품'].unique())) print(set(validation['주구매상품'].unique()) - set(x['주구매상품'].unique())) * set을 통해 확인 가능
2021.12.04 -
Anomaly detection (IQR을 통한 이상치 처리)
특정 변수의 데이터 전체를 이상치 처리 하는 것 보다, 특정 레이블에 해당 될 때의 데이터를 삭제하는게 효과가 존재 def remove_outlier_test(d_cp,column): o_df = d_cp[column] quan_25 = np.percentile(o_df.values,25) quan_75 = np.percentile(o_df.values,75) iqr = quan_75 - quan_25 iqr = iqr * 1.5 lowest = quan_25 - iqr highest = quan_75 + iqr outlier_index = o_df[(o_df highest)].index d_cp = d_cp.drop(outlier_index, axis=0) return d_cp a1_out = remo..
2021.12.01