本文整理汇总了Python中sklearn.tree.export_graphviz函数的典型用法代码示例。如果您正苦于以下问题:Python export_graphviz函数的具体用法?Python export_graphviz怎么用?Python export_graphviz使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了export_graphviz函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:Battleroid,项目名称:dectree,代码行数:60,代码来源:create_tree.py
示例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]))
开发者ID:kimmobrunfeldt,项目名称:machine-learning,代码行数:32,代码来源:main.py
示例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)
开发者ID:doubaoatthu,项目名称:UWRoutingSystem,代码行数:7,代码来源:server.py
示例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()
开发者ID:karlwichorek,项目名称:mlbscrape-python,代码行数:25,代码来源:train_regression.py
示例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')
开发者ID:lispc,项目名称:COMP8502,代码行数:7,代码来源:tree_test.py
示例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'
开发者ID:kargee2000,项目名称:ModellingTool_Python,代码行数:34,代码来源:FlaskTest1.py
示例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()))
开发者ID:BD2KGenomics,项目名称:brca-pipeline,代码行数:7,代码来源:machine_learning.py
示例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()
开发者ID:pramodbhn,项目名称:Programming-Data-Analytics,代码行数:28,代码来源:ClassificationTree.py
示例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'
开发者ID:kargee2000,项目名称:ModellingTool_Python,代码行数:31,代码来源:mainFlaskFile1.py
示例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')
开发者ID:antoine-tran,项目名称:pyksc,代码行数:30,代码来源:tree_infogain.py
示例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')
开发者ID:abernkopf,项目名称:Display-Ad-Network-Analysis,代码行数:7,代码来源:NvD_analysis1.py
示例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
开发者ID:ITh4cker,项目名称:trident,代码行数:28,代码来源:mlalgos.py
示例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")
开发者ID:albertoqa,项目名称:housingRegression,代码行数:60,代码来源:proyecto.py
示例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)
开发者ID:romanklinger,项目名称:irony-detection,代码行数:26,代码来源:machineLearning.py
示例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
开发者ID:tum-i22,项目名称:Oedipus,代码行数:30,代码来源:classification.py
示例16: drawDecTree
def drawDecTree(decTree, X, y, outdir, label=randint(100), featNames=None):
decTree.fit(X, y)
#return decTree.feature_importances_
dot_data = StringIO.StringIO()
tree.export_graphviz(decTree, out_file=dot_data, feature_names=featNames)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_png(outdir + "/" + str(label) + "_graph" + ".png")
开发者ID:churnprediction,项目名称:stackoverflow,代码行数:7,代码来源:plot_decision_tree.py
示例17: generate_plot
def generate_plot(clf):
print "\nGenerating plot..."
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("weather_forecast.pdf")
print "Plot generated!"
开发者ID:laurauzcategui,项目名称:machineLearning,代码行数:7,代码来源:decision_trees_classifier.py
示例18: make_tree_test
def make_tree_test():
from sklearn import tree
import StringIO
import pydot
from IPython.display import display, Image
x,y,dates,movies = load_data()
#x = add_missed_value_indicator(x)
test_x, train_x, test_y, train_y = create_test_train_set(x, y)
clf = tree.DecisionTreeClassifier(min_samples_split=3000)
fit = clf.fit(train_x,train_y)
dot_data = StringIO.StringIO()
tree.export_graphviz(fit,
feature_names=train_x.columns,
class_names=["1","2","3","4","5"],
out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_png("tree_toy.png")
img = Image(graph[0].create_png())
display(img)
return fit
开发者ID:yonatan-katz,项目名称:summer_course_2016,代码行数:35,代码来源:netflix.py
示例19: learn_dtree
def learn_dtree(data, csvfile):
clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=4)
k = data.groupby(['operator'])
# k = data.groupby(['operator'])
f = k['isCovered'].agg({'mean_kill': np.mean, 'number of mutants': len, 'number of killed':np.sum})
f.to_csv(csvfile)
fig = plt.figure()
# ax = Axes3D(fig)
# ax = fig.add_subplot(111, projection='3d')
plt.scatter(standardize (f['mean']),f['sum'])
plt.ylabel('mutant_size')
plt.xlabel('expected_kill (standatdize)')
# print f[f['len'] > 25000]
# ax.set_xlabel('mean')
# ax.set_ylabel('len')
# ax.set_zlabel('sum')
plt.show()
# plt.show()
# for m in k.groups:
# print m,len(k.groups[m]),
data['op'] = pd.factorize(data['operator'])[0]
data['m'] = pd.factorize(data['method'])[0]
HLdata['c'] = pd.factorize(data['class'])[0]
# plt.show()
plt.close()
x = data[['op', 'c', 'testId']].values
y = data['isCovered'].values
clf.fit(x,y)
dot_data = StringIO.StringIO()
tree.export_graphviz(clf, out_file=dot_data)
return dot_data.getvalue()
开发者ID:alipourm,项目名称:mut_json2db,代码行数:34,代码来源:dtree.py
示例20: main
def main():
if (len(sys.argv) < 2):
print("One Argument Required; Training Set")
return
X_train, Y_train = ParseTraining(sys.argv[1])
#X_test, Y_test = ParseTraining(sys.argv[2])
#X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X, Y, test_size=0.2, random_state=99)
#X_train, X_test, Y_train, Y_test = X, X, Y, Y
#clf = tree.DecisionTreeClassifier()
clf = tree.DecisionTreeClassifier(max_depth=6)
#clf = OneVsRestClassifier(SVC(kernel="linear", C=0.025))
#clf = RandomForestClassifier(max_depth=6, n_estimators=10, max_features=1)
#clf = SVC(kernel="linear", C=0.025)
#clf = AdaBoostClassifier()
#clf = SVC(gamma=2, C=1)
clf = clf.fit(X_train, Y_train)
#feature_names = ["partAvg", "recavg", "latency", "ReadRate"]
feature_names = ["partConf", "recAvg", "latency", "ReadRate", "homeconf"]
#feature_names = ["partAvg", "recAvg", "recVar", "ReadRate"]
#feature_names = ["partAvg", "recAvg", "recVar"]
#feature_names = ["recAvg", "recVar", "Read"]
#feature_names = ["partAvg", "recVar"]
##class_names = ["Partition", "OCC", "2PL"]
#class_names = ["OCC", "2PL"]
class_names = ["Partition", "No Partition"]
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=feature_names,
class_names=class_names,
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_png("partition.png")
开发者ID:totemtang,项目名称:ACC-Classifier,代码行数:35,代码来源:single-partition.py
注:本文中的sklearn.tree.export_graphviz函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论