决策树方法的简单调用记录一下
1 clf=tree.DecisionTreeClassifier() 2 dataMat=[];labelMat=[] 3 dataPath='D:/machinelearning data/machinelearninginaction/Ch05/testSet.txt' 4 fr = open(dataPath) 5 for line in fr.readlines(): # readilnes()将文件内容存在列表里 6 lineArr = line.strip().split() # 去掉空格 7 labelMat.append(int(lineArr[-1])) 8 dataMat.append([float(lineArr[0]), float(lineArr[1])]) 9 x=np.array(dataMat) 10 y=np.array(labelMat) 11 clf.fit(x,y) 12 yHat=clf.predict(x) 13 result=np.count_nonzero(yHat==y) 14 print('正确个数',result) 15 print('正确率',result/len(yHat))
并附上介绍决策树的链接http://scikit-learn.org/stable/modules/tree.html
sklearn中自带的数据应用sklearn.datasets.load_iris()的调用以及相应的应用链接:http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html#sklearn.datasets.load_iris