课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
5分钟

Scikit-Learn API-示例

class SKLTest:
  def __init__(self):
    df = pd.read_csv('./data/iris.csv')
    _feature_names = ['Sepal Length', 'Sepal Width', 'Petal Length', 'Petal Width']
    x = df[_feature_names]
    y = df['Class'].map(lambda x: _label_map[x])
​
    self.train_X, self.test_X, self.train_Y, self.test_Y = \
      train_test_split(x, y, test_size=0.3, stratify=y, shuffle=True, random_state=1)
      
  def train_test(self):
    clf=xgt.XGBClassifier(max_depth=3,learning_rate=0.1,n_estimators=100)
    clf.fit(self.train_X,self.train_Y,eval_metric='auc',
            eval_set=[( self.test_X,self.test_Y),],
            early_stopping_rounds=3)
    # 训练输出:
    # Will train until validation_0-auc hasn't improved in 3 rounds.
    # [0]   validation_0-auc:0.933333
    # ...
    # Stopping. Best iteration:
    # [2]   validation_0-auc:0.997778
    print('evals_result:',clf.evals_result())
    # evals_result: {'validation_0': {'auc': [0.933333, 0.966667, 0.997778, 0.997778, 0.997778]}}
    print('predict:',clf.predict(self.test_X))
    # predict: [1 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0]