8000 Pprett/gradient boosting by glouppe · Pull Request #6 · pprett/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

Pprett/gradient boosting #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 20, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cosmit
  • Loading branch information
glouppe committed Mar 19, 2012
commit ec9ed897b4331b7f3a317e66b69eb5f249e7bed7
34 changes: 21 additions & 13 deletions sklearn/tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ def recurse(tree, node_id):
return out_file





class Tree(object):
"""Struct-of-arrays representation of a binary decision tree.

Expand Down Expand Up @@ -267,18 +264,23 @@ def add_leaf(self, parent, is_left_child, value, error, n_samples):

def predict(self, X):
out = np.empty((X.shape[0], self.value.shape[1]), dtype=DTYPE)
_tree._predict_tree(X, self.children, self.feature, self.threshold,
self.value, out)

_tree._predict_tree(X,
self.children,
self.feature,
self.threshold,
self.value,
out)
return out

def compute_feature_importances(self, n_features, method="gini"):
"""Computes the importance of each feature (aka variable).

The following `method`s are supported:

* 'gini' : The difference of the initial error and the error of the
* "gini" : The difference of the initial error and the error of the
split times the number of samples that passed the node.
* 'squared' : The empirical improvement in squared error.
* "squared" : The empirical improvement in squared error.

Parameters
----------
Expand All @@ -289,24 +291,30 @@ def compute_feature_importances(self, n_features, method="gini"):
The method to estimate the importance of a feature. Either "gini"
or "mse".
"""
gini = lambda node: (self.n_samples[node] * \
(self.init_error[node] - self.best_error[node]))
squared = lambda node: (self.init_error[node] - \
self.best_error[node]) ** 2.0
if method == "gini":
method = lambda node: (self.n_samples[node] * \
(self.init_error[node] -
self.best_error[node]))
elif method == "mse":
method = lambda node: (self.init_error[node] - \
self.best_error[node]) ** 2.0
else:
raise ValueError(
'Invalid value for method. Allowed string '
'values are "gini", or "mse".')

method = gini if method == "gini" else squared
importances = np.zeros((n_features,), dtype=DTYPE)

for node in range(self.node_count):
if (self.children[node, 0]
== self.children[node, 1]
== Tree.LEAF):
continue

else:
importances[self.feature[node]] += method(node)

importances /= np.sum(importances)

return importances


Expand Down
0