本文整理汇总了Python中sklearn.tree方法的典型用法代码示例。如果您正苦于以下问题:Python sklearn.tree方法的具体用法?Python sklearn.tree怎么用?Python sklearn.tree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn
的用法示例。
在下文中一共展示了sklearn.tree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_tree_paths
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def _get_tree_paths(tree, node_id, depth=0):
"""
Returns all paths through the tree as list of node_ids
"""
if node_id == _tree.TREE_LEAF:
raise ValueError("Invalid node_id %s" % _tree.TREE_LEAF)
left_child = tree.children_left[node_id]
right_child = tree.children_right[node_id]
if left_child != _tree.TREE_LEAF:
left_paths = _get_tree_paths(tree, left_child, depth=depth + 1)
right_paths = _get_tree_paths(tree, right_child, depth=depth + 1)
for path in left_paths:
path.append(node_id)
for path in right_paths:
path.append(node_id)
paths = left_paths + right_paths
else:
paths = [[node_id]]
return paths
示例2: visualize_tree
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def visualize_tree(tree, feature_names, save_dir='./'):
"""Create tree png using graphviz.
Args
----
tree -- scikit-learn DecsisionTree.
feature_names -- list of feature names.
"""
with open(save_dir+'/'+"dt.dot", 'w') as f:
export_graphviz(tree, out_file=f,
feature_names=feature_names)
command = ["dot", "-Tpng", save_dir+"/dt.dot", "-o", save_dir+"/dt.png"]
try:
subprocess.check_call(command)
except:
exit("Could not run dot, ie graphviz, to "
"produce visualization")
开发者ID:mahendrakhened,项目名称:Automated-Cardiac-Segmentation-and-Disease-Diagnosis,代码行数:20,代码来源:stage_1_diagnosis.py
示例3: test_pickle_version_warning_is_not_raised_with_matching_version
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def test_pickle_version_warning_is_not_raised_with_matching_version():
iris = datasets.load_iris()
tree = DecisionTreeClassifier().fit(iris.data, iris.target)
tree_pickle = pickle.dumps(tree)
assert b"version" in tree_pickle
tree_restored = assert_no_warnings(pickle.loads, tree_pickle)
# test that we can predict with the restored decision tree classifier
score_of_original = tree.score(iris.data, iris.target)
score_of_restored = tree_restored.score(iris.data, iris.target)
assert_equal(score_of_original, score_of_restored)
示例4: test_pickle_version_warning_is_issued_upon_different_version
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def test_pickle_version_warning_is_issued_upon_different_version():
iris = datasets.load_iris()
tree = TreeBadVersion().fit(iris.data, iris.target)
tree_pickle_other = pickle.dumps(tree)
message = pickle_error_message.format(estimator="TreeBadVersion",
old_version="something",
current_version=sklearn.__version__)
assert_warns_message(UserWarning, message, pickle.loads, tree_pickle_other)
示例5: test_pickle_version_warning_is_issued_when_no_version_info_in_pickle
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def test_pickle_version_warning_is_issued_when_no_version_info_in_pickle():
iris = datasets.load_iris()
# TreeNoVersion has no getstate, like pre-0.18
tree = TreeNoVersion().fit(iris.data, iris.target)
tree_pickle_noversion = pickle.dumps(tree)
assert b"version" not in tree_pickle_noversion
message = pickle_error_message.format(estimator="TreeNoVersion",
old_version="pre-0.18",
current_version=sklearn.__version__)
# check we got the warning about using pre-0.18 pickle
assert_warns_message(UserWarning, message, pickle.loads,
tree_pickle_noversion)
示例6: test_pickle_version_no_warning_is_issued_with_non_sklearn_estimator
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def test_pickle_version_no_warning_is_issued_with_non_sklearn_estimator():
iris = datasets.load_iris()
tree = TreeNoVersion().fit(iris.data, iris.target)
tree_pickle_noversion = pickle.dumps(tree)
try:
module_backup = TreeNoVersion.__module__
TreeNoVersion.__module__ = "notsklearn"
assert_no_warnings(pickle.loads, tree_pickle_noversion)
finally:
TreeNoVersion.__module__ = module_backup
示例7: decisiontree_on_fold
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def decisiontree_on_fold(feature_sets, train, test, y, y_all, X, dim, dimsum, learn_options):
'''
DecisionTreeRegressor from scikitlearn.
'''
clf = tree.DecisionTreeRegressor()
clf.fit(X[train], y[train][:, 0])
y_pred = clf.predict(X[test])[:, None]
return y_pred, clf
示例8: encode
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def encode(cls, obj):
import sklearn.tree
assert type(obj) == sklearn.tree._tree.Tree
init_args = obj.__reduce__()[1]
state = obj.__getstate__()
return {
'__mlspl_type': [type(obj).__module__, type(obj).__name__],
'init_args': init_args,
'state': state
}
示例9: decode
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def decode(cls, obj):
import sklearn.tree
init_args = obj['init_args']
state = obj['state']
# Add max_depth for backwards compatibility with PSC 1.2
# Previous version did not set the max_depth in the state when calling __getstate__
# https://github.com/scikit-learn/scikit-learn/blob/51a765acfa4c5d1ec05fc4b406968ad233c75162/sklearn/tree/_tree.pyx#L615
# and has been added in sklearn 0.18 to be used in both __getstate__ and __setstate__
# https://github.com/scikit-learn/scikit-learn/blob/ef5cb84a805efbe4bb06516670a9b8c690992bd7/sklearn/tree/_tree.pyx#L649
# Older models will not have the max_depth in their stored state, such that a key error is raised.
# the max_depth is only used in the decision path method, which we don't currently use
# and is used to init an np array of zeros in version 0.18:
# https://github.com/scikit-learn/scikit-learn/blob/ef5cb84a805efbe4bb06516670a9b8c690992bd7/sklearn/tree/_tree.pyx#L926
# https://github.com/scikit-learn/scikit-learn/blob/ef5cb84a805efbe4bb06516670a9b8c690992bd7/sklearn/tree/_tree.pyx#L991
state['max_depth'] = state.get('max_depth', 0)
t = sklearn.tree._tree.Tree(*init_args)
t.__setstate__(state)
return t
示例10: dtree
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def dtree(frame, target, criterion = 'gini', depth = None, sample = 0.01, ratio = 0.15):
tree = DecisionTreeClassifier(
criterion = criterion,
min_samples_leaf = sample,
max_depth = depth,
)
tree.fit(frame.fillna(-1), target)
dot_string = tree_to_dot(tree, frame.columns.values, high_light = ratio)
dot_to_img(dot_string, file = target.name + '.png')
示例11: selection_parameters_for_classfier
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def selection_parameters_for_classfier(X,y):
from sklearn import grid_search
#paras={ 'n_neighbors':[1,10], 'weights':['uniform', 'distance'], 'algorithm':['auto', 'ball_tree','kd_tree', 'brute'], 'leaf_size':[20,50]}
#knn = KNeighborsClassifier()
#naive_bayes
#nbg = GaussianNB()
#nbm = MultinomialNB()
#nbb = BernoulliNB()
#decision tree
#paras={ 'criterion':['gini','entropy'], 'splitter':['random', 'best'], 'max_features':[None, 'auto','sqrt', 'log2'], 'min_samples_split':[1,10]}
#dtree = DecisionTreeClassifier()
#random forest
#rforest = RandomForestClassifier()
#paras={ 'n_estimators':[2,15], 'criterion':['gini','entropy'], 'max_features': ['auto','sqrt', 'log2'], 'min_samples_split':[1,10]}
#svm
svmm = svm.SVC()
paras={'kernel':['rbf','linear','poly']}
clt =grid_search.GridSearchCV(svmm, paras, cv=5)
clt.fit(X,y)
print (clt)
#print (clt.get_params())
print (clt.set_params())
print (clt.score(X,y))
#scores = cross_val_score(clt,X,y,cv=10)
#print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
#this is to get score using cross_validation
示例12: my_get_fp_fn_CV
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def my_get_fp_fn_CV(X_original,y):
#generate classfiers
knn = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', n_neighbors=5, p=2, weights='uniform')
#decision tree
dtree = DecisionTreeClassifier( criterion='gini', min_samples_leaf=4, min_samples_split=2, random_state=None, splitter='best')
#naive
#nbbern = BernoulliNB()
#random forest
rforest = RandomForestClassifier(bootstrap=True, criterion='gini', max_depth=None, max_features='auto', min_samples_leaf=1, min_samples_split=2, n_estimators=10, n_jobs=1, oob_score=False, random_state=3)
#svm
svmrbf= svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, kernel='rbf', max_iter=-1, probability=True, random_state=None,
shrinking=True, tol=0.001, verbose=False)
#reduce the size
#X = SelectKBest(f_classif, k=80).fit_transform(X_original,y)
skb = SelectKBest(f_classif, k=80).fit(X_original,y)
X = skb.fit_transform(X_original,y)
print ("KNN")
my_get_fp_fn_inter(knn,X,y)
print ("DTree")
my_get_fp_fn_inter(dtree,X,y)
print ("rforest")
my_get_fp_fn_inter(rforest,X,y)
#print ("naive bayes")
#my_get_fp_fn_inter(nbbern,X,y)
print ("SVMrbf")
my_get_fp_fn_inter(svmrbf,X,y)
示例13: train_and_test
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def train_and_test(X,y):
#KNN
knn = KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski', n_neighbors=5, p=2, weights='uniform')
#naive-bayees
nbbern = BernoulliNB()
#decision tree
dtree = DecisionTreeClassifier( criterion='gini', min_samples_leaf=4, min_samples_split=2, random_state=None, splitter='best')
#random forest
rforest = RandomForestClassifier(bootstrap=True, criterion='gini', max_depth=None, max_features='auto', min_samples_leaf=1, min_samples_split=2, n_estimators=10, n_jobs=1, oob_score=False, random_state=3)
#svm
svmrbf= svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, kernel='rbf', max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False)
get_scroe_using_cv(knn, X, y)
get_scroe_using_cv(nbbern, X, y)
get_scroe_using_cv(dtree, X, y)
get_scroe_using_cv(rforest, X, y)
get_scroe_using_cv(svmrbf, X, y)
print ("\n")
######################################################################
#this is to draw the Roc curve example by splitting the dataset
#just want a figure to make it more beautiful
示例14: get_fpr_tpr
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def get_fpr_tpr(clt, X, y):
random_state = np.random.RandomState(0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25 , random_state = 0)
#from sklearn import tree
#clt = tree.DecisionTreeClassifier( criterion='entropy', min_samples_leaf=2, min_samples_split=2, random_state=None, splitter='best')
clt = clt.fit(X_train,y_train)
#from sklearn.externals.six import StringIO
#with open("iris_plus.dot", 'w') as f:
# f = tree.export_graphviz(clt, out_file=f)
y_pred = clt.predict(X_test)
#accuracy score
_accuracy_score = accuracy_score(y_test, y_pred)
print ("Accuracy score {}".format(_accuracy_score))
#roc curve
probas_ = clt.predict_proba(X_test)
#print (probas_)
#draw_confusion_matrix(y_test,y_pred)
#print probas_
fpr, tpr, thresholds = roc_curve(y_test, probas_[:, 1])
#print (fpr, tpr,thresholds)
roc_auc = auc(fpr, tpr)
print ("Area under the ROC curve : %f" % roc_auc)
return fpr, tpr , roc_auc
# this is used to draw
示例15: __init__
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import tree [as 别名]
def __init__(self, ranking_size, logging_policy, target_policy, estimator_type):
Estimator.__init__(self, ranking_size, logging_policy, target_policy)
self.name = 'Direct_'+estimator_type
self.estimatorType = estimator_type
self.numFeatures=self.loggingPolicy.dataset.features[0].shape[1]
self.hyperParams={'alpha': (numpy.logspace(-2,1,num=4,base=10)).tolist()}
self.treeDepths={'max_depth': list(range(3,15,3))}
if self.estimatorType=='tree':
self.tree=None
else:
self.policyParams=None
#This member is set on-demand by estimateAll(...)
self.savedValues=None