本文整理汇总了Python中sklearn.ensemble.RandomForestClassifier.getImportance方法的典型用法代码示例。如果您正苦于以下问题:Python RandomForestClassifier.getImportance方法的具体用法?Python RandomForestClassifier.getImportance怎么用?Python RandomForestClassifier.getImportance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.ensemble.RandomForestClassifier
的用法示例。
在下文中一共展示了RandomForestClassifier.getImportance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getMetrics
# 需要导入模块: from sklearn.ensemble import RandomForestClassifier [as 别名]
# 或者: from sklearn.ensemble.RandomForestClassifier import getImportance [as 别名]
#.........这里部分代码省略.........
acc /= float(5)
if acc > best_acc:
best_acc = acc
best_alpha = alpha
lasso = Lasso(alpha=best_alpha)
lasso.fit(X, y_cont)
metrics.append(lasso.coef_)
#Ridge Regression
alphas = [0.03, 0.1, 0.3, 1, 3, 10]
best_alpha = 0.01
best_acc = 0
for train_index, test_index in KFold(len(y_cont), n_folds=5):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y_cont[train_index], y_cont[test_index]
ridge = Ridge(alpha=best_alpha)
ridge.fit(X_train, y_train)
pred = ridge.predict(X_test)
best_acc += self.accuracy(pred, y_test)
best_acc /= float(5)
for alpha in alphas:
acc = 0
for train_index, test_index in KFold(len(y_cont), n_folds=5):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y_cont[train_index], y_cont[test_index]
ridge = Ridge(alpha=alpha)
ridge.fit(X_train, y_train)
pred = lasso.predict(X_test)
acc += self.accuracy(pred, y_test)
acc /= float(5)
if acc > best_acc:
best_acc = acc
best_alpha = alpha
ridge = Ridge(alpha=best_alpha)
ridge.fit(X, y_cont)
metrics.append(ridge.coef_)
#SVM
clf = SVC(kernel='linear')
clf.fit(X, y_disc)
svm_weights = (clf.coef_ ** 2).sum(axis=0)
svm_weights /= float(svm_weights.max())
metrics.append(svm_weights)
#Random Forests
#rf importances
#grow forest
importances = []
for run in range(self.runs):
forest = RandomForestClassifier(
n_estimators=2000,
max_features='auto',
criterion='gini',
n_jobs=-1,
)
forest.fit(X, y_disc)
importances.append(forest.feature_importances_)
# stds.append( np.std([tree.feature_importances_ for tree in forest.estimators_], axis = 0) )
metrics.append(np.average(importances, axis=0))
metrics.append(np.std(importances, axis=0))
X, y_disc = self.reverseFixStructure(X, y_disc)
forest.fit(X,y_disc)
importances, stds = forest.getImportance()
metrics.append(importances)
metrics.append(stds)
X, y_disc = self.fixStructure(X,y_disc)
#ANOVA
anova = SelectKBest(f_regression, k=self.threshold)
anova.fit(X,y_disc)
selected_features = anova.get_support()
metrics.append(selected_features)
#Linear Discriminant Analysis
lda = LinearDiscriminantAnalysis(n_components=1)
lda.fit(X,y_disc)
metrics.append(lda.coef_[0])
#Principal Component Analysis
pca = PCA(n_components=1)
pca.fit(X)
metrics.append(pca.components_[0])
#absolute values
metrics = np.absolute(np.array(metrics))
dump(metrics, 'all_metrics', path=self.ddpad)
return np.array(metrics)