-
-
Notifications
You must be signed in to change notification settings - Fork 25.2k
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
[MRG] Sparse input support for decision tree and forest #3173
Merged
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c03c01a
ENH Bring sparse input support to tree-based methods
arjoly 0bc8a98
FIX+ENH add min_weight_fraction_split support for sparse splitter
arjoly 7cb9a5c
Re-organize code dense splitter then sparse splitter
arjoly 0e86dfd
Simplify call to extract_nnz making it a method
arjoly 9dd87ad
ENH while -> for loop
arjoly 62893e3
ENH reduce number of parameters
arjoly 70226d0
FIX min_weight_fraction_split with random splitter
arjoly 6cd9333
FIX min_weight_leaf in best sparse splitter
arjoly 306924b
ENH remove spurious code
arjoly cb9b741
cosmit
arjoly c6af5c6
ENH adaboost should accept c and fortran array
arjoly cb11511
COSMIT simplify function call
arjoly 1c26cec
ENH expand ternary operator
arjoly 3047cd6
Revert previous version
arjoly 38183c8
ENH move utils near its use
arjoly 06701fc
ENH add a benchmark script for sparse input data
arjoly 9f3f5bb
Extract non zero value extraction constant
arjoly 24281b1
Lower number of trees
arjoly c31d565
wip benchmark
arjoly bf98916
Temporarily allows to set algorithm switching through an environment …
arjoly 2838a14
Benchmark: Add more estimators + uncomment text
arjoly 8b3b071
FIX duplicate type coercision + DOC fix inversion between csc and csr
arjoly 4f423d6
Remove constant print
arjoly ab57964
COSMIT add Base prefix to DenseSplitter and DenseSplitter
arjoly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from __future__ import print_function, division | ||
from time import time | ||
import argparse | ||
import numpy as np | ||
|
||
from sklearn.dummy import DummyClassifier | ||
|
||
from sklearn.datasets import fetch_20newsgroups_vectorized | ||
from sklearn.metrics import accuracy_score | ||
from sklearn.utils.validation import check_array | ||
|
||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.ensemble import ExtraTreesClassifier | ||
from sklearn.ensemble import AdaBoostClassifier | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.naive_bayes import MultinomialNB | ||
|
||
ESTIMATORS = { | ||
"dummy": DummyClassifier(), | ||
"random_forest": RandomForestClassifier(n_estimators=100, | ||
max_features="sqrt", | ||
min_samples_split=10), | ||
"extra_trees": ExtraTreesClassifier(n_estimators=100, | ||
max_features="sqrt", | ||
min_samples_split=10), | ||
"logistic_regression": LogisticRegression(), | ||
"naive_bayes": MultinomialNB(), | ||
"adaboost": AdaBoostClassifier(n_estimators=10), | ||
} | ||
|
||
|
||
############################################################################### | ||
# Data | ||
|
||
if __name__ == "__main__": | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-e', '--estimators', nargs="+", required=True, | ||
choices=ESTIMATORS) | ||
args = vars(parser.parse_args()) | ||
|
||
data_train = fetch_20newsgroups_vectorized(subset="train") | ||
data_test = fetch_20newsgroups_vectorized(subset="test") | ||
X_train = check_array(data_train.data, dtype=np.float32, | ||
accept_sparse="csc") | ||
X_test = check_array(data_test.data, dtype=np.float32, accept_sparse="csr") | ||
y_train = data_train.target | ||
y_test = data_test.target | ||
|
||
print("20 newsgroups") | ||
print("=============") | ||
print("X_train.shape = {0}".format(X_train.shape)) | ||
print("X_train.format = {0}".format(X_train.format)) | ||
print("X_train.dtype = {0}".format(X_train.dtype)) | ||
print("X_train density = {0}" | ||
"".format(X_train.nnz / np.product(X_train.shape))) | ||
print("y_train {0}".format(y_train.shape)) | ||
print("X_test {0}".format(X_test.shape)) | ||
print("X_test.format = {0}".format(X_test.format)) | ||
print("X_test.dtype = {0}".format(X_test.dtype)) | ||
print("y_test {0}".format(y_test.shape)) | ||
print() | ||
|
||
print("Classifier Training") | ||
print("===================") | ||
accuracy, train_time, test_time = {}, {}, {} | ||
for name in sorted(args["estimators"]): | ||
clf = ESTIMATORS[name] | ||
try: | ||
clf.set_params(random_state=0) | ||
except (TypeError, ValueError): | ||
pass | ||
|
||
print("Training %s ... " % name, end="") | ||
t0 = time() | ||
clf.fit(X_train, y_train) | ||
train_time[name] = time() - t0 | ||
t0 = time() | ||
y_pred = clf.predict(X_test) | ||
test_time[name] = time() - t0 | ||
accuracy[name] = accuracy_score(y_test, y_pred) | ||
print("done") | ||
|
||
print() | ||
print("Classification performance:") | ||
print("===========================") | ||
print() | ||
print("%s %s %s %s" % ("Classifier ", "train-time", "test-time", | ||
"Accuracy")) | ||
print("-" * 44) | ||
for name in sorted(accuracy, key=accuracy.get): | ||
print("%s %s %s %s" % (name.ljust(16), | ||
("%.4fs" % train_time[name]).center(10), | ||
("%.4fs" % test_time[name]).center(10), | ||
("%.4f" % accuracy[name]).center(10))) | ||
|
||
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a linear model such as
LogisticRegression
andMultinomialNB
as common baselines for text classification here.