當前位置: 首頁>>代碼示例>>Python>>正文


Python six.StringIO方法代碼示例

本文整理匯總了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) 
開發者ID:columbia,項目名稱:fairtest,代碼行數:22,代碼來源:guided_tree.py

示例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 
開發者ID:lhm30,項目名稱:PIDGINv2,代碼行數:27,代碼來源:predict_enriched_decision_tree.py

示例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()) 
開發者ID:apachecn,項目名稱:AiLearning,代碼行數:24,代碼來源:DTSklearn.py

示例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()) 
開發者ID:apachecn,項目名稱:AiLearning,代碼行數:24,代碼來源:DTSklearn.py

示例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) 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:21,代碼來源:test_online_lda.py

示例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 
開發者ID:yassineAlouini,項目名稱:kaggle-tools,代碼行數:20,代碼來源:visualize_tree.py

示例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 
開發者ID:lhm30,項目名稱:PIDGINv2,代碼行數:28,代碼來源:predict_enriched_two_libraries_decision_tree.py

示例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) 
開發者ID:biocommons,項目名稱:uta,代碼行數:7,代碼來源:learn.py

示例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") 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:34,代碼來源:test_export.py

示例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()) 
開發者ID:alvarobartt,項目名稱:twitter-stock-recommendation,代碼行數:15,代碼來源:test_export.py


注:本文中的sklearn.externals.six.StringIO方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。