本文整理汇总了Python中sklearn.tree.DecisionTreeClassifier.apply方法的典型用法代码示例。如果您正苦于以下问题:Python DecisionTreeClassifier.apply方法的具体用法?Python DecisionTreeClassifier.apply怎么用?Python DecisionTreeClassifier.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.tree.DecisionTreeClassifier
的用法示例。
在下文中一共展示了DecisionTreeClassifier.apply方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_new_features
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import apply [as 别名]
def compute_new_features(X_train,y_train,X_test,y_test):
classifier = DecisionTreeClassifier(max_leaf_nodes=50)
classifier.fit(X_train,y_train)
idx_train = classifier.apply(X_train)
idx_train = idx_train.reshape([-1,1])
enc = OneHotEncoder()
enc.fit(idx_train)
new_features_train = enc.transform(idx_train).toarray()
idx_test = classifier.apply(X_test)
idx_test = idx_test.reshape([-1,1])
new_features_test = enc.transform(idx_test).toarray()
return ([np.hstack([X_train,new_features_train]), np.hstack([X_test,new_features_test])])
示例2: print
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import apply [as 别名]
print(
"%snode=%s test node: go to node %s if X[:, %s] <= %ss else to "
"node %s." % (node_depth[i] * "\t", i, children_left[i], feature[i], threshold[i], children_right[i])
)
print()
# First let's retrieve the decision path of each sample. The decision_path
# method allows to retrieve the node indicator functions. A non zero element of
# indicator matrix at the position (i, j) indicates that the sample i goes
# through the node j.
node_indicator = estimator.decision_path(X_test)
# Similarly, we can also have the leaves ids reached by each sample.
leave_id = estimator.apply(X_test)
# Now, it's possible to get the tests that were used to predict a sample or
# a group of samples. First, let's make it for the sample.
sample_id = 0
node_index = node_indicator.indices[node_indicator.indptr[sample_id] : node_indicator.indptr[sample_id + 1]]
print("Rules used to predict sample %s: " % sample_id)
for node_id in node_index:
if leave_id[sample_id] != node_id:
continue
if X_test[sample_id, feature[node_id]] <= threshold[node_id]:
threshold_sign = "<="
else:
示例3: execfile
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import apply [as 别名]
# -*- coding: utf-8 -*-
execfile('Original.py')
execfile('tfidf.py')
from sklearn.tree import DecisionTreeClassifier
s=DecisionTreeClassifier(max_depth=2, random_state=5)
s.fit(x,cuisine)
leaves=pd.Series(s.apply(x),name='leaves')
idk=pd.concat([cuisine,leaves],axis=1)
m=list(leaves.value_counts().index.values)
for y in m:
print y
print idk[leaves==y]['cuisine'].value_counts()
leaves.value_counts()
s2=DecisionTreeClassifier(max_depth=3, random_state=5)
s2.fit(x,cuisine)
leaves2=pd.Series(s2.apply(x),name='leaves')
idk=pd.concat([cuisine,leaves2],axis=1)
m=list(leaves2.value_counts().index.values)
for y in m:
print y
print idk[leaves2==y]['cuisine'].value_counts()
leaves2.value_counts()
s3=DecisionTreeClassifier(max_leaf_nodes=8, random_state=5,criterion='entropy')
s3.fit(x,cuisine)
示例4: hmwrapper
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import apply [as 别名]
#divide each column over sum of rows
df_new=df.div(df.sum(axis=0),axis='columns').fillna(0)
return df_new
def hmwrapper(cm,filename):
h=heatmap(cm).get_figure()
ax=h.add_subplot(111)
ax.set_xlabel('Predictions')
h.tight_layout()
h.set_size_inches(8,5.5)
h.savefig(filename,bbox_inches='tight',dpi=100)
#fitting/examining tree
s2=DecisionTreeClassifier(max_leaf_nodes=10, min_samples_leaf=500, random_state=5)
s2.fit(x,cuisine)
leaves2=pd.Series(s2.apply(x),name='leaves')
idk=pd.concat([cuisine,leaves2],axis=1)
m=list(leaves2.value_counts().index.values) #[3, 6, 10, 4, 7, 13, 11, 14]
for y in m:
print y
print idk[leaves2==y]['cuisine'].value_counts()
leaves2.value_counts()
#==============================================================================
# 3 33684: SGD SVM
# 6 2991: SGD SVM
# 10 1848: Naive Bayes/Logistic Regression
# 4 914: Naive Bayes/Logistic Regression
# 7 300: Logistic Regression
# 13 20: Naive Bayes
# 11 14: Naive Bayes
示例5: execfile
# 需要导入模块: from sklearn.tree import DecisionTreeClassifier [as 别名]
# 或者: from sklearn.tree.DecisionTreeClassifier import apply [as 别名]
@author: Samruddhi Somani
"""
execfile('Original.py')
execfile('tfidf.py')
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
import numpy as np
#fitting/examining tree
s2=DecisionTreeClassifier(max_depth=3, random_state=5)
s2.fit(x,cuisine)
leaves2=pd.Series(s2.apply(x),name='leaves')
idk=pd.concat([cuisine,leaves2],axis=1)
m=list(leaves2.value_counts().index.values) #[3, 6, 10, 4, 7, 13, 11, 14]
for y in m:
print y
print idk[leaves2==y]['cuisine'].value_counts()
leaves2.value_counts()
#==============================================================================
# 3 33684: SGD SVM
# 6 2991: SGD SVM
# 10 1848: Naive Bayes/Logistic Regression
# 4 914: Naive Bayes/Logistic Regression
# 7 300: Logistic Regression
# 13 20: Naive Bayes
# 11 14: Naive Bayes