本文整理汇总了Python中sklearn.ensemble.ExtraTreesClassifier.variable_feature_importances方法的典型用法代码示例。如果您正苦于以下问题:Python ExtraTreesClassifier.variable_feature_importances方法的具体用法?Python ExtraTreesClassifier.variable_feature_importances怎么用?Python ExtraTreesClassifier.variable_feature_importances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.ExtraTreesClassifier
的用法示例。
在下文中一共展示了ExtraTreesClassifier.variable_feature_importances方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.ensemble import ExtraTreesClassifier [as 别名]
# 或者: from sklearn.ensemble.ExtraTreesClassifier import variable_feature_importances [as 别名]
def main():
u"""Main function for assignment 02."""
# Load prepared data.
df = return_processed_diamonds_data_set()
# Mass is already included as mass in SI units.
df.drop(['carat'], inplace=True, axis=1)
# A bit of error checking.
if df.isnull().sum().sum() != 0:
raise ValueError('Your data has unintended nulls.')
# A bit of data description. See histograms from assignment 01.
# print('Data description:\n', df.describe())
# Split into training and testing sets
# The predictirs should not include any price variable since this was used
# to create the output variable
predictors = [x for x in df.columns.tolist() if 'price' not in x]
print('Input variables:', predictors)
input_variables = df[predictors]
output_variable = df.price_expensive_binary.copy() # Categorized price
input_training, input_test, output_training, output_test = train_test_split(
input_variables, output_variable, test_size=0.3, random_state=0)
# Random forests and extra tree classifiers (extremely randomized trees) are
# two ways of improving the bias-variance trade off
# IN RANDOM FORESTS: 'each tree in the ensemble is built from a sample drawn
# with replacement (i.e., a bootstrap sample) from the training set. In
# addition, when splitting a node during the construction of the tree, the
# split that is chosen is no longer the best split among all features.
# Instead, the split that is picked is the best split among a random subset
# of the features. As a result of this randomness, the bias of the forest
# usually slightly increases (with respect to the bias of a single
# non-random tree) but, due to averaging, its variance also decreases,
# usually more than compensating for the increase in bias, hence yielding an
# overall better model.'
rf_classifier = RandomForestClassifier(
n_estimators=400,
max_depth=5,
min_samples_split=500,
min_samples_leaf=100,
random_state=0,
# verbose=True,
n_jobs=-1)
rf_classifier.fit(input_training, output_training)
predictions = rf_classifier.predict(input_test)
print('Random Forest')
print('confusion matrix:\n:',
sklearn.metrics.confusion_matrix(output_test, predictions))
print('accuracy score:\n',
sklearn.metrics.accuracy_score(output_test, predictions))
rf_classifier.variable_feature_importances = dict(
zip(predictors, rf_classifier.feature_importances_))
# EXTREMELY RANDOMIZED TREES: In extremely randomized trees (see
# ExtraTreesClassifier and ExtraTreesRegressor classes), randomness goes one
# step further in the way splits are computed. As in random forests, a
# random subset of candidate features is used, but instead of looking for
# the most discriminative thresholds, thresholds are drawn at random for
# each candidate feature and the best of these randomly-generated thresholds
# is picked as the splitting rule. This usually allows to reduce the
# variance of the model a bit more, at the expense of a slightly greater
# increase in bias:
er_classifier = ExtraTreesClassifier(
n_estimators=400,
max_depth=5,
min_samples_split=500,
min_samples_leaf=100,
random_state=0,
# verbose=True,
n_jobs=-1)
er_classifier.fit(input_training, output_training)
predictions = er_classifier.predict(input_test)
er_classifier.variable_feature_importances = dict(
zip(predictors, er_classifier.feature_importances_))
print('Extreme Classifier Tree')
print('confusion matrix:\n:',
sklearn.metrics.confusion_matrix(output_test, predictions))
print('accuracy score:\n',
sklearn.metrics.accuracy_score(output_test, predictions))
print()
return {'extreme': er_classifier,
'random_forest': rf_classifier,
'dataframe': df}