本文整理汇总了Python中sklearn.ensemble.forest.RandomForestClassifier方法的典型用法代码示例。如果您正苦于以下问题:Python forest.RandomForestClassifier方法的具体用法?Python forest.RandomForestClassifier怎么用?Python forest.RandomForestClassifier使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.forest
的用法示例。
在下文中一共展示了forest.RandomForestClassifier方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def __init__(self, n_estimators=10, criterion='gini', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto', max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, bootstrap=True, oob_score=False, n_jobs=None, random_state=None, verbose=0, warm_start=False, class_weight='balanced'):
self._hyperparams = {
'n_estimators': n_estimators,
'criterion': criterion,
'max_depth': max_depth,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'min_weight_fraction_leaf': min_weight_fraction_leaf,
'max_features': max_features,
'max_leaf_nodes': max_leaf_nodes,
'min_impurity_decrease': min_impurity_decrease,
'min_impurity_split': min_impurity_split,
'bootstrap': bootstrap,
'oob_score': oob_score,
'n_jobs': n_jobs,
'random_state': random_state,
'verbose': verbose,
'warm_start': warm_start,
'class_weight': class_weight}
self._wrapped_model = Op(**self._hyperparams)
示例2: test_nlp_not_padded_invalid
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def test_nlp_not_padded_invalid(self):
num_words = 1024
(x_train, y_train), (_, _) = TestUtil.get_random_variable_length_dataset(max_value=num_words)
explained_model = RandomForestClassifier(n_estimators=64, max_depth=5, random_state=1)
counter = CountVectoriser(num_words)
tfidf_transformer = TfidfTransformer()
explained_model = Pipeline([('counts', counter),
('tfidf', tfidf_transformer),
('model', explained_model)])
explained_model.fit(x_train, y_train)
model_builder = RNNModelBuilder(embedding_size=num_words, with_embedding=True,
num_layers=2, num_units=32, activation="relu", p_dropout=0.2, verbose=0,
batch_size=32, learning_rate=0.001, num_epochs=2, early_stopping_patience=128)
masking_operation = WordDropMasking()
loss = binary_crossentropy
explainer = CXPlain(explained_model, model_builder, masking_operation, loss)
with self.assertRaises(ValueError):
explainer.fit(x_train, y_train)
示例3: multi_scorer_gridsearch
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def multi_scorer_gridsearch(estimator, x, y):
"""
Helper for sklearn, required to be able to score by more than one metric.
:param estimator:
:param x:
:param y:
:return:
"""
with __lock:
if isinstance(estimator, SVC):
if estimator.kernel == 'rbf':
params = {'C': estimator.C, 'gamma': estimator.gamma, 'kernel': estimator.kernel}
else:
params = {'C': estimator.C, 'kernel': estimator.kernel}
if isinstance(estimator, RandomForestClassifier):
log.info('RandomForestClassifier')
params = {'max_features': estimator.max_features, 'n_estimators': estimator.n_estimators,
'criterion': estimator.criterion}
try:
with open(settings.GRID_SEARCH_FOLDER_TMP + ''.join(__rand_id), 'rb') as f:
all_scores = pickle.load(f)
except FileNotFoundError:
all_scores = dict()
accuracy, precision, recall, roc, f1 = _get_scores(estimator, x, y)
scores = {'accuracy': accuracy, 'precision': precision, 'recall': recall, 'roc': roc, 'f1': f1}
params = frozenset(params.items())
if params not in all_scores.keys():
all_scores[params] = []
all_scores[params].append(scores)
with open(settings.GRID_SEARCH_FOLDER_TMP + ''.join(__rand_id), 'wb+') as f:
pickle.dump(all_scores, f)
return roc
示例4: rf_grid_search
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def rf_grid_search(data_set: DataSet, n_est_values=16, n_jobs=8):
num_of_estimators = numpy.random.random_integers(10, 1000, n_est_values)
max_feat = range(2, 44)
param_grid = dict(max_features=max_feat, n_estimators=num_of_estimators, criterion=['gini', 'entropy'])
return grid_search(RandomForestClassifier(), param_grid, data_set, n_jobs=n_jobs)
示例5: test_nlp_padded_valid
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def test_nlp_padded_valid(self):
num_words = 1024
(x_train, y_train), (x_test, y_test) = TestUtil.get_random_variable_length_dataset(max_value=num_words)
explained_model = RandomForestClassifier(n_estimators=64, max_depth=5, random_state=1)
counter = CountVectoriser(num_words)
tfidf_transformer = TfidfTransformer()
explained_model = Pipeline([('counts', counter),
('tfidf', tfidf_transformer),
('model', explained_model)])
explained_model.fit(x_train, y_train)
model_builder = RNNModelBuilder(embedding_size=num_words, with_embedding=True,
num_layers=2, num_units=32, activation="relu", p_dropout=0.2, verbose=0,
batch_size=32, learning_rate=0.001, num_epochs=2, early_stopping_patience=128)
masking_operation = WordDropMasking()
loss = binary_crossentropy
explainer = CXPlain(explained_model, model_builder, masking_operation, loss)
x_train = pad_sequences(x_train, padding="post", truncating="post", dtype=int)
x_test = pad_sequences(x_test, padding="post", truncating="post", dtype=int, maxlen=x_train.shape[1])
explainer.fit(x_train, y_train)
eval_score = explainer.score(x_test, y_test)
train_score = explainer.get_last_fit_score()
median = explainer.predict(x_test)
self.assertTrue(median.shape == x_test.shape)
示例6: test_imdb_padded_valid
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def test_imdb_padded_valid(self):
num_samples = 32
num_words = 1024
(x_train, y_train), (x_test, y_test) = TestUtil.get_imdb(word_dictionary_size=num_words,
num_subsamples=num_samples)
explained_model = RandomForestClassifier(n_estimators=64, max_depth=5, random_state=1)
counter = CountVectoriser(num_words)
tfidf_transformer = TfidfTransformer()
explained_model = Pipeline([('counts', counter),
('tfidf', tfidf_transformer),
('model', explained_model)])
explained_model.fit(x_train, y_train)
model_builder = RNNModelBuilder(embedding_size=num_words, with_embedding=True,
num_layers=2, num_units=32, activation="relu", p_dropout=0.2, verbose=0,
batch_size=32, learning_rate=0.001, num_epochs=2, early_stopping_patience=128)
masking_operation = WordDropMasking()
loss = binary_crossentropy
explainer = CXPlain(explained_model, model_builder, masking_operation, loss)
x_train = pad_sequences(x_train, padding="post", truncating="post", dtype=int)
x_test = pad_sequences(x_test, padding="post", truncating="post", dtype=int, maxlen=x_train.shape[1])
explainer.fit(x_train, y_train)
eval_score = explainer.score(x_test, y_test)
train_score = explainer.get_last_fit_score()
median = explainer.predict(x_test)
self.assertTrue(median.shape == x_test.shape)
示例7: test_nlp_erroneous_rnn_args_invalid
# 需要导入模块: from sklearn.ensemble import forest [as 别名]
# 或者: from sklearn.ensemble.forest import RandomForestClassifier [as 别名]
def test_nlp_erroneous_rnn_args_invalid(self):
num_words = 1024
(x_train, y_train), (x_test, y_test) = TestUtil.get_random_variable_length_dataset(max_value=num_words)
explained_model = RandomForestClassifier(n_estimators=64, max_depth=5, random_state=1)
counter = CountVectoriser(num_words)
tfidf_transformer = TfidfTransformer()
explained_model = Pipeline([('counts', counter),
('tfidf', tfidf_transformer),
('model', explained_model)])
explained_model.fit(x_train, y_train)
with self.assertRaises(ValueError):
_ = RNNModelBuilder(with_embedding=True, verbose=0) # Must also specify the embedding_size argument.
model_builder = RNNModelBuilder(embedding_size=num_words, with_embedding=True, verbose=0)
input_layer = Input(shape=(10, 2))
with self.assertRaises(ValueError):
model_builder.build(input_layer)
input_layer = Input(shape=(10, 3))
with self.assertRaises(ValueError):
model_builder.build(input_layer)