本文整理汇总了Python中sklearn.externals.six.StringIO方法的典型用法代码示例。如果您正苦于以下问题:Python six.StringIO方法的具体用法?Python six.StringIO怎么用?Python six.StringIO使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.externals.six
的用法示例。
在下文中一共展示了six.StringIO方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_tree
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def print_tree(tree, outfile, encoders):
"""
Print a tree to a file
Parameters
----------
tree :
the tree structure
outfile :
the output file
encoders :
the encoders used to encode categorical features
"""
import pydot
dot_data = StringIO()
export_graphviz(tree, encoders, filename=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf(outfile)
示例2: createTree
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def createTree(matrix,label):
kmeans = KMeans(n_clusters=moa_clusters, random_state=0).fit(matrix)
vector = map(int,kmeans.labels_)
pc_10 = int(len(querymatrix1)*0.01)
clf = tree.DecisionTreeClassifier(min_samples_split=min_sampsplit,min_samples_leaf=min_leafsplit,max_depth=max_d)
clf.fit(matrix,vector)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=label,
class_names=map(str,list(set(sorted(kmeans.labels_)))),
filled=True, rounded=True,
special_characters=True,
proportion=False,
impurity=True)
out_tree = dot_data.getvalue()
out_tree = out_tree.replace('True','Inactive').replace('False','Active').replace(' ≤ 0.5', '').replace('class', 'Predicted MoA')
graph = pydot.graph_from_dot_data(str(out_tree))
try:
graph.write_jpg(output_name_tree)
except AttributeError:
graph = pydot.graph_from_dot_data(str(out_tree))[0]
graph.write_jpg(output_name_tree)
return
#initializer for the pool
示例3: show_pdf
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def show_pdf(clf):
'''
可视化输出
把决策树结构写入文件: http://sklearn.lzjqsdd.com/modules/tree.html
Mac报错: pydotplus.graphviz.InvocationException: GraphViz's executables not found
解决方案: sudo brew install graphviz
参考写入: http://www.jianshu.com/p/59b510bafb4d
'''
# with open("testResult/tree.dot", 'w') as f:
# from sklearn.externals.six import StringIO
# tree.export_graphviz(clf, out_file=f)
import pydotplus
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("../../../output/3.DecisionTree/tree.pdf")
# from IPython.display import Image
# Image(graph.create_png())
示例4: show_pdf
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def show_pdf(clf):
'''
可视化输出
把决策树结构写入文件: http://sklearn.lzjqsdd.com/modules/tree.html
Mac报错: pydotplus.graphviz.InvocationException: GraphViz's executables not found
解决方案: sudo brew install graphviz
参考写入: http://www.jianshu.com/p/59b510bafb4d
'''
# with open("testResult/tree.dot", 'w') as f:
# from sklearn.externals.six import StringIO
# tree.export_graphviz(clf, out_file=f)
import pydotplus
from sklearn.externals.six import StringIO
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("output/3.DecisionTree/tree.pdf")
# from IPython.display import Image
# Image(graph.create_png())
示例5: check_verbosity
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def check_verbosity(verbose, evaluate_every, expected_lines,
expected_perplexities):
n_components, X = _build_sparse_mtx()
lda = LatentDirichletAllocation(n_components=n_components, max_iter=3,
learning_method='batch',
verbose=verbose,
evaluate_every=evaluate_every,
random_state=0)
out = StringIO()
old_out, sys.stdout = sys.stdout, out
try:
lda.fit(X)
finally:
sys.stdout = old_out
n_lines = out.getvalue().count('\n')
n_perplexity = out.getvalue().count('perplexity')
assert_equal(expected_lines, n_lines)
assert_equal(expected_perplexities, n_perplexity)
示例6: visualize_tree
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def visualize_tree(clf, feature_names, class_names, output_file,
method='pdf'):
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True,
impurity=False)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
if method == 'pdf':
graph.write_pdf(output_file + ".pdf")
elif method == 'inline':
Image(graph.create_png())
return graph
# An example using the iris dataset
示例7: createTree
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def createTree(matrix,label):
vector = [1] * len(querymatrix1) + [0] * len(querymatrix2)
ratio = float(len(vector)-sum(vector))/float(sum(vector))
sw = np.array([ratio if i == 1 else 1 for i in vector])
pc_10 = int(len(querymatrix1)*0.01)
clf = tree.DecisionTreeClassifier(min_samples_split=min_sampsplit,min_samples_leaf=min_leafsplit,max_depth=max_d)
clf.fit(matrix,vector)
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=label,
class_names=['File2','File1'],
filled=True, rounded=True,
special_characters=True,
proportion=False,
impurity=True)
out_tree = dot_data.getvalue()
out_tree = out_tree.replace('True','Inactive').replace('False','Active').replace(' ≤ 0.5', '')
graph = pydot.graph_from_dot_data(str(out_tree))
try:
graph.write_jpg(output_name_tree)
except AttributeError:
graph = pydot.graph_from_dot_data(str(out_tree))[0]
graph.write_jpg(output_name_tree)
return
#initializer for the pool
示例8: write_pdf
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def write_pdf(clf, fn):
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf(fn)
示例9: test_graphviz_errors
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def test_graphviz_errors():
# Check for errors of export_graphviz
clf = DecisionTreeClassifier(max_depth=3, min_samples_split=2)
# Check not-fitted decision tree error
out = StringIO()
assert_raises(NotFittedError, export_graphviz, clf, out)
clf.fit(X, y)
# Check if it errors when length of feature_names
# mismatches with number of features
message = ("Length of feature_names, "
"1 does not match number of features, 2")
assert_raise_message(ValueError, message, export_graphviz, clf, None,
feature_names=["a"])
message = ("Length of feature_names, "
"3 does not match number of features, 2")
assert_raise_message(ValueError, message, export_graphviz, clf, None,
feature_names=["a", "b", "c"])
# Check class_names error
out = StringIO()
assert_raises(IndexError, export_graphviz, clf, out, class_names=[])
# Check precision error
out = StringIO()
assert_raises_regex(ValueError, "should be greater or equal",
export_graphviz, clf, out, precision=-1)
assert_raises_regex(ValueError, "should be an integer",
export_graphviz, clf, out, precision="1")
示例10: test_friedman_mse_in_graphviz
# 需要导入模块: from sklearn.externals import six [as 别名]
# 或者: from sklearn.externals.six import StringIO [as 别名]
def test_friedman_mse_in_graphviz():
clf = DecisionTreeRegressor(criterion="friedman_mse", random_state=0)
clf.fit(X, y)
dot_data = StringIO()
export_graphviz(clf, out_file=dot_data)
clf = GradientBoostingClassifier(n_estimators=2, random_state=0)
clf.fit(X, y)
for estimator in clf.estimators_:
export_graphviz(estimator[0], out_file=dot_data)
for finding in finditer("\[.*?samples.*?\]", dot_data.getvalue()):
assert_in("friedman_mse", finding.group())