该表达式的结果被隐式忽略.考虑使用“忽略",例如'expr |>忽略"或“例如'让结果= expr' [英] The result of this expression is implicitly ignored. Consider using 'ignore' e.g. 'expr |> ignore', or ' e.g. 'let result = expr'

查看:84
本文介绍了该表达式的结果被隐式忽略.考虑使用“忽略",例如'expr |>忽略"或“例如'让结果= expr'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为

I'm trying to build F# equivalent code for the Python code appearing here, I've the below code:

let tripleExponentialSmoothing series slen alpha beta gamma nPreds =
    let result : float list = []
    let mutable smooth = 0.
    let mutable trend = 0.
    let seasonals = initialSeasonalComponents series 12
    for i in 0..(series.Length+nPreds-1) do
    match i with
    | 0 ->     // initial values        
        smooth <- series |> Array.head |> float
        trend <- initialTrend series slen
        result |> List.append [series |> Array.head |> float] |> ignore
    | i when i >= series.Length -> // we are forecasting
        let m = i - series.Length + 1
        result |> List.append [(smooth + float m * trend) + seasonals.Item(i%slen)] |> ignore
    | _ -> 
        let v = series |> Array.head  |> float
        let lastSmooth = smooth
        smooth <- alpha*(v-seasonals.Item(i%slen)) + (1.-alpha)*(smooth+trend)
        trend <- beta * (smooth-lastSmooth) + (1.-beta)*trend
        seasonals.Item(i%slen) <- gamma*(v-smooth) + (1.-gamma)*seasonals.Item(i%slen)
        result |> List.append [smooth + trend + seasonals.Item(i%slen)] |> ignore
    result

我收到以下错误:

警告FS0020:该表达式的结果被隐式忽略. 考虑使用'ignore'来显式丢弃此值,例如'expr |>忽略"或让"将结果绑定到名称,例如'让结果= expr'.

warning FS0020: The result of this expression is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.

我试图将上面的代码编写为下面的Python代码的转换:

I tried to write the above as conversion of the below Python code:

def triple_exponential_smoothing(series, slen, alpha, beta, gamma, n_preds):
    result = []
    seasonals = initial_seasonal_components(series, slen)
    for i in range(len(series)+n_preds):
        if i == 0: # initial values
            smooth = series[0]
            trend = initial_trend(series, slen)
            result.append(series[0])
            continue
        if i >= len(series): # we are forecasting
            m = i - len(series) + 1
            result.append((smooth + m*trend) + seasonals[i%slen])
        else:
            val = series[i]
            last_smooth, smooth = smooth, alpha*(val-seasonals[i%slen]) + (1-alpha)*(smooth+trend)
            trend = beta * (smooth-last_smooth) + (1-beta)*trend
            seasonals[i%slen] = gamma*(val-smooth) + (1-gamma)*seasonals[i%slen]
            result.append(smooth+trend+seasonals[i%slen])
    return result

我做错了什么,什么是与提到的Python等效的正确代码.

What the wrong things I did, and what is the correct code equivalent to the mentioned Python's one.

推荐答案

您正在运行for作为副作用.

You are running the for as a side effect.

也许您想要一个seq表达式,我认为在Python中您有生成器,但是在F#中请记住,所有内容都是一个表达式.

Maybe you want to a seq expression, I think in Python you have generators but here in F# remember that everything is an expression.

let tripleExponentialSmoothing series slen alpha beta gamma nPreds =
    let mutable smooth = 0.
    let mutable trend = 0.
    let seasonals = initialSeasonalComponents series 12 |> Dictionary 
    seq {
        for i in 0..(series.Length+nPreds-1) do
          match i with
          | 0 ->     // initial values        
              smooth <- series |> Array.head |> float
              trend <- initialTrend series slen
              yield series |> Array.head |> float
          | i when i >= series.Length -> // we are forecasting
              let m = i - series.Length + 1
              yield (smooth + float m * trend) + seasonals.[i%slen]
          | _ -> 
              let v = series |> Array.head  |> float
              let lastSmooth = smooth
              smooth <- alpha*(v-seasonals.[i%slen]) + (1.-alpha)*(smooth+trend)
              trend <- beta * (smooth-lastSmooth) + (1.-beta)*trend
              seasonals.[i%slen] <- gamma*(v-smooth) + (1.-gamma)*seasonals.[i%slen]
              yield smooth + trend + seasonals.[i%slen] }

因此,序列表达式的形式为seq { expr },并且在表达式内使用yield产生结果.

So, a sequence expressions is of the form seq { expr } and inside the expression you use yield to yield results.

这篇关于该表达式的结果被隐式忽略.考虑使用“忽略",例如'expr |&gt;忽略"或“例如'让结果= expr'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