from sklearn.datasets import load_iris
import xgboost as xgb
import pandas as pd
from xgboost import plot_importance
from matplotlib import pyplot as plt
from sklearn import preprocessing
train_num=50
data_re = pd.read_excel("温带.xlsx",sheet_name='test')
data = data_re.iloc[:62, 1:11].copy().values #读取值,包含label
min_max_scaler = preprocessing.MinMaxScaler()
data = min_max_scaler.fit_transform(data)
train = data[0:train_num] #训练数据
print(train[0])
test = data[:] #测试数据
#读label值
y = data_re.iloc[:62,11].copy().values #读取label值。
y_train = y[0:train_num] #训练的label
y_test = y[:]
model = xgb.XGBClassifier(max_depth=50, learning_rate=0.05, n_estimators=200, silent=True, objective='multi:softmax')
#n_estimators为随机森林的数目
model.fit(train, y_train)
对测试集进行预测
ans = model.predict(test)
print(ans)
plot_importance(model)
plt.show()
plt.plot(y_test,'ro-')
plt.show()
plt.plot(ans, 'g*:', ms=10)
plt.show()