https://today-1.tistory.com/17 의 캐글 데이터 house prices를 가지고 연습하는 것을 따라했습니다.
머신러닝 워크플로우 자동화
데이터탐색 - 전처리 - 모델선택 - 학습+최적화+결과분석 - 배포
일단 로컬과 환경설정 충돌이 일어나서 다시 재설치를 피하려고, 코랩에서 연습을 해보았다. 역시나 버전이 맞지 않아서 발생하는 incompatible에러가 나왔다. 그런데 밑에 코드들은 실행이 되어서 해결은 미루고 그냥 코드들 연습했다.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
xarray-einstats 0.2.2 requires numpy>=1.21, but you have numpy 1.19.5 which is incompatible.
tensorflow 2.9.2 requires numpy>=1.20, but you have numpy 1.19.5 which is incompatible.
jaxlib 0.3.22+cuda11.cudnn805 requires numpy>=1.20, but you have numpy 1.19.5 which is incompatible.
jax 0.3.23 requires numpy>=1.20, but you have numpy 1.19.5 which is incompatible.
en-core-web-sm 3.4.1 requires spacy<3.5.0,>=3.4.0, but you have spacy 2.3.8 which is incompatible.
confection 0.0.3 requires srsly<3.0.0,>=2.4.0, but you have srsly 1.0.6 which is incompatible.
cmdstanpy 1.0.8 requires numpy>=1.21, but you have numpy 1.19.5 which is incompatible.
Successfully installed Boruta-0.3 Mako-1.2.4 alembic-1.8.1 catalogue-1.0.2 databricks-cli-0.17.3 docker-6.0.1 funcy-1.17 gitdb-4.0.9 gitpython-3.1.29 gunicorn-20.1.0 htmlmin-0.1.12 imagehash-4.3.1 imbalanced-learn-0.7.0 jedi-0.18.1 kmodes-0.12.2 lightgbm-3.3.3 llvmlite-0.37.0 mlflow-1.30.0 mlxtend-0.19.0 multimethod-1.9 numba-0.54.1 numpy-1.19.5 pandas-profiling-3.4.0 phik-0.12.2 plac-1.1.3 prometheus-flask-exporter-0.21.0 pyLDAvis-3.2.2 pycaret-2.3.10 pyjwt-2.6.0 pynndescent-0.5.8 pyod-1.0.6 pyyaml-5.4.1 querystring-parser-1.2.4 requests-2.28.1 scikit-learn-0.23.2 scikit-plot-0.3.7 scipy-1.5.4 smmap-5.0.0 spacy-2.3.8 srsly-1.0.6 statsmodels-0.13.5 tangled-up-in-unicode-0.2.0 thinc-7.4.6 umap-learn-0.5.3 urllib3-1.26.12 visions-0.7.5 websocket-client-1.4.2 yellowbrick-1.3.post1
불러올 파일 변수명 = get_data("파일이름")
pd.read_csv로 불러와야 했는데, 파이캐럿에서 제공하는 데이터가 아니더라도 불러올 수 있는 파일이 있다면 get_data로 가져와졌다.
from pycaret.datasets import get_data
# 불러올 파일 변수명 = get_data("파일이름")
train = get_data("train")
print(train.shape)
test = get_data("test")
print(test.shape)
학습 데이터와 target을 설정해야 한다.
Data Type 엔터 , Train/Valid 비율 100% 사용 시 빈칸 엔터 잊지 말기!
필수 파라미터
인코딩(세 가지 인코딩 방식 지원)
from pycaret.regression import *
reg = setup(data=train,
target="SalePrice",
train_size = 0.8)
캐글의 House Prices 데이터는 평가 기준이 RMSE이다. 따라서 RMSE가 잘나온 기준으로 정렬한다. (2분 소요..)
best = compare_models(sort="RMSE")
위에서 잘 나온 gbr, lightgbm, rf 모델을 생성해본다.
gbr = create_model("gbr", cross_validation = False)
lightgbm = create_model("lightgbm", cross_validation=False)
rf = create_model("rf", cross_validation=False)
12분 소요. 랜덤포레스트가 확실히 가지를 많이 만들어서 그런가, 오래 걸린다. 파라미터로 조건을 다르게 넣으면 시간이 단축될지..
tuned_gbr = tune_model(gbr, optimize="RMSE", n_iter=10)
tuned_lightgbm = tune_model(lightgbm, optimize="RMSE", n_iter=10)
tuned_rf = tune_model(rf, optimize="RMSE", n_iter=10)
모델 3개를 앙상블
blender_specific = blend_models(estimator_list=[tuned_gbr, tuned_lightgbm, tuned_rf], optimize="RMSE")
plot_model(blender_specific)
plot_model(blender_specific, plot='error')
plot_model(blender_specific, plot='learning')
# 학습
final_model = finalize_model(blender_specific)
# 예측
pred = predict_model(final_model, data=test)
save_model, load_model
파이캐럿으로 submit 파일을 만드는 방법을 아직 찾지 못해서 기존에 하던 방식으로 사용했다. pd.read_csv로 submission파일을 로드하고, submit["SalePrice"]=pred["Label"]로 예측값을 넣어줬다.
이 때, predict 값이 평소 하던 것과 달리 데이터프레임 형식이라서 컬럼을 따로 설정을 해줘야 하는데, pred["SalePrice"]가 아니고 "Label" 컬럼으로 만들어져있다.
제출은 완료했는데, 점수는 수업 시간에 한 0.15051 보다 0.15515로 안좋게 나왔다. 수업 때 했던 것처럼 전처리를 조금 더 하면 성능이 더 좋아지려나.
공식 문서 : https://pycaret.gitbook.io/docs/
저장 및 업로드 : https://pycaret.gitbook.io/docs/get-started/functions/deploy
파이캐럿 사용법