小编San*_*Dey的帖子

为什么在套索回归中计算 MSE 会给出不同的输出?

我正在尝试对来自 lasso2 包的前列腺癌数据运行不同的回归模型。当我使用 Lasso 时,我看到了两种不同的方法来计算均方误差。但他们确实给了我完全不同的结果,所以我想知道我是否做错了什么,或者这是否只是意味着一种方法比另一种更好?

# Needs the following R packages.
library(lasso2)
library(glmnet)

# Gets the prostate cancer dataset
data(Prostate)

# Defines the Mean Square Error function 
mse = function(x,y) { mean((x-y)^2)}

# 75% of the sample size.
smp_size = floor(0.75 * nrow(Prostate))

# Sets the seed to make the partition reproductible.
set.seed(907)
train_ind = sample(seq_len(nrow(Prostate)), size = smp_size)

# Training set
train = Prostate[train_ind, ]

# Test set
test = Prostate[-train_ind, ]

# Creates matrices for independent and dependent …
Run Code Online (Sandbox Code Playgroud)

r machine-learning lasso-regression glmnet mean-square-error

5
推荐指数
1
解决办法
7726
查看次数

如何解释 h2o.predict() 结果的概率 (p0, p1)

我想了解H2o R-package中h2o.predict()函数的值(结果)的含义。我意识到在某些情况下,当predict列是 时1p1列的值低于列p0。我对p0p1列的解释是指每个事件的概率,所以我预计predict=1的概率p1应该高于相反事件的概率 ( p0),但它并不总是发生,正如我在以下示例中所示:使用前列腺数据集

这是可执行示例:

library(h2o)
h2o.init(max_mem_size = "12g", nthreads = -1)
prostate.hex <- h2o.importFile("https://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv")
prostate.hex$CAPSULE  <- as.factor(prostate.hex$CAPSULE)
prostate.hex$RACE     <- as.factor(prostate.hex$RACE)
prostate.hex$DCAPS    <- as.factor(prostate.hex$DCAPS)
prostate.hex$DPROS    <- as.factor(prostate.hex$DPROS)

prostate.hex.split = h2o.splitFrame(data = prostate.hex,
  ratios = c(0.70, 0.20, 0.10), seed = 1234)
train.hex     <- prostate.hex.split[[1]]
validate.hex  <- prostate.hex.split[[2]]
test.hex      <- prostate.hex.split[[3]]

fit <- h2o.glm(y = "CAPSULE", x = …
Run Code Online (Sandbox Code Playgroud)

r machine-learning glm deep-learning h2o

5
推荐指数
2
解决办法
3156
查看次数

解决n-queen拼图

我刚刚解决了python中的nqueen问题.该解决方案输出用于在nXn棋盘上放置n个皇后的解决方案的总数,但是在n = 15时尝试它需要一个多小时才能得到答案.任何人都可以看看代码,并给我提示加快这个程序......一个新手python程序员.

#!/usr/bin/env python2.7

##############################################################################
# a script to solve the n queen problem in which n queens are to be placed on
# an nxn chess board in a way that none of the n queens is in check by any other
#queen using backtracking'''
##############################################################################
import sys
import time
import array

solution_count = 0

def queen(current_row, num_row, solution_list):
    if current_row == num_row:
        global solution_count 
        solution_count = solution_count + 1
    else:
        current_row += 1
        next_moves = gen_nextpos(current_row, solution_list, …
Run Code Online (Sandbox Code Playgroud)

python artificial-intelligence r n-queens

4
推荐指数
1
解决办法
1万
查看次数

如何在 R 中使用曲线()对图形进行着色

我正在绘制标准正态分布。

curve(dnorm(x), from=-4, to=4, 
  main = "The Standard Normal Distibution", 
  ylab = "Probability Density",
  xlab = "X")
Run Code Online (Sandbox Code Playgroud)

出于教学原因,我想将低于我选择的某个分位数的区域涂上阴影。我怎样才能做到这一点?

plot r graph area ggplot2

4
推荐指数
1
解决办法
1780
查看次数

使用`glmnet`的岭回归给出的系数与我通过"教科书定义"计算的系数不同?

我使用glmnet R包运行Ridge回归.我注意到我从glmnet::glmnet函数中获得的系数与通过定义计算系数得到的系数不同(使用相同的lambda值).有人可以解释一下为什么吗?

数据(两者:响应Y和设计矩阵X)都是按比例缩放的.

library(MASS)
library(glmnet)

# Data dimensions
p.tmp <- 100
n.tmp <- 100

# Data objects
set.seed(1)
X <- scale(mvrnorm(n.tmp, mu = rep(0, p.tmp), Sigma = diag(p.tmp)))
beta <- rep(0, p.tmp)
beta[sample(1:p.tmp, 10, replace = FALSE)] <- 10
Y.true <- X %*% beta
Y <- scale(Y.true + matrix(rnorm(n.tmp))) # Y.true + Gaussian noise

# Run glmnet 
ridge.fit.cv <- cv.glmnet(X, Y, alpha = 0)
ridge.fit.lambda <- ridge.fit.cv$lambda.1se

# Extract coefficient values for …
Run Code Online (Sandbox Code Playgroud)

regression r machine-learning linear-regression glmnet

4
推荐指数
1
解决办法
1793
查看次数

R 中的 lm 与 Python 中的 statsmodel OLS 的不同结果

我是 Python 新手,并且是 R 用户。当我在 R 中构建它与在 iPython 中执行相同的事情时,我从一个简单的回归模型中得到非常不同的结果。

R 平方、P 值、系数的重要性 - 没有匹配项。我是读错了输出还是犯了其他一些基本错误?

以下是我的代码和结果:

代码

str(df_nv)
Classes 'tbl_df', 'tbl' and 'data.frame':   81 obs. of  2 variables:
 $ Dependent Variabls       : num  733 627 405 353 434 556 381 558 612 901 ...
 $ Independent Variable: num  0.193 0.167 0.169 0.14 0.145 ...


summary(lm(`Dependent Variable` ~ `Independent Variable`, data = df_nv))

Call:
    lm(formula = `Dependent Variable` ~ `Independent Variable`, data = df_nv)


Residuals:
    Min      1Q  Median      3Q     Max …
Run Code Online (Sandbox Code Playgroud)

python r machine-learning lm statsmodels

4
推荐指数
1
解决办法
3072
查看次数

R: Plot multiple different coloured ROC curves using ROCR

The following code was taken from @adibender answer to "Multiple ROC curves in one plot ROCR". The code is partly from ?plot.performance.

library(ROCR)
data(ROCR.simple)
preds <- cbind(p1 = ROCR.simple$predictions, 
            p2 = abs(ROCR.simple$predictions + 
            rnorm(length(ROCR.simple$predictions), 0, 0.1)))

pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels, 
            nrow = length(ROCR.simple$labels), ncol = 2) )

