- 生成唯一目录或文件名
'checkpoints-{:s}-{:s}-{:s}'.format( time.strftime('%Y%m%d%H%M%S'), other_name, str(uuid.uuid4()).split('-')[0] )
- xgb分类
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
param = {}
param['eta'] = 0.01
param['max_depth'] = 6
param["booster"] = "gbtree"
param['silent'] = 1
param['nthread'] = 4
param["silent"] = 1
param["min_child_weight"] = 3
param['num_class'] = 5
watchlist = [(dtrain, 'train'), (dtest, 'test')]
num_round = 7
# train1
#param['objective'] = 'multi:softmax' # shape: (5080,), 直接输出标签[0,1,3,..]
#bst = xgb.train(param, dtrain, num_round, watchlist)
# train2
param['objective'] = 'multi:softprob' # shape: (5080, 5), 输出nclass 概率值[[ 0.20230168 0.14427288 0.29164004 0.20053586 0.16124953], [...]]
bst = xgb.train(param, dtrain, num_round, watchlist)
# get prediction1
#pred = bst.predict(dtest)
#error_rate = np.sum(pred != y_test.squeeze(axis=1).values)*1.0 / y_test.shape[0]
#print('Test error using softmax = {}'.format(error_rate))
# get prediction2
pred_prob = bst.predict(dtest).reshape(y_test.shape[0], 5)
pred_label = np.argmax(pred_prob, axis=1)
error_rate = np.sum(pred_label != y_test.squeeze(axis=1).values)*1.0 / y_test.shape[0]
print('Test error using softprob = {}'.format(error_rate))
# get feature importance
importance = bst.get_score(importance_type='weight')
sorted_importance = sorted(importance.items(), key=operator.itemgetter(1), reverse=True)
print('feature importances[weight]:')
print(sorted_importance)
print('-'*30)
gains = bst.get_score(importance_type='gain')
sorted_gains = sorted(gains.items(), key=operator.itemgetter(1), reverse=True)
print('feature gains:')
print(sorted_gains)
# model save
os.system("mkdir -p models")
model_save_file = 'bst-speedcamera-{:s}-{:.4f}-{:s}.pth'.format(time.strftime('%Y%m%d%H%M%S'), 1-error_rate, str(uuid.uuid4()).split('-')[0])
bst.save_model(os.path.join('models', model_save_file))
print('model saved to: {:s}'.format(model_save_file))
print('-'*30)
print('elapsed time: {:d} s'.format(int(time.time()-since)))
# model predict
# get prediction
dpred = xgb.DMatrix(df[feature_cols], label=None)
pred_probs = bst.predict(dpred)
pred_labels = np.argmax(pred_probs, axis=1)
bash error trap
#!/usr/bin/env bash
err_report() {
echo "error! failed at line $(caller)"
exit -1
}
trap err_report ERR
tar zcvf xxx.tar.gz v1 v2 v3
echo "success"
输出:
tar: v3: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors.
error! failed at line 7 ./run.sh















网友评论