-- coding: utf-8 --
“””
Spyder Editor
This is a temporary script file.
“”“
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
import os
import matplotlib.pylab as plt
from sklearn.cross_validation import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
import sklearn.metrics
“””
load dataset
“””
data = pd.read_csv(“E:\Coursera\Machine Learning for Data Analysis\PDF\tree_addhealth.csv”)
data_clean = data.dropna()
data_clean.dtypes
data_clean.describe()
“””
Modeling and Prediction
“”“
Split into training and testing sets
predictors = data_clean[[‘marever1’,’ALCEVR1’]]
targets = data_clean.TREG1
pred_train, pred_test, tar_train, tar_test = train_test_split(predictors, targets, test_size=.4)
pred_train.shape
pred_test.shape
tar_train.shape
tar_test.shape
Build model on training data
classifier=tree.DecisionTreeClassifier()
classifier=classifier.fit(pred_train,tar_train)
predictions=classifier.predict(pred_test)
“””
confusion matrix is the accuracy matrix
“””
sklearn.metrics.confusion_matrix(tar_test,predictions)
sklearn.metrics.accuracy_score(tar_test, predictions)
Displaying the decision tree
from sklearn import tree
from StringIO import StringIO
from io import StringIO
from StringIO import StringIO
from IPython.display import Image
out = StringIO()
tree.export_graphviz(classifier, out_file=out)
import pydotplus
graph=pydotplus.graph_from_dot_data(out.getvalue())
Image(graph.create_png())
graph.write_pdf(“d:\a.pdf”)