展平参数列表,其中参数可以是切片或数组

时间:2018-12-21 01:40:33

标签: go flatten

如果我有这样的功能:

te()

我正在尝试实现FlattenDeep:

library(mgcv)
set.seed(540)
df <- gamSim(2, n = 300, scale = 0.15)[[1]]
df$x <- scales::rescale(df$x, to = c(-180,180))
df$y <- scales::rescale(df$y, to = c(-90,90))
head(df)
# works fine using s()
m1 <- gam(z ~ s(x, y, bs = "gp", m = c(2, 5, 2), k = 30), data = df)
summary(m1)
Family: gaussian 
Link function: identity 

Formula:
z ~ s(x, y, bs = "gp", m = c(2, 5, 2), k = 30)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.46143    0.01581   29.19   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
         edf Ref.df     F p-value
s(x,y) 8.055  12.73 0.333   0.983

R-sq.(adj) =  0.0248   Deviance explained = 5.11%
GCV = 0.077292  Scale est. = 0.074959  n = 300

# Fails
# pass list as per example in ?te for multivariate marginal
mp <- list(c(2, 5, 2))
m2 <- gam(z ~ te(x, y, bs = "gp", m = mp, k = 30), data = df)
Error in gpE(knt, knt, object$p.order) : 
  incorrect arguments to GP smoother
In addition: Warning message:
In mgcv::te(x, y, bs = "gp", m = mp, k = 30) : m wrong length and ignored.

# try passing directly
m2 <- gam(z ~ te(x, y, bs = "gp", m = c(2,5,2), k = 30), data = df) # FAILS
Error in gpE(knt, knt, object$p.order) : 
  incorrect arguments to GP smoother
In addition: Warning message:
In mgcv::te(x, y, bs = "gp", m = c(2, 5, 2), k = 30) :
  m wrong length and ignored.

# don't pass m
m2 <- gam(z ~ te(x, y, bs = "gp", k = 10), data = df) # works
summary(m2)
Family: gaussian 
Link function: identity 

Formula:
z ~ te(x, y, bs = "gp", k = 10)

Parametric coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.46143    0.01299   35.52   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Approximate significance of smooth terms:
          edf Ref.df     F p-value    
te(x,y) 14.69  20.14 7.633  <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

R-sq.(adj) =  0.341   Deviance explained = 37.4%
GCV = 0.05341  Scale est. = 0.050618  n = 300

gam.check(m2) # k has been ignored!?
Method: GCV   Optimizer: magic
Smoothing parameter selection converged after 15 iterations.
The RMS GCV score gradient at convergence was 6.499192e-07 .
The Hessian was positive definite.
Model rank =  100 / 100 

Basis dimension (k) checking results. Low p-value (k-index<1) may
indicate that k is too low, especially if edf is close to k'.

          k'  edf k-index p-value
te(x,y) 99.0 14.7    1.08    0.94

但是我不知道如何一次将多个项目添加到列表中。我应该只遍历FlattenDeep的结果,还是可以分散结果并将其附加到列表中?

这可能有效:

func AcceptsAnything(v ...interface{}){
  args =: FlattenDeep(v);  // flatten any arrays or slices
}

但是我正在寻找一些不太详细的东西

1 个答案:

答案 0 :(得分:2)

以下是将任意切片和数组展平为[]接口{}的方法:

"

Run it on the Playground

如果函数必须处理具有任意元素类型的切片和数组类型,则应用程序必须使用反射API遍历切片或数组,以将值放入[]接口{}。

如果仅需要展平[] interface {},则不需要反射API:

let currentQuestion = 0;

let score = 1;

function renderQuestion() {

document.getElementById("quiz").innerHTML =
`
     <fieldset>
     <legend> ${questions[currentQuestion].question} </legend>
     <input  type="radio" name="option" 
     value="A">${questions[currentQuestion].options[0].option}<br>
     <input type="radio" name="option" 
     value="B">${questions[currentQuestion].options[1].option}<br>
     <input type="radio" name="option" 
     value="C">${questions[currentQuestion].options[2].option}<br>
     <input type="radio" name="option" 
     value="D">${questions[currentQuestion].options[3].option}<br>
     <button id="button" class="submitAnswer"> Submit answer </button>
     <div id="errorMessage">
     </div>
     <div id="feed">
      </div>
    </fieldset>

    `;

    document.getElementById("button").addEventListener("click", 
  function(event) {

    event.preventDefault();

    let hasAnswer = false;

    for (let el of document.querySelectorAll("input")) {

        if (el.checked) {

            hasAnswer = true;

            if (el.value === questions[currentQuestion].answer)


    {

                score++;
            }
        }
    }

    if (hasAnswer) {

        if (currentQuestion === questions.length - 1) {

            document.getElementById("quiz").innerHTML = `Your score is 
   ${score}`;
        }

        else {
            currentQuestion++;
            renderQuestion();
        }
    }
      else {
        document.getElementById("errorMessage").innerHTML = "Please 
select an answer!";

    }
  });
 }



renderQuestion();

Run it on the Playground