File indexing completed on 2024-11-25 02:29:52
0001
0002
0003
0004 import numpy as np
0005 import xgboost as xgb
0006 from sklearn import datasets
0007 from sklearn.model_selection import train_test_split
0008 from sklearn.datasets import dump_svmlight_file
0009 import joblib
0010 from sklearn.metrics import precision_score
0011
0012 iris = datasets.load_iris()
0013 X = iris.data
0014 y = iris.target
0015
0016 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
0017
0018
0019 dtrain = xgb.DMatrix(X_train, label=y_train)
0020 dtest = xgb.DMatrix(X_test, label=y_test)
0021
0022
0023 dump_svmlight_file(X_train, y_train, 'dtrain.svm', zero_based=True)
0024 dump_svmlight_file(X_test, y_test, 'dtest.svm', zero_based=True)
0025 dtrain_svm = xgb.DMatrix('dtrain.svm')
0026 dtest_svm = xgb.DMatrix('dtest.svm')
0027
0028
0029 param = {
0030 'max_depth': 3,
0031 'eta': 0.3,
0032 'silent': 1,
0033 'objective': 'multi:softprob',
0034 'num_class': 3}
0035 num_round = 20
0036
0037
0038
0039 bst = xgb.train(param, dtrain, num_round)
0040 preds = bst.predict(dtest)
0041
0042
0043 best_preds = np.asarray([np.argmax(line) for line in preds])
0044 print("Numpy array precision:", precision_score(y_test, best_preds, average='macro'))
0045
0046
0047
0048 bst_svm = xgb.train(param, dtrain_svm, num_round)
0049 preds = bst.predict(dtest_svm)
0050
0051
0052 best_preds_svm = [np.argmax(line) for line in preds]
0053 print("Svm file precision:",precision_score(y_test, best_preds_svm, average='macro'))
0054
0055
0056
0057 bst.dump_model('dump.raw.txt')
0058 bst_svm.dump_model('dump_svm.raw.txt')
0059
0060
0061
0062 joblib.dump(bst, 'bst_model.pkl', compress=True)
0063 joblib.dump(bst_svm, 'bst_svm_model.pkl', compress=True)