Python Package Introduction
Python 패키지는 기본 인터페이스, scikit-learn 인터페이스 및 dask 인터페이스를 포함하여 3가지 다른 인터페이스로 구성된다. Dask 인터페이스에 대한 소개는 Distributed XGBoost with Dask를 참조하면 된다.
import xgboost as xgb
데이터 인터페이스
| NumPy 2D array SciPy 2D sparse array Pandas data frame cuDF DataFrame cupy 2D array dlpack datatable XGBoost binary buffer file LIBSVM text format file Comma-separated values (CSV) file |
데이터는 DMatrix 개체에 저장된다.
1) NumPy 배열 로드
data = np.random.rand(5, 10) # 5 entities, each contains 10 features
label = np.random.randint(2, size=5) # binary target
dtrain = xgb.DMatrix(data, label=label)
2) scipy.sparse 배열 로드
csr = scipy.sparse.csr_matrix((dat, (row, col)))
dtrain = xgb.DMatrix(csr)
3) Pandas 데이터 프레임 로드
data = pandas.DataFrame(np.arange(12).reshape((4,3)), columns=['a', 'b', 'c'])
label = pandas.DataFrame(np.random.randint(2, size=4))
dtrain = xgb.DMatrix(data, label=label)
4) XGBoost 바이너리 파일에 저장 DMatrix하면 로딩 속도가 빨라진다.
dtrain = xgb.DMatrix('train.svm.txt')
dtrain.save_binary('train.buffer')
5) 누락된 값은 DMatrix 생성자의 기본값으로 대체될 수 있다.
dtrain = xgb.DMatrix(data, label=label, missing=np.NaN)
6) 필요한 경우 가중치를 설정할 수 있다.
w = np.random.rand(5, 1)
dtrain = xgb.DMatrix(data, label=label, missing=np.NaN, weight=w)
랭킹 작업을 수행할 때 가중치의 수는 그룹의 수와 같아야 한다.
7) LIBSVM 텍스트 파일 또는 XGBoost 바이너리 파일 로드
dtrain = xgb.DMatrix('train.svm.txt')
dtest = xgb.DMatrix('test.svm.buffer')
XGBoost의 파서는 기능이 제한되어 있다. Python 인터페이스를 사용할 때 load_svmlight_fileXGBoost의 내장 파서보다 sklearn 또는 기타 유사한 유틸리티를 사용하는 것이 좋다.
8) CSV 파일 로드
# label_column specifies the index of the column containing the true label
dtrain = xgb.DMatrix('train.csv?format=csv&label_column=0')
dtest = xgb.DMatrix('test.csv?format=csv&label_column=0')
XGBoost의 파서는 기능이 제한되어 있다. Python 인터페이스를 사용할 read_csv때 XGBoost의 내장 파서보다 팬더 또는 기타 유사한 유틸리티를 사용하는 것이 좋다.
https://xgboost.readthedocs.io/en/stable/python/python_intro.html
Python Package Introduction — xgboost 1.6.0 documentation
© Copyright 2021, xgboost developers. Revision f75c007f.
xgboost.readthedocs.io
'Learning-driven Methodology > ML (Machine Learning)' 카테고리의 다른 글
| [XGBoost] Python Package Introduction (3) (0) | 2022.05.09 |
|---|---|
| [XGBoost] Python Package Introduction (2) (0) | 2022.05.09 |
| [XGBoost] 트리 부스팅 (Tree Boosting) (2) (0) | 2022.05.06 |
| [XGBoost] 트리 부스팅 (Tree Boosting) (1) (0) | 2022.05.06 |
| [XGBoost] 의사결정 트리 앙상블 (Decision Tree Ensembles) (0) | 2022.05.06 |