机器学习爬坑之路 ---- 如何让训练的决策面以图像的形式具体展现

-------------------------  文件 studentMain.py ---------------  start --------

#!/usr/bin/python



""" lecture and example code for decision tree unit """


import sys
from class_vis import prettyPicture, output_image
from prep_terrain_data import makeTerrainData


import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
from classifyDT import classify


features_train, labels_train, features_test, labels_test = makeTerrainData()


### the classify() function in classifyDT is where the magic
### happens--fill in this function in the file 'classifyDT.py'!
clf = classify(features_train, labels_train)




#### grader code, do not modify below this line


prettyPicture(clf, features_test, labels_test)

output_image("test.png", "png", open("test.png", "rb").read())

-------------------------  文件 studentMain.py ---------------  end --------

-------------------------  文件 classifyDT.py ---------------  start --------

from sklearn.tree import DecisionTreeClassifier


def classify(features_train, labels_train):
    
    ### your code goes here--should return a trained decision tree classifer


    clf = DecisionTreeClassifier()
    clf.fit(features_train,labels_train)
    

    return clf

-------------------------  文件 classifyDT.py ---------------  end --------

-------------------------  文件 class_vis.py ---------------  start --------


#!/usr/bin/python

import numpy as np
import matplotlib.pyplot as plt
import pylab as pl


def prettyPicture(clf, X_test, y_test):
    x_min = 0.0;
    x_max = 1.0
    y_min = 0.0;
    y_max = 1.0

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, m_max]x[y_min, y_max].
    h = .01  # step size in the mesh
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())

    plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)

    # Plot also the test points
    grade_sig = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii] == 0]
    bumpy_sig = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii] == 0]
    grade_bkg = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii] == 1]
    bumpy_bkg = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii] == 1]

    plt.scatter(grade_sig, bumpy_sig, color="b", label="fast")
    plt.scatter(grade_bkg, bumpy_bkg, color="r", label="slow")
    plt.legend()
    plt.xlabel("bumpiness")
    plt.ylabel("grade")

    plt.savefig("test.png")


import base64
import json
import subprocess


def output_image(name, format, bytes):
    image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
    image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
    data = {}
    data['name'] = name
    data['format'] = format
    data['bytes'] = base64.encodestring(bytes)
    print image_start + json.dumps(data) + image_end
-------------------------  文件 class_vis.py ---------------  end --------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值