Commit b8eae307 authored by linfang.wang's avatar linfang.wang

加入MySQL 连接

parent 313190e7
...@@ -32,14 +32,6 @@ def split_train_val(df, trainsplit = 'random', trainsplitRatio = 0.8, sort_col=N ...@@ -32,14 +32,6 @@ def split_train_val(df, trainsplit = 'random', trainsplitRatio = 0.8, sort_col=N
val = None val = None
return train,val return train,val
# log 损失函数
def logregobj(preds, dtrain):
labels = dtrain.get_label()
preds = 1.0 / (1.0 + np.exp(-preds))
grad = preds - labels
hess = preds * (1.0 - preds)
return grad, hess
def buildClf(max_depth=2,learning_rate=0.1, n_estimators=5000, gamma=0, def buildClf(max_depth=2,learning_rate=0.1, n_estimators=5000, gamma=0,
min_child_weight=1, max_delta_step=0, subsample=0.8, colsample_bytree=0.8, reg_alpha=0, reg_lambda=1, min_child_weight=1, max_delta_step=0, subsample=0.8, colsample_bytree=0.8, reg_alpha=0, reg_lambda=1,
...@@ -105,12 +97,12 @@ def automodelfit(clf,param_grid,dftrain,features,resp, kfold=10,scoring='roc_auc ...@@ -105,12 +97,12 @@ def automodelfit(clf,param_grid,dftrain,features,resp, kfold=10,scoring='roc_auc
return grid_search return grid_search
def modelfit(clf, dftrain, features, resp, dfval=None,useTrainCV = True, cv_folds=10, eval_metric='auc',early_stopping_rounds=20): def modelfit(clf, dftrain, features, resp,useTrainCV = True, cv_folds=10, eval_metric='auc',early_stopping_rounds=20):
''' '''
模型训练 模型训练
:type useTrainCV: object
:param clf:XGBClassifier :param clf:XGBClassifier
:param dftrain:训练集 :param dftrain:训练集
:param dfval 验证集用于模型训练,early_stopping_rounds
:param features: 特征 :param features: 特征
:param resp:label :param resp:label
:param useTrainCV:if True call cv function,目的是调节参数 n_estimators :param useTrainCV:if True call cv function,目的是调节参数 n_estimators
...@@ -126,13 +118,7 @@ def modelfit(clf, dftrain, features, resp, dfval=None,useTrainCV = True, cv_fold ...@@ -126,13 +118,7 @@ def modelfit(clf, dftrain, features, resp, dfval=None,useTrainCV = True, cv_fold
metrics=eval_metric, early_stopping_rounds=early_stopping_rounds,verbose_eval=True) metrics=eval_metric, early_stopping_rounds=early_stopping_rounds,verbose_eval=True)
clf.set_params(n_estimators=cvresult.shape[0]) clf.set_params(n_estimators=cvresult.shape[0])
# Fit the algorithm on the data and save the model clf.fit(dftrain[features], dftrain[resp],eval_metric=eval_metric)
if not dfval==None:
#== 如果有验证集的话,则无需进行cv运算 基于验证集auc early_stopping_rounds
clf.fit(dftrain[features], dftrain[resp],eval_set=[(dftrain[features], dftrain[resp]), (dfval[features], dfval[resp])],
eval_metric=eval_metric,early_stopping_rounds=early_stopping_rounds)
else:
clf.fit(dftrain[features], dftrain[resp],eval_metric=eval_metric)
return clf return clf
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment