zoukankan      html  css  js  c++  java
  • 《机器学习》周志华 习题答案4.3

      原题,对西瓜数据集用决策树来进行划分,此处我只选取了西瓜的密度和含糖率这两个连续属性来进行划分,

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import colors
    from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
    
    file1 = open('c:quantwatermelon.csv','r')
    data = [line.strip('
    ').split(',') for line in file1]
    X = [[float(raw[-3]), float(raw[-2])] for raw in data[1:]]
    y = [1 if raw[-1]=='1' else 0 for raw in data[1:]]
    X = np.array(X)
    y = np.array(y)
    print y
    #######################################################################以上是西瓜
    from sklearn.datasets import load_iris
    from sklearn import tree
    from sklearn.externals.six import StringIO
    import pydot
    iris = load_iris()
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(iris.data, iris.target)
    
    
    #with open("iris.dot", 'w') as f:
    #    f = tree.export_graphviz(clf, out_file=f)
    filename = "iris.pdf"
    dot_data = StringIO()
    tree.export_graphviz(clf, out_file=dot_data)
    
    graph = pydot.graph_from_dot_data(dot_data.getvalue())
    print graph
    graph.write_pdf(filename)

    结果如下:

  • 相关阅读:
    css实现京东顶部导航条
    css盒布局-省份选择盘的实现
    css字体的属性
    css行高
    CSS定位(position)
    CSS-clear属性的作用
    2.b统计字符串长度
    rectangle类。java
    mysql 版本问题之sql-mode 导致 sql 语句报错
    根据一个表中同一字段的不同值进行统计!!!
  • 原文地址:https://www.cnblogs.com/zhusleep/p/5638704.html
Copyright © 2011-2022 走看看