import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import preprocessing
from sklearn import tree
from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import preprocessing
from numpy import genfromtxt
from sklearn.metrics import *

def draw(x_data,y_data,model,plot_=0):
if plot_ == 0 :
x_min,x_max=x_data[:,0].min()-1,x_data[:,0].max()+1
y_min,y_max=x_data[:,1].min()-1,x_data[:,1].max()+1
xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))

z = model.predict(np.c_[xx.ravel(),yy.ravel()])
z = z.reshape(xx.shape)

cs = plt.contourf(xx,yy,z)
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
plt.show()
predictions = model.predict(x_data)
return (classification_report(predictions,y_data))

def graphviz(model):
import graphviz
dot_data = tree.export_graphviz(model,out_file=None,feature_names = ["x","y"],class_names=["l1","l2"],filled=True,rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph.render("new")

def main():
plot_=0
data = genfromtxt("LR-testSet.csv",delimiter=",")
x_data = data[:,:-1]
y_data = data[:,-1]
plt.scatter(x_data[:,0],x_data[:,1],c=y_data)
plt.show()
model = tree.DecisionTreeClassifier()#基尼系数
model.fit(x_data,y_data)
graphviz(model)
result=draw(x_data,y_data,model)
print ("result:",result)
print ("end".center(10,"-"))


main()

决策树_线性分类_数据集

决策树_线性分类_数据集_02


决策树_线性分类_数据集_03

数据集

决策树_线性分类_数据集_04