擅长:python、mysql、java
<p>我将使用sklearn的<code>train_test_split</code>,它也有一个分层参数,然后将结果放入<code>dtrain</code>和{<cd3>}。在</p>
<pre><code>from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
import xgboost as xgb
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
</code></pre>
<p>请参阅此处的实现:<a href="https://www.kdnuggets.com/2017/03/simple-xgboost-tutorial-iris-dataset.html" rel="nofollow noreferrer">A Simple XGBoost Tutorial Using the Iris Dataset</a>。在</p>