本文整理汇总了Python中sklearn.ensemble.ExtraTreesClassifier类的典型用法代码示例。如果您正苦于以下问题:Python ExtraTreesClassifier类的具体用法?Python ExtraTreesClassifier怎么用?Python ExtraTreesClassifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExtraTreesClassifier类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: calc_prob
def calc_prob(df_features_driver, df_features_other):
df_train = df_features_driver.append(df_features_other)
df_train.reset_index(inplace = True)
df_train.Driver = df_train.Driver.astype(int)
# So far, the best result was achieved by using a RandomForestClassifier with Bagging
# model = BaggingClassifier(base_estimator = ExtraTreesClassifier())
# model = BaggingClassifier(base_estimator = svm.SVC(gamma=2, C=1))
# model = BaggingClassifier(base_estimator = linear_model.LogisticRegression())
# model = BaggingClassifier(base_estimator = linear_model.LogisticRegression())
# model = BaggingClassifier(base_estimator = AdaBoostClassifier())
#model = RandomForestClassifier(200)
# model = BaggingClassifier(base_estimator = [RandomForestClassifier(), linear_model.LogisticRegression()])
# model = EnsembleClassifier([BaggingClassifier(base_estimator = RandomForestClassifier()),
# GradientBoostingClassifier])
#model = GradientBoostingClassifier(n_estimators = 10000)
model = ExtraTreesClassifier(n_estimators=100,max_features='auto',random_state=0, n_jobs=2, criterion='entropy', bootstrap=True)
# model = ExtraTreesClassifier(500, criterion='entropy')
feature_columns = df_train.iloc[:, 4:]
# Train the classifier
model.fit(feature_columns, df_train.Driver)
df_submission = pd.DataFrame()
df_submission['driver_trip'] = create_first_column(df_features_driver)
probs_array = model.predict_proba(feature_columns[:200]) # Return array with the probability for every driver
probs_df = pd.DataFrame(probs_array)
df_submission['prob'] = np.array(probs_df.iloc[:, 1])
return df_submission
开发者ID:EdwardBetts,项目名称:awesome-kagg-ml,代码行数:34,代码来源:Step_3_ExtraTrees.py
示例2: ET_classif
def ET_classif(features_df=None, labels_df=None):
'''Scoring function to be used in SelectKBest feature selection class
object.
This scoring function assigns varaible importances to the features
passed in to it using the ExtraTreesClassifier. It then returns
the features as two identical arrays mimicking the scores and
p-values arrays required by SelectKBest to pick the top K
features.
Args:
features_df: Pandas dataframe of features to be used to predict
using the ExtraTreesClassifier.
labels_df: Pandas dataframe of the labels being predicted.
Returns:
Two identical arrays containing the feature importance scores
returned for each feature by the ExtraTreesClassifier.
'''
reducer = ExtraTreesClassifier(n_estimators=500, bootstrap=False,
oob_score=False, max_features=.10,
min_samples_split=10, min_samples_leaf=2,
criterion='gini', random_state=42)
reducer.fit(features_df, labels_df)
return reducer.feature_importances_, reducer.feature_importances_
开发者ID:DingChiLin,项目名称:FCH808.github.io,代码行数:25,代码来源:poi_model.py
示例3: learn
def learn(f):
global raw_data
print 'testing classifier'
data = raw_data[raw_data['label'] != 'unknown']
data = data[data['file type'] == 'EXECUTE']
X = data.as_matrix(f)
y = np.array(data['label'].tolist())
#clf = RandomForestClassifier(n_estimators=100)
clf = ExtraTreesClassifier(n_estimators=100)
#clf = AdaBoostClassifier()
scores = sklearn.cross_validation.cross_val_score(clf, X, y, cv=10)
print("predicted accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
seed = 3301
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=seed)
clf.fit(X_train, y_train)
scores = clf.score(X_test, y_test)
print("actual accuracy: %0.2f" % scores)
importances = zip(f, clf.feature_importances_)
importances.sort(key=lambda k:k[1], reverse=True)
for im in importances[0:20]:
print im[0].ljust(30), im[1]
#y_pred = clf.predict(X_test)
#labels = ['good', 'bad']
#cm = confusion_matrix(y_test, y_pred, labels)
#plot_cm(cm, labels)
#joblib.dump(clf, 'model.pkl')
return clf
开发者ID:fxfactorial,项目名称:macholibre,代码行数:27,代码来源:create_model.py
示例4: tree_based_selection
def tree_based_selection(self, data_set, data_target, feature_names):
"""
:param data_set:
:return:
"""
clf = ExtraTreesClassifier()
clf = clf.fit(data_set, data_target)
print clf.feature_importances_
model = SelectFromModel(clf, prefit=True)
feature_set = model.transform(data_set)
fea_index = []
for A_col in np.arange(data_set.shape[1]):
for B_col in np.arange(feature_set.shape[1]):
if (data_set[:, A_col] == feature_set[:, B_col]).all():
fea_index.append(A_col)
check = {}
for i in fea_index:
check[feature_names[i]] = data_set[0][i]
print np.array(check)
return feature_set, fea_index
开发者ID:StevenLOL,项目名称:kdd99-scikit,代码行数:26,代码来源:CART_Trainer.py
示例5: train_random_forest
def train_random_forest(X_train,y_train,**kwargs):
from sklearn.ensemble import ExtraTreesClassifier
n_estimators = kwargs.pop('n_estimators',300)
max_features = kwargs.pop('max_features','auto')
n_jobs = kwargs.pop('n_jobs',-1)
verbose = kwargs.pop('verbose',0)
tuned_params = kwargs.pop('tuned_params',None)
# initialize baseline classifier
clf = ExtraTreesClassifier(n_estimators=n_estimators,random_state=42,
n_jobs=n_jobs,verbose=verbose,criterion='gini',
max_features=max_features,oob_score=True,
bootstrap=True)
if tuned_params is not None: # optimize if desired
from sklearn.grid_search import GridSearchCV
cv = GridSearchCV(clf,tuned_params,cv=5,scoring='roc_auc',
n_jobs=n_jobs,verbose=verbose,refit=True)
cv.fit(X_train, y_train)
clf = cv.best_estimator_
else: # otherwise train with the specified parameters (no tuning)
clf.fit(X_train,y_train)
return clf
开发者ID:caseyjlaw,项目名称:activecontainer,代码行数:25,代码来源:sklearn_utils.py
示例6: tree_based_feature_selection
def tree_based_feature_selection(self, x: np.ndarray, y: np.ndarray) -> np.ndarray:
n = len(self.features)
forest = ExtraTreesClassifier(n_estimators=250, random_state=0)
forest.fit(x, y)
importances = forest.feature_importances_
print(importances)
std = np.std([tree.feature_importances_ for tree in forest.estimators_], axis=0)
indices = np.argsort(importances)[::-1]
print("Feature ranking:")
for f in range(n):
print("%d. feature %d: %s (%f)" % (f + 1, indices[f], self.features[indices[f]],importances[indices[f]]))
# Plot the feature importances of the forest
# plt.figure()
# plt.title("Feature importances")
# plt.bar(range(n), importances[indices],
# color="r", yerr=std[indices], align="center")
# plt.xticks(range(n), indices)
# plt.xlim([-1, n])
# plt.show()
n = 12
print(indices[0:n+1])
print(self.features[indices[0:n+1]])
new_x = x[:, indices[0:n+1]]
return new_x
开发者ID:qianFX,项目名称:final_project,代码行数:26,代码来源:kdc.py
示例7: crossVal
def crossVal(positions, X, y, missedYFile):
outF = open(missedYFile, 'w')
posArray = np.array(positions)
# Split into training and test
sss = StratifiedShuffleSplit(y, 4, test_size=0.1, random_state=442)
cvRound = 0
for train_index, test_index in sss:
clf = ExtraTreesClassifier(n_estimators=300,
random_state=13,
bootstrap=True,
max_features=20,
min_samples_split=1,
max_depth=8,
min_samples_leaf=13,
n_jobs=4
)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
pos_test = posArray[test_index]
clf = clf.fit(X_train, y_train)
preds = clf.predict(X_test)
metrics.confusion_matrix( y_test, preds )
print( metrics.classification_report(y_test, clf.predict(X_test)) )
for loc,t,p in zip(pos_test, y_test, preds):
if t=='0' and p=='1':
print >> outF, loc + '\t' + str(cvRound)
cvRound += 1
outF.close()
开发者ID:samesense,项目名称:snv_decision_tree,代码行数:29,代码来源:fairForestLimitFeatures.py
示例8: remove_feature_tree_based
def remove_feature_tree_based(train_X,train_Y):
'''
Removes features based on trees - see sklearn:
http://scikit-learn.org/dev/auto_examples/ensemble/plot_forest_importances.html#example-ensemble-plot-forest-importances-py
Actually removes based on "importance"
'''
forest = ExtraTreesClassifier(n_estimators=1000,
compute_importances = True,
random_state = 0)
forest.fit(train_X, train_Y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
x_labels = ['rc1', 'rc2', 'dca1', 'dca2','dcm1', 'dcm2','ace1','ace2','acsc1', 'acsc2', 'acsv1', 'acsv2', 'acss1','acss2', 'acsk1', 'acsk2', 'taca1', 'taca2', 'tdc1', 'tdc2', 'gmin', 'gmean', 'trd','ep111','ep112','ep211', 'ep212', 'ep311','ep312', 'ep411','ep412','ep511','ep512','ep611','ep612','ep121','ep122','ep221', 'ep222', 'ep321','ep322', 'ep421','ep422','ep521','ep522','ep621','ep622']
# Print the feature ranking
print "Feature ranking:"
for f in xrange(46):
print "%d. feature %s (%f)" % (f + 1, x_labels[indices[f]], importances[indices[f]])
# Transform the data to have only the features that are important
x_new = forest.transform(train_X)
return (forest, x_new)
开发者ID:IanTheEngineer,项目名称:Penn-haptics-bolt,代码行数:29,代码来源:train_adjective_phase_feature_selection.py
示例9: algo_fit_cross_validated
def algo_fit_cross_validated(training_matrix, target):
# Build a forest and compute the feature importances
forest = ExtraTreesClassifier(n_estimators=250,
random_state=0)
forest.fit(training_matrix, target)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
l = list(training_matrix.columns.values)
for f in range(training_matrix.shape[1]):
print("%d. feature %d(%s) (%f)" % (f + 1, indices[f], l[indices[f]], importances[indices[f]]))
##### Works well ######
# SVM
# svm = SVC(kernel="linear", C=0.06)
# svm.fit(training_matrix, target)
#
# scores_svm = cross_validation.cross_val_score(svm, training_matrix, target, cv=5)
# print("(svm) Accuracy: %0.5f (+/- %0.2f)" % (scores_svm.mean(), scores_svm.std() * 2))
#
# return svm
##### Works well ######
# Random Forest
rf = RandomForestClassifier(n_estimators=1500, max_depth=2, max_features=4)
scores_rf = cross_validation.cross_val_score(rf, training_matrix, target, cv=5)
print("(Random Forest) Accuracy: %0.5f (+/- %0.2f)" % (scores_rf.mean(), scores_rf.std() * 2))
rf.fit(training_matrix, target)
return rf
开发者ID:kraktos,项目名称:Data_Science_Analytics,代码行数:32,代码来源:Main.py
示例10: extratreeclassifier
def extratreeclassifier(input_file,Output,test_size):
lvltrace.lvltrace("LVLEntree dans extratreeclassifier split_test")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
print X_train.shape, X_test.shape
clf = ExtraTreesClassifier(n_estimators=10)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print "Extremely Randomized Trees"
print "classification accuracy:", metrics.accuracy_score(y_test, y_pred)
print "precision:", metrics.precision_score(y_test, y_pred)
print "recall:", metrics.recall_score(y_test, y_pred)
print "f1 score:", metrics.f1_score(y_test, y_pred)
print "\n"
results = Output+"_Extremely_Random_Forest_metrics_test.txt"
file = open(results, "w")
file.write("Extremely Random Forest Classifier estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y_test, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y_test, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y_test, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y_test, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y_test)):
file.write("%f,%f,%i\n"%(y_test[n],y_pred[n],(n+1)))
file.close()
title = "Extremely Randomized Trees %f"%test_size
save = Output + "Extremely_Randomized_Trees_confusion_matrix"+"_%s.png"%test_size
plot_confusion_matrix(y_test, y_pred,title,save)
lvltrace.lvltrace("LVLSortie dans extratreeclassifier split_test")
开发者ID:xaviervasques,项目名称:Neuron_Morpho_Classification_ML,代码行数:34,代码来源:supervised_split_test.py
示例11: reduceRF
def reduceRF(label):
global x_data_rf_reduced, importantFeatureLocs
model = ExtraTreesClassifier()
model.fit(x_data, y_data[:, label])
# the relative importance of each attribute
importance = model.feature_importances_
weight = float(0)
del importantFeatureLocs[:] # reset
#print(importance)
for ele in np.sort(importance)[::-1]:
weight += float(ele)
featureIndex = np.where(importance==ele)
for loc in featureIndex[0]:
importantFeatureLocs.append(loc)
if weight > RFThreshold :
break
# remove duplications
importantFeatureLocs = list(set(importantFeatureLocs))
# extracting relevant columns from input data. Note that importantFeatureLocs
# may be unsorted (since python 'set' is unsorted), so features are extracted
# in unorderd fashion. This info is stored in the softmax model class
x_data_rf_reduced = x_data[:, importantFeatureLocs]
开发者ID:tgangwani,项目名称:DynReconfig,代码行数:27,代码来源:learn.py
示例12: fit
def fit(self, X, Y, sample_weight=None):
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
num_features = X.shape[1]
max_features = int(float(self.max_features) * (np.log(num_features) + 1))
# Use at most half of the features
max_features = max(1, min(int(X.shape[1] / 2), max_features))
estimator = ExtraTreesClassifier(
n_estimators=self.n_estimators,
criterion=self.criterion,
max_depth=self.max_depth,
min_samples_split=self.min_samples_split,
min_samples_leaf=self.min_samples_leaf,
bootstrap=self.bootstrap,
max_features=max_features,
max_leaf_nodes=self.max_leaf_nodes,
oob_score=self.oob_score,
n_jobs=self.n_jobs,
verbose=self.verbose,
random_state=self.random_state,
class_weight=self.class_weight,
)
estimator.fit(X, Y, sample_weight=sample_weight)
self.preprocessor = SelectFromModel(estimator=estimator, threshold="mean", prefit=True)
return self
开发者ID:automl,项目名称:auto-sklearn,代码行数:26,代码来源:extra_trees_preproc_for_classification.py
示例13: MyExtraTree
class MyExtraTree(MyClassifier):
def __init__(self, params=dict()):
self._params = params
self._extree = ExtraTreesClassifier(**(self._params))
def update_params(self, updates):
self._params.update(updates)
self._extree = ExtraTreesClassifier(**(self._params))
def fit(self, Xtrain, ytrain):
self._extree.fit(Xtrain, ytrain)
# def predict(self, Xtest, option = None):
# return self._extree.predict(Xtest)
def predict_proba(self, Xtest, option = None):
return self._extree.predict_proba(Xtest)[:, 1]
def predict_proba_multi(self, Xtest, option = None):
return self._extree.predict_proba(Xtest)
def plt_feature_importance(self, fname_list, f_range = list()):
importances = self._extree.feature_importances_
std = np.std([tree.feature_importances_ for tree in self._extree.estimators_], axis=0)
indices = np.argsort(importances)[::-1]
fname_array = np.array(fname_list)
if not f_range:
f_range = range(indices.shape[0])
n_f = len(f_range)
plt.figure()
plt.title("Extra Tree Feature importances")
plt.barh(range(n_f), importances[indices[f_range]],
color="b", xerr=std[indices[f_range]], ecolor='k',align="center")
plt.yticks(range(n_f), fname_array[indices[f_range]])
plt.ylim([-1, n_f])
plt.show()
def list_feature_importance(self, fname_list, f_range = list(), return_list = False):
importances = self._extree.feature_importances_
indices = np.argsort(importances)[::-1]
print 'Extra tree feature ranking:'
if not f_range :
f_range = range(indices.shape[0])
n_f = len(f_range)
for i in range(n_f):
f = f_range[i]
print '{0:d}. feature[{1:d}] {2:s} ({3:f})'.format(f + 1, indices[f], fname_list[indices[f]], importances[indices[f]])
if return_list:
return [indices[f_range[i]] for i in range(n_f)]
开发者ID:tonyzhangrt,项目名称:wklearn,代码行数:60,代码来源:learner.py
示例14: _cascade_layer
def _cascade_layer(self, X, y=None, layer=0):
n_tree = getattr(self, 'n_cascadeRFtree')
n_cascadeRF = getattr(self, 'n_cascadeRF')
min_samples = getattr(self, 'min_samples_cascade')
prf = RandomForestClassifier(
n_estimators=100, max_features=8,
bootstrap=True, criterion="entropy", min_samples_split=20,
max_depth=None, class_weight='balanced', oob_score=True)
crf = ExtraTreesClassifier(
n_estimators=100, max_depth=None,
bootstrap=True, oob_score=True)
prf_pred = []
if y is not None:
# print('Adding/Training Layer, n_layer={}'.format(self.n_layer))
for irf in range(n_cascadeRF):
prf.fit(X, y)
crf.fit(X, y)
setattr(self, '_casprf{}_{}'.format(self.n_layer, irf), prf)
setattr(self, '_cascrf{}_{}'.format(self.n_layer, irf), crf)
probas = prf.oob_decision_function_
probas += crf.oob_decision_function_
prf_pred.append(probas)
elif y is None:
for irf in range(n_cascadeRF):
prf = getattr(self, '_casprf{}_{}'.format(layer, irf))
crf = getattr(self, '_cascrf{}_{}'.format(layer, irf))
probas = prf.predict_proba(X)
probas += crf.predict_proba(X)
prf_pred.append(probas)
return prf_pred
开发者ID:TinghuiWang,项目名称:pyActLearn,代码行数:33,代码来源:gcforest.py
示例15: plotImportance
def plotImportance(X,y):
forest = ExtraTreesClassifier(n_estimators=250,
random_state=0)
forest.fit(X, y)
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
n=X.shape[1]
#Print the feature ranking
#print("Feature ranking:")
#for f in range(n):
# print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
# Plot the feature importances of the forest
plt.figure(figsize=(20,15))
plt.title("Feature importances")
plt.bar(range(n), importances[indices],
color="r", yerr=std[indices], align="center")
plt.xticks(range(n), X.columns[indices],rotation=90)
plt.xlim([-1, n])
plt.savefig('featuresel.pdf')
开发者ID:Johayon,项目名称:BGD-Work,代码行数:25,代码来源:featuresSelection.py
示例16: FeaturesImportance
def FeaturesImportance(trainData, trainLabels):
forest = ExtraTreesClassifier(n_estimators=250, random_state=0)
forest.fit(trainData, trainLabels)
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(16):
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(16), importances[range(16)], color="r", align="center")
plt.xticks(range(16), [r'$x_1$', r'$x_2$', r'$x_3$', r'$x_4$', r'$x_5$',
r'$x_6$', r'$x_7$', r'$x_8$', r'$x_9$', r'$x_{10}$',
r'$x_{11}$', r'$x_{12}$', r'$x_{13}$', r'$x_{14}$', r'$x_{15}$',
r'$x_{16}$'])
plt.yticks([0.0, 0.05, 0.10, 0.15, 0.20, 0.25], [r'$0.00$', r'$0.05$', r'$0.10$', r'$0.15$', r'$0.20$', r'$0.25$'])
plt.xlabel('Features')
plt.ylabel('Importance')
plt.xlim([-1, 16])
plt.show()
return importances
开发者ID:kmakantasis,项目名称:SVM-AnchorGraphLabeling,代码行数:28,代码来源:kmClassification.py
示例17: FeaturesSelectionRandomForests
class FeaturesSelectionRandomForests(object):
def __init__(self, n_estimators = 100, feature_importance_th = 0.005):
self.n_estimators = n_estimators
self.feature_importance_th = feature_importance_th
def fit(self, X, y, n_estimators = None, feature_importance_th = None):
if n_estimators is not None:
assert isinstance(n_estimators,(int,long,float))
self.n_estimators = n_estimators
if feature_importance_th is not None:
assert isinstance(feature_importance_th,(int,long,float))
self.feature_importance_th = feature_importance_th
#filter features by forest model
self.trees = ExtraTreesClassifier(n_estimators=100, compute_importances=True)
self.trees.fit(X, y)
self.features_mask = np.where(self.trees.feature_importances_ > 0.005)[0]
def plot_features_importance(self):
pd.DataFrame(self.trees.feature_importances_).plot(kind='bar')
plt.show()
def transform(self, X):
assert hasattr(self,"features_mask")
return X[:, self.features_mask]
开发者ID:macchineimparanti,项目名称:impara,代码行数:35,代码来源:features_filtering.py
示例18: select_with_forest
def select_with_forest(X, y, n_trees=10, treshold=0.01):
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import ExtraTreesClassifier
import pandas as pd
import numpy as np
# encode labels (str -> int):
le = LabelEncoder()
X = X.copy()
for col in X.columns:
le.fit(X[col].unique())
X[col] = le.transform(X[col])
# train the classifier:
forest = ExtraTreesClassifier(criterion="entropy", n_estimators=n_trees)
forest.fit(X, y)
print('number of selected features: ', np.sum(forest.feature_importances_ >= treshold))
# select important features:
importances = pd.DataFrame()
importances['predictor name'] = X.columns.tolist()
importances['importance'] = forest.feature_importances_
importances = importances.sort_values(by='importance', ascending=False)
#X2 = forest.transform(X, treshold)
#labels2 = X.columns[list(forest.feature_importances_>=treshold)]
#X2 = pd.DataFrame(X2)
#X2.columns = labels2
return importances #X2
开发者ID:d-an,项目名称:notebooky,代码行数:25,代码来源:model.py
示例19: top_importances
def top_importances(features_df=None, labels_df=None, top_N=10):
''' Finds the top N importances using the ExtraTreesClassifier.
Finds the top N importances of a dataframe of features and a dataframe
of labels using the ExtraTreesClassifier.
Args:
features_df: Pandas dataframe of features used to predict.
labels_df: Pandas dataframe of labels to be predicted.
top_N: interger value of the top N most importance features to return.
Returns:
Pandas dataframe containing the top N importances and their
importance scores.
'''
reducer = ExtraTreesClassifier(n_estimators=2000, bootstrap=False,
oob_score=False, max_features=.10,
min_samples_split=10, min_samples_leaf=2,
criterion='gini')
reducer.fit(features_df, labels_df)
scores = pd.DataFrame(reducer.feature_importances_,
index=features_df.columns)
scores.columns = ['Importances']
scores = scores.sort(['Importances'], ascending=False)
return scores[0:top_N]
开发者ID:DingChiLin,项目名称:FCH808.github.io,代码行数:26,代码来源:poi_add_features.py
示例20: get_most_important_features
def get_most_important_features(train):
train = train.drop('ID', 1)
train_y = train['TARGET']
train_X = train.drop('TARGET', 1)
random_forest = RandomForestClassifier(n_estimators=100)
random_forest.fit(train_X, train_y)
feater_importance = pd.Series(random_forest.feature_importances_, index=train_X.columns)
feater_importance.sort_values(inplace=True)
feater_importance.tail(20).plot(kind='barh', figsize=(15 ,7), title='Feature importance by random forest')
# plt.savefig("feature_importance.png")
grad_boosting = GradientBoostingClassifier()
grad_boosting.fit(train_X, train_y)
feater_importance = pd.Series(grad_boosting.feature_importances_, index=train_X.columns)
feater_importance.sort_values(inplace=True)
feater_importance.tail(20).plot(kind='barh', figsize=(10,7), title='Feature importance by gradient boosting')
# plt.savefig("feature_importance2.png")
extra_trees = ExtraTreesClassifier()
extra_trees.fit(train_X, train_y)
feater_importance = pd.Series(extra_trees.feature_importances_, index=train_X.columns)
feater_importance.sort_values(inplace=True)
feater_importance.tail(20).plot(kind='barh', figsize=(20,7), title='Feature importance by extra trees classifier')
开发者ID:poketulhu,项目名称:happy_customers,代码行数:29,代码来源:features_selection.py
注:本文中的sklearn.ensemble.ExtraTreesClassifier类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论