less than 1 minute read

다항 회귀

kaggle_fish_market 을 사용하여 다항 회귀를 해보려 한다.

데이터 전처리

데이터 분리

먼저 훈련과 타겟 데이터를 나누어준다.

y=df['Species']
X=df.drop('Species', axis=1)

drop을 사용해서 1행에 있는 ‘Species’를 지워주어서 간단하게 훈련 데이터를 만들 수 있다.

Encoding

그 다음 범주형 데이터를 매핑해주려 한다.

‘Species’는 총 7가지의 물고기 종류인데

sklearn.preprocessingLabelEncoder를 사용하려 한다.

encoder = LabelEncoder()
y = encoder.fit_transform(y)
y_mapping = {index: label for index, label in enumerate(encoder.classes_)}

encoder의 fit과 transform을 같이 사용하고 enumerate()함수를 사용해서 루프를 돌려주어서 classes_의 0,1,2..값들을 label값과 매핑시켜주고

사전형으로 바꾸어서 key에 ‘Species’의 각 index값을 넣어주고 value에는 encoder로 매핑해준 값들을 넣어주어 사전을 만들어주었다.

Updated: