本文整理汇总了Python中sklearn.tree.export_graphviz函数的典型用法代码示例。如果您正苦于以下问题:Python export_graphviz函数的具体用法?Python export_graphviz怎么用?Python export_graphviz使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了export_graphviz函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_tree
def create_tree(args):
# load
df = pd.read_csv(args['<input>'])
class_attr = args['<class>']
# check
if class_attr not in df.columns:
print('Class attribute "{}" not in dataset!'.format(class_attr))
sys.exit(1)
# flags & options
verbose = False
if args['--verbose']:
verbose = True
exceptions = None
output = None
if args['--output']:
output = args['--output']
if args['--except']:
exceptions = args['--except'].split(',')
samples = int(args['--samples'])
# get numeric columns and drop class and exceptions (our features)
features = df._get_numeric_data().columns.difference([class_attr])
if exceptions:
if verbose:
print('Removing the following: {}'.format(', '.join(exceptions)))
features = features.difference(exceptions)
# verbose detail
if verbose:
print('Using the following features: {}'.format(', '.join(features)))
# create tree
dt = DecisionTreeClassifier(
min_samples_split=samples,
criterion='entropy',
splitter='best',
random_state=99)
y = df[class_attr]
X = df[features]
dt.fit(X, y)
# export graph of tree if graph output specified
if output:
import subprocess
if verbose:
print('Saving as {0}.dot and {0}.png'.format(output))
# save dot
with open(output + '.dot', 'w') as f:
export_graphviz(dt, out_file=f, feature_names=features)
command = 'dot -Tpng {0}.dot -o {0}.png'.format(output).split()
try:
subprocess.check_call(command)
except:
print('Problem creating graphviz image, is graphviz installed?')
sys.exit(1)
示例2: main
def main():
data = run_game()
clf = DecisionTreeClassifier(criterion='entropy')
game_data = [[i[0], i[1]] for i in data]
profits = [i[2] for i in data]
clf.fit(game_data, profits)
with open('tree.dot', 'w') as dotfile:
export_graphviz(
clf,
dotfile,
feature_names=['coin', 'bet']
)
predictions_lose1 = [clf.predict([0, 0]) for x in xrange(100)]
predictions_lose2 = [clf.predict([0, 1]) for x in xrange(100)]
predictions_win = [clf.predict([1, 1]) for x in xrange(100)]
print 'All these profit predictions should be zero:'
print predictions_lose1
print 'Accuracy was', calculate_accuracy(predictions_lose1, np.array([0]))
print 'All these profit predictions should be zero:'
print predictions_lose2
print 'Accuracy was', calculate_accuracy(predictions_lose2, np.array([0]))
print 'All these profit predictions should be two:'
print predictions_win
print 'Accuracy was', calculate_accuracy(predictions_win, np.array([2]))
示例3: drawDecisionTree
def drawDecisionTree(dt, filename, featureNames, classNames):
dot_data = StringIO()
print featureNames
print classNames
tree.export_graphviz(dt, out_file=dot_data, feature_names=featureNames, class_names=classNames, rounded=True, special_characters=True, filled=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_png(filename)
示例4: train_network
def train_network(self):
""" Pure virtual method for training the network
"""
db_query = self._database_session.query(PregameHitterGameEntry)
mlb_training_data, mlb_evaluation_data = self.get_train_eval_data(db_query, 0.8)
X_train, Y_train = self.get_stochastic_batch(mlb_training_data, self.SIZE_TRAINING_BATCH)
self._decision_tree.fit(X_train, Y_train)
dot_data = StringIO()
tree.export_graphviz(self._decision_tree, out_file=dot_data,
feature_names=PregameHitterGameEntry.get_input_vector_labels())
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("hitter_tree.pdf")
x_test_actual = list()
y_test_actual = list()
for data in mlb_evaluation_data:
try:
postgame_entry = self._database_session.query(PostgameHitterGameEntry).filter(PostgameHitterGameEntry.rotowire_id == data.rotowire_id,
PostgameHitterGameEntry.game_date == data.game_date).one()
y_test_actual.append([postgame_entry.actual_draftkings_points])
x_test_actual.append(data.to_input_vector())
except NoResultFound:
print "Ignoring hitter %s since his postgame stats were not found." % data.rotowire_id
continue
self._database_session.close()
示例5: draw_tree
def draw_tree(clf):
import pydot
import StringIO
output = StringIO.StringIO()
tree.export_graphviz(clf, out_file=output)
graph = pydot.graph_from_dot_data(output.getvalue())
graph.write_pdf('tree.pdf')
示例6: tree2
def tree2():
global final_html
global df,origin_df
chi_key = list()
firstkey = ""
init_style_string = """<p style="position: absolute; font-size: 12px; top: <top>px; width: <width>px; height: <height>px; left:<left>px; text-align: center;">tree_text_here</p>"""
if request.method == 'POST':
Listkey1 = list(MultiDict(request.form).values())
Listkey2 = MultiDict(request.form)
DV_tree = Listkey2.get('DV')
df1 = df
for key1 in Listkey1:
if(key1 <> "Build Tree" and key1 <> DV_tree):
chi_key.append(key1)
df1 = df.loc[:,chi_key]
df2 = df1.values
temp_count = 0
Y = df[DV_tree]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(df2,Y.values)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
k = dot_data.getvalue()
k1 = k.split(";")
left_px = 600
width_px = 150
top_px = 50
height_px = 309
s = build_tree_html(k,init_style_string,left_px,width_px,top_px,height_px)
temp_df = df[0:15]
t = """</div><div style="float:right;"><br> Decision Tree result <br>"""
final_html = template.s1 + t + k + "</div><br><br><br>" + temp_df.to_html()
return final_html
return 'helloo'
示例7: visualize_tree
def visualize_tree(dtree):
dot_data = StringIO()
tree.export_graphviz(dtree, out_file=dot_data,
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
display(Image(graph.create_png()))
示例8: fit_decision_tree
def fit_decision_tree(train_X, train_y, test_X, test_y):
# print classification reports
# print accuracy
# The format should be
"""
Classification Report:
precision recall f1-score support
0.0 0.80 0.89 0.85 4932
1.0 0.75 0.60 0.67 2676
avg / total 0.78 0.79 0.78 7608
Accuracy: 0.788512092534"""
dtc = tree.DecisionTreeClassifier()
dtc = dtc.fit(train_X,train_y.flat)
pred_y = dtc.predict(test_X)
print classification_report(test_y, pred_y)
print accuracy_score(test_y,pred_y)
# create the graph - Here you just need to create the dot file. Please uncomment my code below
from sklearn.externals.six import StringIO
f = open('tre.dot','w')
tree.export_graphviz(dtc, out_file=f) # please change your_tree_model_fit with the variable you used above
f.close()
示例9: tree3
def tree3():
global final_html
global df,df_train,df_test,test_train_created,origin_df
chi_key = list()
init_style_string = template.style_string
if request.method == 'POST':
Listkey1 = list(MultiDict(request.form).values())
Listkey2 = MultiDict(request.form)
DV_tree = Listkey2.get('DV')
df1 = df
for key1 in Listkey1:
if(key1 <> "Build Tree" and key1 <> DV_tree):
chi_key.append(key1)
df1 = df.loc[:,chi_key]
df2 = df1.values
Y = df[DV_tree]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(df2,Y.values)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
k = dot_data.getvalue()
left_px = 600
width_px = 150
top_px = 50
height_px = 309
s = build_tree_html(k,init_style_string,left_px,width_px,top_px,height_px)
temp_df = df[0:15]
t = """</div><div style="width:600px; height:700px; position: absolute; top: 20px; left:500px;"><br> Decision Tree result <br>"""
final_html = template.s1 + t + k + "<br><br></div>" + temp_df.to_html()
return final_html
return 'helloo'
示例10: main
def main(features_fpath, classes_fpath):
with open(features_fpath) as features_file:
for line in features_file:
if '#' in line:
spl = line.split()
names = spl[1:]
X = scale(np.genfromtxt(features_fpath)[:,1:].copy())
y = np.loadtxt(classes_fpath)
forest = ExtraTreesClassifier(max_depth=4,
criterion="entropy",
compute_importances=True)
scores = cross_val_score(forest, X, y, score_func=f1_score, cv=5)
print(scores)
forest.fit(X, y)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in xrange(len(importances[indices])):
print("%d. feature %s (%f)" % (f + 1, names[indices[f]],
importances[indices[f]]))
export_graphviz(forest, 'bala.dot')
示例11: create_pdf
def create_pdf(clf):
print 'Drawing tree...'
"""Save dec tree graph as pdf."""
dot_data = StringIO.StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf('NvD5.pdf')
示例12: dt_graph
def dt_graph(treeest, cv, scores, features, labels, featnames, outfile):
''' Retrains the tree estimator using the fold with the best results
from the cross-validation process. Prints out a graph pdf file of
that estimator.'''
# Hacky way to get the training data for the best fold
bestfold = np.argmax(scores)
cnt = 0
for train, _ in cv:
# Only do stuff when you've got the training indices for the best fold
if(cnt == bestfold):
# Fit
treeest.fit(features[train], labels[train])
# Get the dot file
dot_data = StringIO()
tree.export_graphviz(treeest, out_file=dot_data, \
feature_names=featnames)
# Convert the dot file to a graph
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf(outfile)
return
else:
cnt += 1
print("You should never see this text from dt_graph!")
return
示例13: arbolesRegresion
def arbolesRegresion(caract):
clf = DecisionTreeRegressor(min_samples_leaf=10, min_samples_split=15, max_depth=13, compute_importances=True)
importancias = [0,0,0,0,0,0,0,0,0,0,0,0,0]
mae=mse=r2=0
kf = KFold(len(boston_Y), n_folds=10, indices=True)
for train, test in kf:
trainX, testX, trainY, testY=boston_X[train], boston_X[test], boston_Y[train], boston_Y[test]
nCar=len(caract)
train=np.zeros((len(trainX), nCar))
test=np.zeros((len(testX), nCar))
trainYNuevo=trainY
for i in range(nCar):
for j in range(len(trainX)):
train[j][i]=trainX[j][caract[i]]
for k in range(len(testX)):
test[k][i]=testX[k][caract[i]]
trainYNuevo=np.reshape(trainYNuevo, (len(trainY), -1))
clf.fit(train, trainYNuevo)
prediccion=clf.predict(test)
# clf.fit(trainX, trainY)
# prediccion=clf.predict(testX)
mae+=metrics.mean_absolute_error(testY, prediccion)
mse+=metrics.mean_squared_error(testY, prediccion)
r2+=metrics.r2_score(testY, prediccion)
feature_importance = clf.feature_importances_
feature_importance = 100.0 * (feature_importance / feature_importance.max())
for i in range(13):
importancias[i] = importancias[i] + feature_importance[i]
print 'Error abs: ', mae/len(kf), 'Error cuadratico: ', mse/len(kf), 'R cuadrado: ', r2/len(kf)
for i in range(13):
importancias[i] = importancias[i]/10
sorted_idx = np.argsort(importancias)
pos = np.arange(sorted_idx.shape[0]) + .5
importancias = np.reshape(importancias, (len(importancias), -1))
boston = datasets.load_boston()
pl.barh(pos, importancias[sorted_idx], align='center')
pl.yticks(pos, boston.feature_names[sorted_idx])
pl.xlabel('Importancia relativa')
pl.show()
import StringIO, pydot
dot_data = StringIO.StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("bostonTree.pdf")
示例14: applyDecisionTree
def applyDecisionTree(trainData, trainTargets, testData, testTargets, featureNames):
"""Train and classify using a Decision Tree and prints the decision Tree."""
decisionTree = DecisionTreeClassifier()
model = decisionTree.fit(trainData, trainTargets)
# Create graph description of the Decision Tree
dot_data = StringIO()
#export_graphviz(model, out_file=dot_data, max_depth=5)
print("Feature names:", featureNames)
export_graphviz(model, out_file=dot_data, feature_names=featureNames,
max_depth=5)
export_graphviz(model, out_file="DecisionTree.dot", feature_names=featureNames,
max_depth=5)
#with open("DecisionTree.dot", 'r') as dotFile:
# dotFile.write(exportFile)
# Create PDF from dot
graph = pydot.graph_from_dot_data(dot_data.getvalue())
#path = "/Users/konstantin/Documents/University/Bachelorthesis/paper/src/DecisionTree.dot"
#graph = pydot.graph_from_dot_file(path)
#graph.write_pdf("DecisionTree.pdf")
classification = [model.predict(d)[0] for d in testData]
print("\nUsing a Decision Tree:")
showPerformance(testTargets, classification)
示例15: classifyTree
def classifyTree(Xtr, ytr, Xte, yte, splitCriterion="gini", maxDepth=0, visualizeTree=False):
""" Classifies data using CART """
try:
accuracyRate, probabilities, timing = 0.0, [], 0.0
# Perform classification
cartClassifier = tree.DecisionTreeClassifier(criterion=splitCriterion, max_depth=maxDepth)
startTime = time.time()
prettyPrint("Training a CART tree for classification using \"%s\" and maximum depth of %s" % (splitCriterion, maxDepth), "debug")
cartClassifier.fit(numpy.array(Xtr), numpy.array(ytr))
prettyPrint("Submitting the test samples", "debug")
predicted = cartClassifier.predict(Xte)
endTime = time.time()
# Compare the predicted and ground truth and append result to list
accuracyRate = round(metrics.accuracy_score(predicted, yte), 2)
# Also append the probability estimates
probs = cartClassifier.predict_proba(Xte)
probabilities.append(probs)
timing = endTime-startTime # Keep track of performance
if visualizeTree:
# Visualize the tree
dot_data = StringIO()
tree.export_graphviz(cartClassifier, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
prettyPrint("Saving learned CART to \"tritonTree_%s.pdf\"" % getTimestamp(), "debug")
graph.write_pdf("tree_%s.pdf" % getTimestamp())
except Exception as e:
prettyPrint("Error encountered in \"classifyTree\": %s" % e, "error")
return accuracyRate, timing, probabilities, predicted