
https://www.kaggle.com/code/satyaprakashshukl/mushroom-classification-analysis
🌴Mushroom🎉Classification📈Analysis
Explore and run machine learning code with Kaggle Notebooks | Using data from multiple data sources
www.kaggle.com
이제 EDA를 하자!
시각화를 위해 seaborn을 데려온다.
isna()를 통해 값이 NaN이면 True, 아니면 False를 만드는데 이를 mean()에 넣으면 True는 1로 계산이 되기 때문에 여기에 mean()을 하고 100을 곱하면 "해당 column에 있는 NaN 값의 비율"을 얻을 수 있다.
주석에는 "결측값의 비율이 10% 이상"인 애들을 고른다고 썼는데 코드에는 0% 초과인 애들을 다 뽑았다. 오류긴 한데 큰 오류는 아니라서 대충 넘어가자.
아무튼 train과 test를 보면,
이런 식으로 서로 공통의 columns(ex. stem-root, veil-type, veil-color, and spore-print-color)의 결측값이 매우 높은 것을 알 수 있다. 이런 건 못 쓰겠지?
그림으로 그려보자!
(isnull()과 isna()는 동일한 기능을 수행한다.)
몇 퍼센트를 기준으로 자를지,,는 잘 모르겠다,
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OrdinalEncoder
import category_encoders as ce
missing_threshold = 0.95
high_missing_columns = df_train.columns[df_train.isnull().mean() > missing_threshold]
df_train = df_train.drop(columns=high_missing_columns)
df_test = df_test.drop(columns=high_missing_columns)
label encoder와 ordinal encoder는 categorical 데이터를 숫자로 변환할 때 사용한다. 전자는 변수를 하나만 받기 때문에 변수마다 따로 해야 하고, 후자는 한번에 여러 개의 변수를 받을 수 있다.
category_encoders는 categorical 데이터를 처리하기 위한 다양한 인코딩 방법을 제공하는 라이브러리다.
high_missing_columns는 결측값의 비율이 95%가 넘는 친구들을 모은 건데,, 그만큼 비율이 높은 건 없을 텐데?! 암튼 기준이 되는 비율은 알아서 설정하시길~
for column in df_train.columns:
if df_train[column].isnull().any():
if df_train[column].dtype == 'object':
mode_value = df_train[column].mode()[0]
df_train[column].fillna(mode_value, inplace=True)
df_test[column].fillna(mode_value, inplace=True)
else:
median_value = df_train[column].median()
df_train[column].fillna(median_value, inplace=True)
df_test[column].fillna(median_value, inplace=True)
이후 남은 column들에 대해서 결측값을 categorical column은 최빈값으로, numerical column은 중간값으로 대체한다.
이후 correlation matrix를 보자.
dython은 categorical 데이터에 대한 다양한 분석 도구를 제공하는 라이브러리다.
nominal은 특별히 명목형 변수를 다루는 데 특화되어있고, associations은 말 그대로 상관관계를 계산하는 함수다.
associations_df = associations(df_train[:10000], nominal_columns='all', plot=False)
첫 10,000개 행에 대해 상관 관계를 계산하는데, 모든 column을 categorical 변수로 간주한다. 물론 numerical 변수도 있지만 수가 적기 때문에 그냥 이렇게 했다.
암튼 matrix를 보면~~
'분명 전산학부 졸업 했는데 코딩 개못하는 조준호 > AI, ML, DL' 카테고리의 다른 글
Kaggle Competition - Binary Prediction of Poisonous Mushrooms (4) Imputing (2) | 2024.09.06 |
---|---|
Kaggle Competition - Binary Prediction of Poisonous Mushrooms (3) EDA 시각화 (0) | 2024.09.02 |
Kaggle Competition - Binary Prediction of Poisonous Mushrooms (1) EDA 전까지 (1) | 2024.09.02 |
역시 ML의 시작은 타이타닉 - (5) hyperparameter 튜닝 (8) | 2024.07.24 |
역시 ML의 시작은 타이타닉 - (4) GBT 모델 뜯어보기 (3) | 2024.07.20 |
한국은행 들어갈 때까지만 합니다
조만간 티비에서 봅시다