반응형

15일차에는 아래와 같은 매일을 받았다.

Day15~Day30 까지는 지금까지 배운 것들을 응용해서 해당 competition을 참여한다.
대회의 대략적인 개요는 아래와 같다.

해당 competition은 보험금 청구 금액 target을 예측하는 대회이다.

 

Base Line Code 제작

오늘은, 해당 competition을 위한 기본 baseline 코드를 작성해 보겠다.
feature의 정보는 비공개이고, column cat0 ~ cat9 는 범주형, cont0 ~ cont13은 연속형 데이터라는 것만 알 수 있다.

쥬피터 노트북에서 데이터를 로드하고 확인해보면, 저런식으로 구성되어있고, 우리가 예측해야 하는 값은 target이다.
train data를 y로 지정해준 다음, target column을  train data에서 drop해주고, 그 값을 features로 지정해 주었다.

# Separate target from features
y = train['target']
features = train.drop(['target'], axis=1)

# Preview features
features.head()

이제 범주형 데이터를 처리해 주어야한다. 
앞서 말했듯, column cat0 ~ cat9 는 범주형, cont0 ~ cont13은 연속형 데이터이다.

# List of categorical columns
object_cols = [col for col in features.columns if 'cat' in col]

# ordinal-encode categorical columns
X = features.copy()
X_test = test.copy()
ordinal_encoder = OrdinalEncoder()
X[object_cols] = ordinal_encoder.fit_transform(features[object_cols])
X_test[object_cols] = ordinal_encoder.transform(test[object_cols])

# Preview the ordinal-encoded features
X.head()

cat이 포함된 columns을 OrdinalEncoder()을 이용해서 처리해주었다.
X.head()를 찍어보면 범주형데이터가 아래와 같이 바뀐 것을 알 수 있다.

이제, train data를 train데이터와 validation데이터로 split 해줄 것이다.
이때, random_state=0 으로 지정해주었다.

X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.25, random_state=0)
# 총 row는 30만개, train 75%, validation 25%로 split하였음.

그리고 기본적으로 RandomForest모델을 만들어주고 fit한다.

# Define the model 
model = RandomForestRegressor(random_state=1)

# Train the model (will take about 10 minutes to run)
model.fit(X_train, y_train)
preds_valid = model.predict(X_valid)
print(mean_squared_error(y_valid, preds_valid, squared=False))

Base Line code의 mean_squared_error는 대략 0.7375가 나왔다.

sibmission을 제출해 보도록 하자.

# Use the model to generate predictions
predictions = model.predict(X_test)

# Save the predictions to a CSV file
output = pd.DataFrame({'Id': X_test.index,
                       'target': predictions})
output.to_csv('submission.csv', index=False)

오늘은 competition의 base line code를 제작해 보았다. 

이 코드를 개선시켜서 모델의 성능을 상승시켜 봐야겠다. 어떤 방법을 적용해 보면 좋을까? 벌써 설렌다.
Base Line Code를 공유한다.
https://github.com/mgkim-developer/30-Days-of-ML-with-Kaggle/blob/main/30-days-of-ml-day-15-30-Base%20Line%20Code.ipynb

 

GitHub - mgkim-developer/30-Days-of-ML-with-Kaggle: 30 Days of ML with Kaggle

30 Days of ML with Kaggle. Contribute to mgkim-developer/30-Days-of-ML-with-Kaggle development by creating an account on GitHub.

github.com

반응형