predict

Error in Python script “Expected 2D array, got 1D array instead:”?

纵饮孤独 提交于 2019-11-28 23:32:34
问题 I'm following this tutorial to make this ML prediction: 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])) I'm using Python 3.6 and I get error "Expected 2D

Error when I try to predict class probabilities in R - caret

耗尽温柔 提交于 2019-11-28 08:06:49
I've build a model using caret. When the training was completed I got the following warning: Warning message: In train.default(x, y, weights = w, ...) : At least one of the class levels are not valid R variables names; This may cause errors if class probabilities are generated because the variables names will be converted to: X0, X1 The names of the variables are: 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

R predict glm fit on each column in data frame using column index number

痴心易碎 提交于 2019-11-28 08:04:11
问题 Trying to fit BLR model to each column in data frame, and then predict on new data pts. Have a lot of columns, so cannot identify the columns by name, only column number. Having reviewed the several examples of similar nature on this site, cannot figure out why this does not work. df <- data.frame(x1 = runif(1000, -10, 10), x2 = runif(1000, -2, 2), x3 = runif(1000, -5, 5), y = rbinom(1000, size = 1, prob = 0.40)) for (i in 1:length(df)-1) { fit <- glm (y ~ df[,i], data = df, family = binomial

What is the difference between lm(offense$R ~ offense$OBP) and lm(R ~ OBP)?

我怕爱的太早我们不能终老 提交于 2019-11-28 07:42:41
问题 I am trying to use R to create a linear model and use that to predict some values. The subject matter is baseball stats. If I do this: obp <- lm(offense$R ~ offense$OBP) predict(obp, newdata=data.frame(OBP=0.5), interval="predict") I get the error: Warning message: 'newdata' had 1 row but variables found have 20 rows. However, if I do this: attach(offense) obp <- lm(R ~ OBP) predict(obp, newdata=data.frame(OBP=0.5), interval="predict") It works as expected and I get one result. What is the

using predict with a list of lm() objects

北慕城南 提交于 2019-11-28 04:24:47
I have data which I regularly run regressions on. Each "chunk" of data gets fit a different regression. Each state, for example, might have a different function that explains the dependent value. This seems like a typical "split-apply-combine" type of problem so I'm using the plyr package. I can easily create a list of lm() objects which works well. However I can't quite wrap my head around how I use those objects later to predict values in a separate data.frame. Here's a totally contrived example illustrating what I'm trying to do: # setting up some fake data set.seed(1) funct <- function

r predict function returning too many values [closed]

一个人想着一个人 提交于 2019-11-28 01:39:30
I've read other postings regarding named variables and tried implementing the answers but still get too many values for my new data that I want to run my existing model on. Here is working example code: set.seed(123) mydata <- data.frame("y"=rnorm(100,mean=0, sd = 1),"x"=c(1:100)) mylm <- lm(y ~ x, data=mydata) # ok so mylm is a model on 100 points - lets look at it and the data par(mfrow=c(2,2)) plot(mylm) par(mfrow=c(1,1)) predvals <- predict(mylm, data=mydata) plot(mydata$x,mydata$y) lines(predvals) No surprises here - a straight line through generated points - both 100 observations in

R: numeric 'envir' arg not of length one in predict()

一世执手 提交于 2019-11-27 21:00:43
I'm trying to predict a value in R using the predict() function, by passing along variables into the model. I am getting the following error: Error in eval(predvars, data, env) : numeric 'envir' arg not of length one Here is my data frame , name 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

Error when using predict() on a randomForest object trained with caret's train() using formula

只愿长相守 提交于 2019-11-27 16:21:57
问题 Using R 3.2.0 with caret 6.0-41 and randomForest 4.6-10 on a 64-bit Linux machine. When trying to use the predict() method on a randomForest object trained with the train() function from the caret package using a formula, the function returns an error. When training via randomForest() and/or using x= and y= rather than a formula, it all runs smoothly. Here is a working example: library(randomForest) library(caret) data(imports85) imp85 <- imports85[, c("stroke", "price", "fuelType",

linear model with `lm`: how to get prediction variance of sum of predicted values

有些话、适合烂在心里 提交于 2019-11-27 06:20:39
问题 I'm summing the predicted values from a linear model with multiple predictors, as in the example below, and want to calculate the combined variance, standard error and possibly confidence intervals for this sum. lm.tree <- lm(Volume ~ poly(Girth,2), data = trees) Suppose I have a set of Girths : newdat <- list(Girth = c(10,12,14,16) for which I want to predict the total Volume : pr <- predict(lm.tree, newdat, se.fit = TRUE) total <- sum(pr$fit) # [1] 111.512 How can I obtain the variance for

R: numeric 'envir' arg not of length one in predict()

∥☆過路亽.° 提交于 2019-11-27 02:07:18
问题 I'm trying to predict a value in R using the predict() function, by passing along variables into the model. I am getting the following error: Error in eval(predvars, data, env) : numeric 'envir' arg not of length one Here is my data frame , name 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"