path ='iris.data' # 数据文件路径
data = pd.read_csv(path, header=None)
x = data[[0, 1]]
# y = pd.Categorical(data[4]).codes
y = LabelEncoder().fit_transform(data[4])
# 为了可视化,仅使用前两列特征
x = x.iloc[:, :2]
# x = x[[0,1]]
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7, random_state=1)
# 决策树参数估计
# min_samples_split = 10:如果该结点包含的样本数目大于10,则(有可能)对其分支
# min_samples_leaf = 10:若将某结点分支后,得到的每个子结点样本数目都大于10,则完成分支;否则,不进行分支
model = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=3)
model.fit(x_train, y_train)
y_test_hat = model.predict(x_test) # 测试数据
print('accuracy_score:', accuracy_score(y_test, y_test_hat))
网友评论