我今天早些时候发布了关于使用该predict
功能时遇到的错误.我能够纠正错误,并认为我走在了正确的道路上.
我有一些观察(实际),我有一些我想要推断或预测的数据点.我曾经lm
创建过一个模型,然后我尝试使用predict
将作为预测输入的实际值.
这段代码都是从我之前的帖子中重复出来的,但这里是:
df <- read.table(text = '
Quarter Coupon Total
1 "Dec 06" 25027.072 132450574
2 "Dec 07" 76386.820 194154767
3 "Dec 08" 79622.147 221571135
4 "Dec 09" 74114.416 205880072
5 "Dec 10" 70993.058 188666980
6 "Jun 06" 12048.162 139137919
7 "Jun 07" 46889.369 165276325
8 "Jun 08" 84732.537 207074374
9 "Jun 09" 83240.084 221945162
10 "Jun 10" 81970.143 236954249
11 "Mar 06" 3451.248 116811392
12 "Mar 07" 34201.197 155190418
13 …
Run Code Online (Sandbox Code Playgroud) 我正在按照本教程进行ML预测:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm
x = [1, 5, 1.5, 8, 1, 9]
y = [2, 8, 1.8, 8, 0.6, 11]
plt.scatter(x,y)
plt.show()
X = np.array([[1,2],
[5,8],
[1.5,1.8],
[8,8],
[1,0.6],
[9,11]])
y = [0,1,0,1,0,1]
X.reshape(1, -1)
clf = svm.SVC(kernel='linear', C = 1.0)
clf.fit(X,y)
print(clf.predict([0.58,0.76]))
Run Code Online (Sandbox Code Playgroud)
我使用Python 3.6,我得到错误"预期的2D阵列,而不是1D阵列:"我认为该脚本适用于旧版本,但我不知道如何将其转换为3.6版本.
已经尝试过:
X.reshape(1, -1)
Run Code Online (Sandbox Code Playgroud) 我试图通过将变量传递到模型中来预测R
使用predict()
函数的值.
我收到以下错误:
Error in eval(predvars, data, env) :
numeric 'envir' arg not of length one
Run Code Online (Sandbox Code Playgroud)
这是我的data frame
名字df:
df <- read.table(text = '
Quarter Coupon Total
1 "Dec 06" 25027.072 132450574
2 "Dec 07" 76386.820 194154767
3 "Dec 08" 79622.147 221571135
4 "Dec 09" 74114.416 205880072
5 "Dec 10" 70993.058 188666980
6 "Jun 06" 12048.162 139137919
7 "Jun 07" 46889.369 165276325
8 "Jun 08" 84732.537 207074374
9 "Jun 09" 83240.084 221945162
10 "Jun 10" 81970.143 …
Run Code Online (Sandbox Code Playgroud) 我使用DBSCAN使用Scikit-Learn(Python 2.7)聚集一些数据:
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(random_state=0)
dbscan.fit(X)
Run Code Online (Sandbox Code Playgroud)
但是,我发现没有内置函数(除了"fit_predict"之外)可以将新数据点Y分配给原始数据中标识的簇X.K-means方法有一个"预测"功能,但我希望能够对DBSCAN做同样的事情.像这样的东西:
dbscan.predict(X, Y)
Run Code Online (Sandbox Code Playgroud)
因此密度可以从X推断,但返回值(集群分配/标签)仅适用于Y.从我所知道的,这个功能在R中可用,所以我假设它在某种程度上也可用于Python.我似乎无法找到任何相关的文档.
此外,我已经尝试搜索为什么DBSCAN不能用于标记新数据的原因,但我没有找到任何理由.
我用插入符号构建了一个模型.培训结束后,我收到以下警告:
警告消息:在train.default(x,y,weights = w,...)中:至少有一个类级别不是有效的R变量名称; 如果生成类概率,这可能会导致错误,因为变量名称将转换为:X0,X1
变量的名称是:
str(train)
'data.frame': 7395 obs. of 30 variables:
$ alchemy_category : Factor w/ 13 levels "arts_entertainment",..: 2 8 6 6 11 6 1 6 3 8 ...
$ alchemy_category_score : num 3737 2052 4801 3816 3179 ...
$ avglinksize : num 2.06 3.68 2.38 1.54 2.68 ...
$ commonlinkratio_1 : num 0.676 0.508 0.562 0.4 0.5 ...
$ commonlinkratio_2 : num 0.206 0.289 0.322 0.1 0.222 ...
$ commonlinkratio_3 : num 0.0471 0.2139 0.1202 0.0167 …
Run Code Online (Sandbox Code Playgroud) 好的,这一切都发生在一个漂亮而简单的2D世界...... :)
假设我在位置Apos处有一个静态物体A,在Bpos处有一个带有bVelocity的线性移动物体B,以及一个带有速度Avelocity的弹药轮......
考虑到B的线速度和A弹药的速度,我如何找出A必须射击的角度,击中B?
现在目标是在物体的当前位置,这意味着当我的射弹到达那里时,该单位已经转移到更安全的位置:)
有没有人有一个很好的清洁方式来获取模型的predict
行为felm
?
library(lfe)
model1 <- lm(data = iris, Sepal.Length ~ Sepal.Width + Species)
predict(model1, newdata = data.frame(Sepal.Width = 3, Species = "virginica"))
# Works
model2 <- felm(data = iris, Sepal.Length ~ Sepal.Width | Species)
predict(model2, newdata = data.frame(Sepal.Width = 3, Species = "virginica"))
# Does not work
Run Code Online (Sandbox Code Playgroud) TL; DR:
我可以在原始 randomForest
调用中 标记一些内容,以避免重新运行predict
函数以获得预测的分类概率,而不仅仅是可能的类别吗?
细节:
我正在使用randomForest包.
我有一个类似的模型:
model <- randomForest(x=out.data[train.rows, feature.cols],
y=out.data[train.rows, response.col],
xtest=out.data[test.rows, feature.cols],
ytest=out.data[test.rows, response.col],
importance= TRUE)
Run Code Online (Sandbox Code Playgroud)
out.data
数据框在哪里,具有feature.cols
数字和分类特征的混合,response.col
而是一个TRUE
/ FALSE
二进制变量,我被强制插入,factor
以便randomForest
模型将其正确地视为分类.
一切运行良好,变量model
正确返回给我.但是,我似乎无法找到传递给randomForest
函数的标志或参数,因此model
返回给我的概率为TRUE
或FALSE
.相反,我得到的只是预测值.也就是说,如果我看一下model$predicted
,我会看到类似的东西:
FALSE
FALSE
TRUE
TRUE
FALSE
.
.
.
Run Code Online (Sandbox Code Playgroud)
相反,我希望看到类似的东西:
FALSE TRUE
1 0.84 0.16
2 0.66 0.34
3 0.11 0.89
4 0.17 0.83
5 0.92 …
Run Code Online (Sandbox Code Playgroud) 我有定期运行回归的数据.每个"数据块"的数据都适合不同的回归.例如,每个州可能具有解释从属值的不同功能.这似乎是典型的"拆分 - 应用 - 组合"类型的问题,因此我使用的是plyr包.我可以轻松创建一个lm()
运行良好的对象列表.但是,我不能完全理解我以后如何使用这些对象来预测单独data.frame中的值.
这是一个完全人为的例子,说明了我正在尝试做的事情:
# setting up some fake data
set.seed(1)
funct <- function(myState, myYear){
rnorm(1, 100, 500) + myState + (100 * myYear)
}
state <- 50:60
year <- 10:40
myData <- expand.grid( year, state)
names(myData) <- c("year","state")
myData$value <- apply(myData, 1, function(x) funct(x[2], x[1]))
## ok, done with the fake data generation.
require(plyr)
modelList <- dlply(myData, "state", function(x) lm(value ~ year, data=x))
## if you want to see the summaries of the lm() do …
Run Code Online (Sandbox Code Playgroud) 我在没有找到解决方案的情况下进行了广泛的研究.我已经清理了我的数据集如下:
library("raster")
impute.mean <- function(x) replace(x, is.na(x) | is.nan(x) | is.infinite(x) ,
mean(x, na.rm = TRUE))
losses <- apply(losses, 2, impute.mean)
colSums(is.na(losses))
isinf <- function(x) (NA <- is.infinite(x))
infout <- apply(losses, 2, is.infinite)
colSums(infout)
isnan <- function(x) (NA <- is.nan(x))
nanout <- apply(losses, 2, is.nan)
colSums(nanout)
Run Code Online (Sandbox Code Playgroud)
问题出现了运行预测算法:
options(warn=2)
p <- predict(default.rf, losses, type="prob", inf.rm = TRUE, na.rm=TRUE, nan.rm=TRUE)
Run Code Online (Sandbox Code Playgroud)
所有的研究都表明它应该是数据中的NA或Inf或NaN,但我没有发现任何数据.我正在制作数据和randomForest摘要可用于[删除] Traceback的调查并没有显示太多(对我来说):
4: .C("classForest", mdim = as.integer(mdim), ntest = as.integer(ntest),
nclass = as.integer(object$forest$nclass), maxcat = as.integer(maxcat),
nrnodes = as.integer(nrnodes), jbt = as.integer(ntree), …
Run Code Online (Sandbox Code Playgroud) predict ×10
r ×7
lm ×3
2d ×1
data-mining ×1
dbscan ×1
intersection ×1
lfe ×1
plyr ×1
python ×1
python-3.x ×1
r-caret ×1
scikit-learn ×1