perf.mat <- performance(pred.mat, "tpr", "fpr")
plot(perf.mat)
Run Code Online (Sandbox Code Playgroud)

I want to illustrate several ROC curves in a single plot, like the code above, using the r package ROCR. However, i would want the …

plot r machine-learning data-mining roc

4
推荐指数
1
解决办法
6074
查看次数

计算R中的值的数量

我有以下数据集:

    ClaimType ClaimDay ClaimCost   dates    month      day
1         1        1     10811 1970-01-01     1 1970-01-01
2         1        1     18078 1970-01-01     1 1970-01-01
3         1        2     44579 1970-01-01     1 1970-01-02
4         1        3     23710 1970-01-01     1 1970-01-03
5         1        4     29580 1970-01-01     1 1970-01-04
6         1        4     36208 1970-01-01     1 1970-01-04
Run Code Online (Sandbox Code Playgroud)

我想创建一个新的数据集,其中包含"声明日"和"日期"列.索赔日应按每个值计算.所以例如,因为我们有两个,一个是两个,一个是三个,然后是两个四,我希望​​新的数据集如下:

ClaimDay   day
2         1970-01-01
1         1970-01-02
1         1970-01-03
2         1970-01-04
Run Code Online (Sandbox Code Playgroud)

如您所见,Claimday和day是相关的.

我试过了

mydata <- aggregate(ClaimDay~Day,FUN=sum,data=mydata)$ClaimDay
Run Code Online (Sandbox Code Playgroud)

但问题是,在聚合时它会计算摘要.

任何人都可以帮我解决我的问题吗?

aggregate r dataframe

4
推荐指数
2
解决办法
2万
查看次数

使用 R 进行 LU 分解

我正在尝试使用 R 运行 LU 分解。这是可重现的代码。我不明白为什么我的排列矩阵与解决方案不同。L 和 U 矩阵是正确的。但对于置换矩阵,第 1 行和第 2 行以及第 3 行和第 4 行互换。因此,我没有得到线性方程组的正确解。将不胜感激您的帮助。

install.packages("Matrix")
library(Matrix)
(A <- matrix(c(4, 3, -2, 5, 2, -4, 6, 1, -1, 2, -5, 6, 3, 5, -2, -3), nrow = 4))
(B <- matrix(c(16.9, -14, 25, 9.4), nrow = 4))

luA <- lu(A)
elu <- expand(luA)
(L <- elu$L)
(U <- elu$U)
(P <- elu$P)

(Y <- solve(L) %*% P %*% B)
(X <- solve(U) %*% Y)
Run Code Online (Sandbox Code Playgroud)

math r matrix linear-algebra numerical-methods

4
推荐指数
1
解决办法
6082
查看次数

如何使用pytesseract获得每一行的信心

我已经成功设置Tesseract并且可以将图像转换为文本...

text = pytesseract.image_to_string(Image.open(image))
Run Code Online (Sandbox Code Playgroud)

但是,我需要获得每一行的置信度值。我找不到使用pytesseract做到这一点的方法。有人知道怎么做吗?

我知道这可以使用PyTessBaseAPI,但我不能使用它,我花了几个小时试图设置它但没有运气,所以我需要一种使用pytesseract来做到这一点的方法。

ocr tesseract image-processing python-3.x python-tesseract

4
推荐指数
2
解决办法
6631
查看次数