- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习机器学习,我想使用 scikit-learn 的 RandomForestRegressor()
在相当复杂的数据集上。不过,为了首先掌握它的窍门,我正在尝试完成一个基本示例,如下所示:
import sklearn.ensemble as se
import numpy as np
forest = se.RandomForestRegressor(n_estimators=1000)
traindata = np.arange(1000).reshape(200,5)
forest = forest.fit(traindata[0::,1::],traindata[0::,0])
此时,我认为我所做的是:我创建了一个 200 行矩阵,每行 5 个值,格式为 [ x, x+1, x+2, x+3, x+4 ]
其中 x
是 5 的倍数(例如 [0,1,2,3,4]
、[5,6,7,8,9]
等)。
我已经告诉我的森林适合这些特征 [ x+1, x+2, x+3, x+4 ]
预测 x
。以下是我预测时会发生的情况:
forest.predict([1,2,3,4])
>> array([2.785])
这对我来说真的很不直观。考虑 [1,2,3,4]
的特征值在 x = 0
的训练数据中,我的森林难道不能比 2.785 更准确地预测它吗?
我更进一步看到特征重要性如下:
forest.feature_importances_
>> array([0.26349716, 0.23664264, 0.23360533, 0.26625487])
对我来说,这并不意味着我所看到的方式存在重大偏差。我在这里错过了什么?
最佳答案
简短版本:由于聪明的 Breiman 提出的方法的性质。
更长的版本:
随机森林是非常有趣的学习器。
但是,您需要一点耐心才能调整它们。
forest.setp_param( oob_score = True, # set True to be able to read
# # oob-samples score
random_state = 2015 # set so as to keep retesting
# # possible / meaniningfull on
# # an otherwise randomised
# # learner construction
)
原则上,任何尝试使用 .fit()
方法在幕后做了很多工作来构建一组随机的决策树,使其成为适用于您的数据集的 RandomForest。
的“质量” .fit()
表示在 .oob_score_
这显示了已经使用的 oob
的(准确)程度。 -samples(Breiman 方法的真实部分)在针对给定的 完成训练后 RandomForest
。这有助于您估计您受过训练的 RandomForest
如何“好”或“差”在可用数据集上执行。
然而,更重要的是(或应该是)学习者的概括能力——即,一旦处理了一个看不见的例子,它的预测能力与现实的吻合程度如何。
这个可以通过 .score()
测试 训练有素的方法 RandomForest
-实例。
RandomForest 是一个“多数投票”预测器,要感受这一点,请尝试显示随机树大军的内部状态:
def printLDF( aPopulationSET ):
LDF_example, LDF_counts = np.unique( aPopulationSET, return_counts = True )
GDF_sum_scaler = float( LDF_counts.sum() )
for i in xrange( LDF_example.shape[0] ):
print "{0: > 6d}: {1: > 6d} x {2: > 15.2f} {3: > 15.4f} % {4: > 15.1f} %".format( i, LDF_counts[i], LDF_example[i], 100 * LDF_counts[i] / GDF_sum_scaler, 100 * LDF_counts[:i].sum() / GDF_sum_scaler )
return
>>> printLDF( forest.estimators_[:].predict( anExample ) )
这将向您显示单个树的预测,用于整个基于森林的预测的多数票计算。
这意味着,除此之外, RandomForest
原则上永远不会预测训练中存在的“访问”值范围“之外”的值(不能通过设计“推断”)。
嗯,特征工程是关键。如果您知道 RandomForest 是您案例的可行学习器,并且您觉得它观察到的预测能力很差,那么首先要归咎于特征选择。
检查学习者的内部状态——检查森林中的树木做了什么:
您可能会通过以下方式更深入地了解模型:
def prediction_up_dn_intervals( aPredictorMODEL, # >>> http://blog.datadive.net/prediction-intervals-for-random-forests/
X_, # aStateVECTOR: X_sampled
aPredictorOutputIDX = 0, # (4,2,2) -> singleQUAD ( LONG.TP/SL, SHORT.TP/SL ) <-- idxMAP( 'LONG', 'TP', 1 )
aRequiredPercentile = 95
):
err_dn = []
err_up = []
#-----------------------------------------------------------------------------------------------
if len( X_.shape ) == 1: # for a single X_example run
preds = []
for pred in aPredictorMODEL.estimators_:
preds.append( pred.predict( X_ )[0,aPredictorOutputIDX] ) # de-array-ification
err_dn.append( np.percentile( preds, ( 100 - aRequiredPercentile ) / 2. ) )
err_up.append( np.percentile( preds, 100 - ( 100 - aRequiredPercentile ) / 2. ) )
else:
#------------------------------------------------------------------------------------------
for x in xrange( len( X_ ) ): # for a multi X_example run
preds = []
for pred in aPredictorMODEL.estimators_:
preds.append( pred.predict( X_[x] )[0,aPredictorOutputIDX] ) # de-array-ification
err_dn.append( np.percentile( preds, ( 100 - aRequiredPercentile ) / 2. ) )
err_up.append( np.percentile( preds, 100 - ( 100 - aRequiredPercentile ) / 2. ) )
#-----------------------------------------------------------------------------------------------
return err_up, err_dn
#numba.jit( 'f8(<<OBJECT>>,f8[:,:],f8[:,:],i8,f8)' ) # <<OBJECT>> prevents JIT
def getPredictionsOnINTERVAL( aPredictorENGINE, # a MULTI-OBJECTIVE PREDICTOR -> a singleQUAD or a full 4-QUAD (16,0) <-(4,2,2)
X_,
y_GndTRUTH, # (4,2,2) -> (16,0) a MULTI-OBJECTIVE PREDICTOR
aPredictionIDX = 0, # (4,2,2) -> singleQUAD ( LONG.TP/SL, SHORT.TP/SL ) <-- idxMAP( 'LONG', 'TP', 1 )
percentile = 75
):
"""
|>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 75 ) 1.0 +0:01:29.375000
|>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 55 ) 0.9992532724237898 +0:03:59.922000
|>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 50 ) 0.997100939998243 +0:09:16.328000
|>>> getPredictionsOnINTERVAL( loc_PREDICTOR, X_sampled, y_sampled, idxMAP( "LONG", "TP", 1 ), 5 ) 0.31375735746288325 +0:01:16.422000
"""
correct_on_interval = 0 # correct = 0. ____________________- faster to keep asINTEGER ... +=1 and only finally make DIV on FLOAT(s) in RET
#ruth = y_ # Y[idx[trainsize:]]
err_up, err_dn = prediction_up_dn_intervals( aPredictorENGINE, # ( rf,
X_, # X[idx[trainsize:]],
aPredictionIDX, # idxMAP( "LONG", "TP", 1 ),
percentile # percentile = 90
) # )
#-------------------------------------------------------------------# for a single X_ run
if ( len( X_.shape ) == 1 ):
if ( err_dn[0] <= y_GndTRUTH[aPredictionIDX] <= err_up[0] ):
return 1.
else:
return 0.
#-------------------------------------------------------------------# for a multi X_ run
for i, val in enumerate( y_GndTRUTH[:,aPredictionIDX] ): # enumerate( truth )
if err_dn[i] <= val <= err_up[i]:
correct_on_interval += 1
#-------------------------------------------------------------------
return correct_on_interval / float( y_GndTRUTH.shape[0] ) # print correct / len( truth )
def mapPredictionsOnINTERVAL( aPredictorENGINE, #
X_,
y_GndTRUTH,
aPredictionIDX = 0,
aPercentilleSTEP = 5
):
for aPercentille in xrange( aPercentilleSTEP, 100, aPercentilleSTEP ):
Quotient = getPredictionsOnINTERVAL( aPredictorENGINE, X_, y_GndTRUTH, aPredictionIDX, aPercentille )
print "{0: > 3d}-percentil {1: > 6.3f} %".format( aPercentille, 100 * Quotient )
"""
5% 0.313757
10% 0.420847
15% 0.510191
20% 0.628481
25% 0.719758
30% 0.839058
35% 0.909646
40% 0.963454
45% 0.986603
50% 0.997101
55% 0.999253
60% 0.999912
65% 1.000000 >>> RET/JIT
70% 1.000000 xxxxxxxxxxxxxx
75% 1.000000 xxxxxxxxxxxxxx ???? .fit( X_, y_[:,8:12] ) # .fit() on HORIZON-T0+3???? ... y_GndTRUTH.shape[1] v/s .predict().shape[1]
"""
if ( Quotient == 1 ):
return
关于python - 为什么我的 Python RandomForestRegressor 不能准确预测训练集数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103301/
当然,您可以将剩余文件大小除以当前下载速度,但如果您的下载速度波动(而且它会波动),这不会产生很好的结果。有什么更好的算法可以产生更平滑的倒计时? 最佳答案 安exponential moving a
对于一个业余项目,我正在尝试对齐照片并创建 3D 图片。我基本上在一个钻机上有 2 个相机,我用来拍照。我会自动尝试以您获得 3D SBS 图像的方式对齐图像。 它们是高分辨率图像,这意味着需要处理大
当然,您可以将剩余的文件大小除以当前的下载速度,但如果您的下载速度波动(而且会波动),这不会产生很好的结果。什么是产生更平滑倒计时的更好算法? 最佳答案 安exponential moving ave
我有一个数据集,其中包含患有糖尿病和未患有糖尿病的人。我想使用这些数据训练一个模型来计算糖尿病状况未知的人的风险概率。我知道在培训中没有被诊断出糖尿病的人大多数都没有糖尿病,但很可能其中一些人可能患有
let parent = path[row-1] let child = path[row] let indexOfChild = matrix[parent.obje
我正在编写一些使用 Element.getBoundingClientRect 的代码(gBCR),加上内联样式更新,以执行计算。 这不适用于一般网站,我不关心或不感兴趣是否有“更好的 CSS 方式”
我有一个很大的 csv 文件,其中包含大量脏数据,我想通过消除所有不是绝对必要的值来稍微清理一下它。 Here是我正在谈论的文件。 它有以下组件: 网站,标题,开始日期,开始日期,雇主,地点,纬度,
有谁知道一个库,它为 Java 提供了一个错误不高于 1-2 毫秒的 Thread.sleep()? 我尝试了 sleep 、错误测量和 BusyWait 的混合,但在不同的 Windows 机器上我
UiApp有DateBox和 DateTimeFormat 对于那个类(class)。但是,不存在诸如 TimePicker 或 TimeBox 这样的东西,用户可以通过明确指定的方式(例如通过使用
因此,我使用 sklearn 的 svm.SVC 模块编写了一个程序来学习 mnist 数据集,出于某种原因,每当我计算其准确性为 100% 时。这似乎好得令人难以置信,这是预期的吗? from sk
我当前找到了 gpytorch ( https://github.com/cornellius-gp/gpytorch )。它似乎是将 GPR 集成到 pytorch 中的一个很棒的包。第一次测试也呈
我正在使用 QT Creator 5.9 创建一个简单的 Web 浏览器模型,我的 EditLine/Text Box 有问题: 1.如何在转到不同的网站/页面后自动更新显示的 URL 字符串。 2。
我在 Linux 上尝试 time -p 命令,我写了一些代码来浪费 CPU 周期: #include using namespace std; int main() { long int c;
亲爱的程序员/脚本编写者/工程师/其他人, 问题:我目前正在为 Android 3.2 平板电脑开发增强现实应用程序,但在获取准确的罗盘读数方面遇到一些问题。我需要确切地知道平板电脑所面向的 (z)
我最近一直在尝试了解 Apache Spark 作为 Scikit Learn 的替代品,但在我看来,即使在简单的情况下,Scikit 收敛到准确模型的速度也远远快于 Spark。例如,我使用以下脚本
如果不是,它的准确性如何? 我想在下载之前知道图片的大小。 最佳答案 HTTP Content-length header 是否格式错误?是的。 您是否应该相信它能公平地表示消息正文的大小?是的。 关
这是一个关于 ngram 线性回归的问题,使用 Tf-IDF(术语频率 - 逆文档频率)。为此,我使用 numpy 稀疏矩阵和 sklearn 进行线性回归。 使用一元语法时,我有 53 个案例和 6
对于某些给定的固定宽度,如何计算特定标签 (NSTextField) 中字符串的高度? 我用谷歌搜索了各种方法并尝试了 this method from Apple .它的工作原理,除了高度变成一行对
我是一名优秀的程序员,十分优秀!