机器学习之决策树:二、算法案例

一、鸢尾花数据集决策树分类

from sklearn.datasets import load_iris
from sklearn import tree  
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.feature_extraction import DictVectorizer
import random

# 导入数据集
iris = load_iris()
# 特征
iris_feature = iris.data
# 分类标签
iris_label = iris.target

# 数据集划分
#x_train,x_test,y_train,y_test = train_test_split(iris_feature,iris_label,test_size=0.4)

#随机打乱数据
data_size = iris.data.shape[0]
index = [i for i in range(data_size)] 
random.shuffle(index)  
iris.data = iris.data[index]
iris.target = iris.target[index]

#切分数据集
test_size = 40#总共150个数据,三种各50
x_train = iris.data[test_size:]
x_test =  iris.data[:test_size]
y_train = iris.target[test_size:]
y_test = iris.target[:test_size]


model = tree.DecisionTreeClassifier()
# 模型训练
model.fi

你可能感兴趣的:(机器学习,决策树,可视化,机器学习,python)