Pat*_*bug 3 python random-forest scikit-learn
这是一个有点菜鸟的问题。
我想训练一个Random Forest使用RandomForestClassifierfrom sklearn。我有几个变量,但在这些变量中,我希望算法SourceID在它训练的每一棵树中确定一个变量(我们称之为)。
我怎么做?在这种情况下,我在分类器中看不到任何有帮助的参数。
任何帮助,将不胜感激!TIA。
编辑
所以这是我的场景..
如果老师在 上布置作业Concept A,我必须预测下一个可能的作业概念。下一个分配的概念将在很大程度上取决于Concept A已经分配的概念。例如 - 在分配“牛顿第一运动定律”之后,很有可能会分配“牛顿第二运动定律”。很多时候,例如,在 之后分配的概念的选择Concept A是有限的。Concept A鉴于过去的数据,我想预测分配后的最佳可能选项。
如果我让random forest随机选择变量的工作完成它的工作,那么将会有一些树没有变量 for Concept A,在这种情况下,预测可能没有多大意义,这就是为什么我想强制这样做变量进入选择。更好的是,如果将此变量选为每棵树中要拆分的第一个变量,那就太好了。
这能说明问题吗?是random forest不是为这个职位的候选人?
在 中没有这个选项RandomForestClassifier,但随机森林算法只是决策树的集合,其中每棵树只考虑所有可能特征的一个子集,并在训练数据的引导子样本上进行训练。
因此,对于被迫使用特定功能集的树,我们自己手动创建它并不太困难。我在下面写了一个类来做到这一点。这并不能像执行稳健的输入验证什么的,但你可以咨询sklearn的随机森林的源头fit该功能。这是为了让您了解如何自己构建它:
固定功能RFC.py
import numpy as np
from sklearn.tree import DecisionTreeClassifier
class FixedFeatureRFC:
def __init__(self, n_estimators=10, random_state=None):
self.n_estimators = n_estimators
if random_state is None:
self.random_state = np.random.RandomState()
def fit(self, X, y, feats_fixed=None, max_features=None, bootstrap_frac=0.8):
"""
feats_fixed: indices of features (columns of X) to be
always used to train each estimator
max_features: number of features that each estimator will use,
including the fixed features.
bootstrap_frac: size of bootstrap sample that each estimator will use.
"""
self.estimators = []
self.feats_used = []
self.n_classes = np.unique(y).shape[0]
if feats_fixed is None:
feats_fixed = []
if max_features is None:
max_features = X.shape[1]
n_samples = X.shape[0]
n_bs = int(bootstrap_frac*n_samples)
feats_fixed = list(feats_fixed)
feats_all = range(X.shape[1])
random_choice_size = max_features - len(feats_fixed)
feats_choosable = set(feats_all).difference(set(feats_fixed))
feats_choosable = np.array(list(feats_choosable))
for i in range(self.n_estimators):
chosen = self.random_state.choice(feats_choosable,
size=random_choice_size,
replace=False)
feats = feats_fixed + list(chosen)
self.feats_used.append(feats)
bs_sample = self.random_state.choice(n_samples,
size=n_bs,
replace=True)
dtc = DecisionTreeClassifier(random_state=self.random_state)
dtc.fit(X[bs_sample][:,feats], y[bs_sample])
self.estimators.append(dtc)
def predict_proba(self, X):
out = np.zeros((X.shape[0], self.n_classes))
for i in range(self.n_estimators):
out += self.estimators[i].predict_proba(X[:,self.feats_used[i]])
return out / self.n_estimators
def predict(self, X):
return self.predict_proba(X).argmax(axis=1)
def score(self, X, y):
return (self.predict(X) == y).mean()
Run Code Online (Sandbox Code Playgroud)
这是一个测试脚本,用于查看上述类是否按预期工作:
测试文件
import numpy as np
from sklearn.datasets import load_breast_cancer
from FixedFeatureRFC import FixedFeatureRFC
rs = np.random.RandomState(1234)
BC = load_breast_cancer()
X,y = BC.data, BC.target
train = rs.rand(X.shape[0]) < 0.8
print "n_features =", X.shape[1]
fixed = [0,4,21]
maxf = 10
ffrfc = FixedFeatureRFC(n_estimators=1000)
ffrfc.fit(X[train], y[train], feats_fixed=fixed, max_features=maxf)
for feats in ffrfc.feats_used:
assert len(feats) == maxf
for f in fixed:
assert f in feats
print ffrfc.score(X[~train], y[~train])
Run Code Online (Sandbox Code Playgroud)
输出是:
n_features = 30
0.983739837398
Run Code Online (Sandbox Code Playgroud)
没有一个断言失败,表明我们选择修复的特征用于每个随机特征子样本,并且每个特征子样本的max_features大小都是所需的大小。保留数据的高精度表明分类器工作正常。
| 归档时间: |
|
| 查看次数: |
1063 次 |
| 最近记录: |