A simple call to the decision tree method records
1clf=Tree. Decisiontreeclassifier ()2datamat=[];labelmat=[]3Datapath='d:/machinelearning data/machinelearninginaction/ch05/testset.txt'4FR =Open (DataPath)5 forLineinchFr.readlines ():#readilnes () The contents of the file exist in the list6Linearr = Line.strip (). Split ()#Remove Spaces7Labelmat.append (int (linearr[-1]))8Datamat.append ([Float (linearr[0]), float (linearr[1])]) 9x=Np.array (Datamat)Teny=Np.array (Labelmat) One clf.fit (x, y) Ayhat=clf.predict (x) -Result=np.count_nonzero (yhat==y) - Print('correct number', result) the Print('correct rate', Result/len (Yhat))
View Code
Links to the decision tree are attached http://scikit-learn.org/stable/modules/tree.html
Sklearn of the data in the application Sklearn.datasets.load_iris () call and the corresponding application link: http://scikit-learn.org/stable/modules/generated/ Sklearn.datasets.load_iris.html#sklearn.datasets.load_iris
Desicionttreeclassifier () Invocation of decision tree algorithm in Sklearn and application of Sklearn packet Sklearn.datasets.load_iris ()