我正在使用插入符合gbm模型.当我打电话时trainedGBM$finalModel$fit,我得到的输出看起来是正确的.
但是当我打电话时predict(trainedGBM$finalModel, origData, type="response"),我会得到非常不同的结果,predict(trainedGBM$finalModel, type="response")即使附加了origData,结果也会产生不同的结果.根据我的思维方式,这些调用应该产生相同的输出.有人可以帮我识别问题吗?
library(caret)
library(gbm)
attach(origData)
gbmGrid <- expand.grid(.n.trees = c(2000),
.interaction.depth = c(14:20),
.shrinkage = c(0.005))
trainedGBM <- train(y ~ ., method = "gbm", distribution = "gaussian",
data = origData, tuneGrid = gbmGrid,
trControl = trainControl(method = "repeatedcv", number = 10,
repeats = 3, verboseIter = FALSE,
returnResamp = "all"))
ntrees <- gbm.perf(trainedGBM$finalModel, method="OOB")
data.frame(y,
finalModelFit = trainedGBM$finalModel$fit,
predictDataSpec = predict(trainedGBM$finalModel, origData, type="response", n.trees=ntrees),
predictNoDataSpec = predict(trainedGBM$finalModel, type="response", n.trees=ntrees))
Run Code Online (Sandbox Code Playgroud)
上面的代码产生以下部分结果:
y …Run Code Online (Sandbox Code Playgroud) 我从我的数据集中创建了一对模型(800行,2列):
#1
m1=lm(A~A1, fish)
#2
mmars1=polymars(fish$A, fish)
#3
bestm1=loess.smooth(fish$A1, fish$A, span=best)
#4
bestm2=smooth.spline(fish$A1,fish$A, spar=best)
Run Code Online (Sandbox Code Playgroud)
然后我试图使用新的x预测y:
#Predict
#1
predict(m1, data.frame(xl=c(100000)))
#2
predict(mmars1, data.frame(xm=c(100000)))
#3
predict(bestm1, data.frame(xb1=c(100000)))
#4
predict(bestm2, data.frame(xb2=c(100000)))
Run Code Online (Sandbox Code Playgroud)
#4工作正常,但我对其他3有问题.#1和#2返回800值而不是1.#3给我这个错误.
UseMethod中的错误("预测"):没有适用于"预测"的方法应用于类"列表"的对象
我在#1和#2做错了什么?有没有其他方法可以使用该predict()方法loess.smooth?
当我运行预测函数时,出现以下错误:
Error in predict.randomForest(fit, newdata = na.roughfix(csvTest[, -c(1:2, :
'prob' or 'vote' not meaningful for regression code here
Run Code Online (Sandbox Code Playgroud)
这是代码:
fit <- foreach (ntree = rep (round(number_trees/nc), nc),
.combine = combine, .packages = 'randomForest') %dopar% randomForest(y=csv$mal,x=na.roughfix (csv[, c(1:2,4:5,8:13,17,19:20,22:29,ncol(csv))]), ntree = number_trees)
prob <- predict (fit, newdata = na.roughfix (csvTest[, -c(1:2,4:5,8:13,17,19:20,22:29,ncol(csvTest))]), type = 'prob')[, 2]
Run Code Online (Sandbox Code Playgroud)
谁能帮助我了解问题所在?谢谢
我知道可以得到如下预测值:
reg y x1 x2 x3
predict pred_values
Run Code Online (Sandbox Code Playgroud)
假设我进行回归并存储值:
reg y x1 x2
matrix stored_b = e(b)
Run Code Online (Sandbox Code Playgroud)
然后,我进行另一次回归(无论如何)。
是否可以使用predictusing命令stored_b代替当前命令e(b)?
(当然,我可以通过基于手动计算预测值来生成预测值stored_b,但是如果系数很多,这可能会很乏味。)
我建立了一个线性回归i R.现在我想存储模型并用它来每周评估一次新的数据集.
谁可以帮我怎么样?
如何保存模型以及如何导入模型并在新数据集上使用它.
我正在使用 Stata 运行固定效应模型,然后执行样本外预测。但似乎
xtreg
Run Code Online (Sandbox Code Playgroud)
其次是
predict yhat, xbu
Run Code Online (Sandbox Code Playgroud)
不预测样本外以及固定效应。有没有办法通过包含固定效应将 xtreg 用于样本外?插图:
webuse nlswork
xtset idcode year
regress ln_wage age if year <= 80
predict temp1
xtreg ln_wage age if year <= 80, fe
predict temp2, xbu
Run Code Online (Sandbox Code Playgroud)
就我而言,我需要预测 year = 81 的值。并且 temp2 为空的年数 > 80。阅读 xtreg 和 areg 的手册,似乎不可能进行样本外预测,尤其是对于 xbu——其中包括固定效应预测。可以理解,如果我使用年份固定效果没有意义,但如果我只是使用idcode应该可以吗?任何建议将不胜感激。或者关于如何获得解决方案的任何建议?
它似乎只针对 <= 2000 年的所有年份生成。也就是说,我只能生成样本中的预测。
我想了解predict.lm函数在 R中的工作原理。因此,我在控制台中键入函数的名称以查看代码,将代码复制到新脚本并调用新函数的 pred:
pred <- function (object, newdata, se.fit = FALSE, scale = NULL, df = Inf,
interval = c("none", "confidence", "prediction"), level = 0.95,
type = c("response", "terms"), terms = NULL, na.action = na.pass,
pred.var = res.var/weights, weights = 1, ...)
{
<here goes the body of the predict.lm function which
I do not copy to the post so it remains readable>
}
Run Code Online (Sandbox Code Playgroud)
然后我拟合一个模型来检查一切是否正常,并使用作为函数pred副本的新函数进行预测predict.lm:
fit <- lm(Sepal.Length ~ Species, data = iris)
new_obs …Run Code Online (Sandbox Code Playgroud) 我正在重新创建 DnCNN,即高斯降噪器,它使用一系列卷积层进行图像到图像的预测。它训练得非常好,但是当我尝试执行列表(model.predict(..))时,出现错误:
标签不能为 none
我实际上将我的 EstimatorSpec 的所有规范参数明确地放在那里,因为它们是根据调用 Estimator 的方法(train/eval/predict)懒惰地评估的。
def DnCNN_model_fn (features, labels, mode):
# some convolutinons here
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=conv_last + input_layer,
loss=tf.losses.mean_squared_error(
labels=labels,
predictions=conv_last + input_layer),
train_op=tf.train.AdamOptimizer(learning_rate=0.001, epsilon=1e-08).minimize(
loss=tf.losses.mean_squared_error(
labels=labels,
predictions=conv_last + input_layer),
global_step=tf.train.get_global_step()),
eval_metric_ops={
"accuracy": tf.metrics.mean_absolute_error(
labels=labels,
predictions=conv_last + input_layer)}
)
Run Code Online (Sandbox Code Playgroud)
将其放入估计器中:
d = datetime.datetime.now()
DnCNN = tf.estimator.Estimator(
model_fn=DnCNN_model_fn,
model_dir=root + 'model/' +
"DnCNN_{}_{}_{}_{}".format(d.month, d.day, d.hour, d.minute),
config=tf.estimator.RunConfig(save_summary_steps=2,
log_step_count_steps=10)
)
Run Code Online (Sandbox Code Playgroud)
训练模型后,我进行如下预测:
test_input_fn = tf.estimator.inputs.numpy_input_fn(
x= test_data[0:2,:,:,:],
y= None,
batch_size=1,
num_epochs=1,
shuffle=False)
predicted = DnCNN.predict(input_fn=test_input_fn)
list(predicted) # …Run Code Online (Sandbox Code Playgroud) 我是R的新手,当我要使用glm()来估计一个逻辑模型时,它不是预测响应,而是为我的预测函数的每个输入调用预测函数(如1)提供一个非实际输出.
Code:
ex2data1R <- read.csv("/media/ex2data1R.txt")
x <-ex2data1R$x
y <-ex2data1R$y
z <-ex2data1R$z
logisticmodel <- glm(z~x+y,family=binomial(link = "logit"),data=ex2data1R)
newdata = data.frame(x=c(10),y=(10))
predict(logisticmodel, newdata, type="response")
Output:
> predict(logisticmodel, newdata, type="response")
1
1.181875e-11
Data(ex2data1R.txt) :
"x","y","z"
34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
60.18259938620976,86.30855209546826,1
79.0327360507101,75.3443764369103,1
45.08327747668339,56.3163717815305,0
61.10666453684766,96.51142588489624,1
75.02474556738889,46.55401354116538,1
76.09878670226257,87.42056971926803,1
84.43281996120035,43.53339331072109,1
95.86155507093572,38.22527805795094,0
75.01365838958247,30.60326323428011,0
82.30705337399482,76.48196330235604,1
69.36458875970939,97.71869196188608,1
39.53833914367223,76.03681085115882,0
53.9710521485623,89.20735013750205,1
69.07014406283025,52.74046973016765,1
67.94685547711617,46.67857410673128,0
70.66150955499435,92.92713789364831,1
76.97878372747498,47.57596364975532,1
67.37202754570876,42.83843832029179,0
89.67677575072079,65.79936592745237,1
50.534788289883,48.85581152764205,0
34.21206097786789,44.20952859866288,0
77.9240914545704,68.9723599933059,1
62.27101367004632,69.95445795447587,1
80.1901807509566,44.82162893218353,1
93.114388797442,38.80067033713209,0
61.83020602312595,50.25610789244621,0
38.78580379679423,64.99568095539578,0
61.379289447425,72.80788731317097,1
85.40451939411645,57.05198397627122,1
52.10797973193984,63.12762376881715,0
52.04540476831827,69.43286012045222,1
40.23689373545111,71.16774802184875,0
54.63510555424817,52.21388588061123,0
33.91550010906887,98.86943574220611,0
64.17698887494485,80.90806058670817,1
74.78925295941542,41.57341522824434,0
34.1836400264419,75.2377203360134,0
83.90239366249155,56.30804621605327,1
51.54772026906181,46.85629026349976,0
94.44336776917852,65.56892160559052,1
82.36875375713919,40.61825515970618,0
51.04775177128865,45.82270145776001,0
62.22267576120188,52.06099194836679,0
77.19303492601364,70.45820000180959,1
97.77159928000232,86.7278223300282,1 …Run Code Online (Sandbox Code Playgroud) 对于以下数据:
require(dplyr)
require(ggplot2)
ds <- read.table(header = TRUE, text ="
obs id year attend
1 47 2000 1
2 47 2001 3
3 47 2002 5
4 47 2003 8
5 47 2004 6
6 47 2005 4
7 47 2006 2
8 47 2007 1
9 47 2008 2
10 47 2009 3
11 47 2010 4
12 47 2011 5
")
print(ds)
Run Code Online (Sandbox Code Playgroud)
我想计算线性模型的预测值
linear<- predict(lm(attend ~ year, ds))
quadratic<- predict(lm(attend ~ year + I(year^2),ds))
cubic<- predict(lm(attend ~ year …Run Code Online (Sandbox Code Playgroud) 这是我的数据(示例):
EXAMPLE<-data.frame(
X=c(99.6, 98.02, 96.43, 94.44, 92.06, 90.08, 87.3, 84.92, 82.14,
79.76, 76.98, 74.21, 71.03, 67.86, 65.08, 62.3, 59.92, 56.35,
52.38, 45.63, 41.67, 35.71, 30.95, 24.6, 17.86, 98.44, 96.48,
94.14, 92.19, 89.84, 87.5, 84.38, 82.42, 78.52, 76.17, 73.83,
70.7, 65.63, 62.89, 60.16, 58.2, 54.69, 52.73, 49.61, 46.09,
42.58, 40.23, 36.72, 32.81),
Y=c(3.62, 9.78, 15.22, 19.93, 24.64, 30.43, 35.14, 39.49, 44.93,
49.64, 52.9, 57.97, 62.68, 66.3, 70.29, 73.55, 76.09, 78.62,
80.8, 82.61, 84.42, 87.32, 91.67, 96.01, 99.28, 3.85, 8.55, 11.97,
17.52, …Run Code Online (Sandbox Code Playgroud) 在 Rstudio 的小数据集上计算 VIF 时出现以下错误。有人可以帮忙吗?如果需要,我可以提供有关数据集的更多信息。
“as.vector(y) 中的错误 - 二元运算符的 mean(y) 非数字参数”。
数据集:80 个观察。和 15 个变量(所有变量都是数字)
步骤如下:
# 1. Determine correlation
library(corrplot)
cor.data <- cor(train)
corrplot(cor.data, method = 'color')
cor.data
# 2. Build Model
model2 <- lm(Volume~., train)
summary(model2)
# 3. Calculate VIF
library(VIF)
vif(model2)
Run Code Online (Sandbox Code Playgroud)
这是一个包含 20 个 obs 的示例数据集。
train <- structure(list(Price = c(949, 2249.99, 399, 409.99, 1079.99,
114.22, 379.99, 65.29, 119.99, 16.99, 6.55, 15, 52.5, 21.08,
18.98, 3.6, 3.6, 174.99, 9.99, 670), X.5.Star.Reviews. = c(3,
2, 3, 49, 58, …Run Code Online (Sandbox Code Playgroud) 我正在尝试检查我的 .onnx 模型是否正确,并且需要运行推理以验证相同的输出。
我知道我们可以使用 Python 中的 coremltools 在 .mlmodel 上运行验证 - 基本上加载模型和输入并获得预测。我正在尝试为 .onnx 模型做类似的事情。
我找到了 MXNet 框架,但我似乎无法理解如何导入模型 - 我只有 .onnx 文件,而 MXNet 除了 onnx 模型之外还需要一些额外的输入。
在 Python 中还有其他简单的方法可以做到这一点吗?我猜这是一个常见问题,但似乎找不到任何相关的库/框架来像 .mlmodel 的 coremltools 那样轻松地做到这一点。
我不希望将 .onnx 转换为另一种类型的模型(比如 PyTorch),因为我想按原样检查 .onnx 模型,而不用担心转换是否正确。只需要一种方法来加载模型和输入,运行推理并打印输出。
这是我第一次遇到这些格式,所以任何帮助或见解将不胜感激。
谢谢!