有没有人有一些使用随机森林与2.3.1 API垫而不是cvMat的例子?
基本上我有一个Matrix Mat数据,由1000行和16x16x3元素组成,Matrix Mat响应1000x1矩阵,该矩阵包含每行所属的类.我想在此运行随机森林算法.
我在一个大数据问题中使用随机森林,它有一个非常不平衡的响应类,所以我阅读了文档,我发现了以下参数:
strata
sampsize
Run Code Online (Sandbox Code Playgroud)
这些参数的文档很少(或者我没有运气找到它),我真的不明白如何实现它.我使用以下代码:
randomForest(x=predictors,
y=response,
data=train.data,
mtry=lista.params[1],
ntree=lista.params[2],
na.action=na.omit,
nodesize=lista.params[3],
maxnodes=lista.params[4],
sampsize=c(250000,2000),
do.trace=100,
importance=TRUE)
Run Code Online (Sandbox Code Playgroud)
响应是一个具有两个可能值的类,第一个出现的频率高于第二个(10000:1或更高)
这list.params是一个包含不同参数的列表(呃!我知道......)
好吧,问题(再次)是:我如何使用'strata'参数?我正确使用sampsize?
最后,有时我会收到以下错误:
Error in randomForest.default(x = predictors, y = response, data = train.data, :
Still have fewer than two classes in the in-bag sample after 10 attempts.
Run Code Online (Sandbox Code Playgroud)
对不起如果我做了很多(也许是愚蠢的)问题......
我无法找到一种在我试图生成的回归随机森林模型上执行交叉验证的方法.
所以我有一个包含1664个解释变量(不同的化学特性)的数据集,有一个响应变量(保留时间).我正在尝试生成回归随机森林模型,以便能够根据其保留时间预测某些物质的化学特性.
ID RT (seconds) 1_MW 2_AMW 3_Sv 4_Se
4281 38 145.29 5.01 14.76 28.37
4952 40 132.19 6.29 11 21.28
4823 41 176.21 7.34 12.9 24.92
3840 41 174.24 6.7 13.99 26.48
3665 42 240.34 9.24 15.2 27.08
3591 42 161.23 6.2 13.71 26.27
3659 42 146.22 6.09 12.6 24.16
Run Code Online (Sandbox Code Playgroud)
这是我的表格的一个例子.我想基本上将RT映射到1_MW等(最多1664个变量),因此我可以找到哪些变量很重要而哪些变量不重要.
我做:-
r = randomForest(RT..seconds.~., data = cadets, importance =TRUE, do.trace = 100)
varImpPlot(r)
Run Code Online (Sandbox Code Playgroud)
它告诉我哪些变量很重要,哪些变量不重要,这很好.但是,我希望能够对我的数据集进行分区,以便我可以对其进行交叉验证.我找到了一个在线教程,解释了如何做到这一点,但是对于分类模型而不是回归.
我明白你这样做: -
k = 10
n = floor(nrow(cadets)/k)
i = 1
s1 = ((i-1) …Run Code Online (Sandbox Code Playgroud) 我创建了一个随机森林并预测了我的测试集的类,它们在数据帧中幸福地生活:
row.names class 564028 1 275747 1 601137 0 922930 1 481988 1 ...
该 row.names属性告诉我在进行各种操作之前哪一行是哪一行,这些操作在进程中扰乱了行的顺序.到现在为止还挺好.
现在,我想对我的预测的准确性有一个普遍的感觉.为此,我需要使用此数据框并根据row.names属性按升序对其重新排序.通过这种方式,我可以将观察结果与行标识进行比较,我已经知道了.
请原谅我提出这样一个基本问题,但对于我的生活,我找不到关于如何完成这项琐碎任务的良好信息来源.
文档恳求我:
使用
attr(x, "row.names"),如果你需要获取一个整数值组行的名字.
但这让我一无所获NULL.
我的问题是,row.names在整个工作流程中,我如何使用忠实地跟随我的数据框架的各种形式?这不是它的用途吗?
我是numpy的新手,我正在使用python中的随机林实现集群.我的问题是:
我怎样才能找到数组中确切行的索引?例如
[[ 0. 5. 2.]
[ 0. 0. 3.]
[ 0. 0. 0.]]
Run Code Online (Sandbox Code Playgroud)
我寻找[0. 0. 3.]并得到结果1(第二行的索引).
有什么建议吗?遵循代码(不工作......)
for index, element in enumerate(leaf_node.x):
for index_second_element, element_two in enumerate(leaf_node.x):
if (index <= index_second_element):
index_row = np.where(X == element)
index_column = np.where(X == element_two)
self.similarity_matrix[index_row][index_column] += 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用scikit-learn和随机林分类器预先形成递归特征消除,使用OOB ROC作为对递归过程中创建的每个子集进行评分的方法.
但是,当我尝试使用该RFECV方法时,我收到一个错误说法AttributeError: 'RandomForestClassifier' object has no attribute 'coef_'
随机森林本身没有系数,但它们确实按基尼评分排名.所以,我想知道如何解决这个问题.
请注意,我想使用一种方法,明确地告诉我pandas在最佳分组中选择了我的DataFrame中的哪些功能,因为我使用递归功能选择来尝试最小化我将输入到最终分类器的数据量.
这是一些示例代码:
from sklearn import datasets
import pandas as pd
from pandas import Series
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import RFECV
iris = datasets.load_iris()
x=pd.DataFrame(iris.data, columns=['var1','var2','var3', 'var4'])
y=pd.Series(iris.target, name='target')
rf = RandomForestClassifier(n_estimators=500, min_samples_leaf=5, n_jobs=-1)
rfecv = RFECV(estimator=rf, step=1, cv=10, scoring='ROC', verbose=2)
selector=rfecv.fit(x, y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/bbalin/anaconda/lib/python2.7/site-packages/sklearn/feature_selection/rfe.py", line 336, in fit
ranking_ = …Run Code Online (Sandbox Code Playgroud) 我在使用RandomForest fit函数时遇到了麻烦
这是我的训练集
P1 Tp1 IrrPOA Gz Drz2
0 0.0 7.7 0.0 -1.4 -0.3
1 0.0 7.7 0.0 -1.4 -0.3
2 ... ... ... ... ...
3 49.4 7.5 0.0 -1.4 -0.3
4 47.4 7.5 0.0 -1.4 -0.3
... (10k rows)
Run Code Online (Sandbox Code Playgroud)
感谢使用sklearn.ensemble RandomForest的所有其他变量,我想预测P1
colsRes = ['P1']
X_train = train.drop(colsRes, axis = 1)
Y_train = pd.DataFrame(train[colsRes])
rf = RandomForestClassifier(n_estimators=100)
rf.fit(X_train, Y_train)
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
ValueError: Unknown label type: array([[ 0. ],
[ 0. ],
[ 0. ],
...,
[ 49.4],
[ 47.4],
Run Code Online (Sandbox Code Playgroud)
我没有发现任何有关此标签错误的信息,我使用的是Python …
我想绘制一个随机森林的决策树.所以,我创建以下代码:
clf = RandomForestClassifier(n_estimators=100)
import pydotplus
import six
from sklearn import tree
dotfile = six.StringIO()
i_tree = 0
for tree_in_forest in clf.estimators_:
if (i_tree <1):
tree.export_graphviz(tree_in_forest, out_file=dotfile)
pydotplus.graph_from_dot_data(dotfile.getvalue()).write_png('dtree'+ str(i_tree) +'.png')
i_tree = i_tree + 1
Run Code Online (Sandbox Code Playgroud)
但它没有产生任何东西..你知道如何从随机森林中绘制决策树吗?
谢谢,
我试图以任何方式获得射频模型的变量重要性.这是我到目前为止尝试过的方法,但非常欢迎其他建议.
我在R训练了一个模型:
require(caret)
require(randomForest)
myControl = trainControl(method='cv',number=5,repeats=2,returnResamp='none')
model2 = train(increaseInAssessedLevel~., data=trainData, method = 'rf', trControl=myControl)
Run Code Online (Sandbox Code Playgroud)
数据集相当大,但模型运行正常.我可以访问它的部件并运行命令,例如:
> model2[3]
$results
mtry RMSE Rsquared RMSESD RsquaredSD
1 2 0.1901304 0.3342449 0.004586902 0.05089500
2 61 0.1080164 0.6984240 0.006195397 0.04428158
3 120 0.1084201 0.6954841 0.007119253 0.04362755
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
> varImp(model2)
Error in varImp[, "%IncMSE"] : subscript out of bounds
Run Code Online (Sandbox Code Playgroud)
显然应该有一个包装器,但似乎并非如此:(cf:http://www.inside-r.org/packages/cran/caret/docs/varImp)
varImp.randomForest(model2)
Error: could not find function "varImp.randomForest"
Run Code Online (Sandbox Code Playgroud)
但这特别奇怪:
> traceback()
No traceback available
> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-redhat-linux-gnu …Run Code Online (Sandbox Code Playgroud) 我在python中使用RandomForestRegressor,我想创建一个图表来说明功能重要性的排名.这是我使用的代码:
from sklearn.ensemble import RandomForestRegressor
MT= pd.read_csv("MT_reduced.csv")
df = MT.reset_index(drop = False)
columns2 = df.columns.tolist()
# Filter the columns to remove ones we don't want.
columns2 = [c for c in columns2 if c not in["Violent_crime_rate","Change_Property_crime_rate","State","Year"]]
# Store the variable we'll be predicting on.
target = "Property_crime_rate"
# Let’s randomly split our data with 80% as the train set and 20% as the test set:
# Generate the training set. Set random_state to be able to replicate results.
train2 = …Run Code Online (Sandbox Code Playgroud) random-forest ×10
python ×5
r ×4
scikit-learn ×3
arrays ×1
c++ ×1
dataframe ×1
numpy ×1
opencv ×1
pandas ×1
plot ×1
pydot ×1
python-3.x ×1
r-caret ×1
tree ×1