def moving_average_forecast(series, window_size):
"""Forecasts the mean of the last few values.
If window_size=1, then this is equivalent to naive forecast"""
forecast = []
for time in range(len(series) - window_size):
forecast.append(series[time:time + window_size].mean())
return np.array(forecast)
moving_avg = moving_average_forecast(series, 30)[split_time - 30:]
What does this [split_time - 30:] mean after the function call moving_average_forecast(series, 30)?
PS: The series is an numpy array.
Thanks
It is simply a shorthand for doing: arr = moving_average_forecast(series, 30) ; moving_avg = arr[split_time - 30:] Thank you #Tomerikoo
Related
temp = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
I am stuck in this assignment and unable to find a relevant answer to help.
I’ve used .split(",") and float()
and I am still stuck here.
temp = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
li = temp.split(",")
def avr(li):
av = 0
for i in li:
av += float(i)
return av/len(li)
print(avr(li))
You can use sum() to add the elements of a tuple of floats:
temp = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
def average (s_vals):
vals = tuple ( float(v) for v in s_vals.split(",") )
return sum(vals) / len(vals)
print (average(temp))
Admittedly similar to the answer by #emacsdrivesmenuts (GMTA).
However, opting to use the efficient map function which should scale nicely for larger strings. This approach removes the for loop and explicit float() conversion of each value, and passes these operations to the lower-level (highly optimised) C implementation.
For example:
def mean(s):
vals = tuple(map(float, s.split(',')))
return sum(vals) / len(vals)
Example use:
temp = '75.1,77.7,83.2,82.5,81.0,79.5,85.7'
mean(temp)
>>> 80.67142857142858
I don't think I fully understand the use of intermediate variables in arrays and would love some help with my code.
Along with the error this equation is posted ((-1)*((((((0.95)*(i371)))*(9))-((int_v2)*(4))))), it looks like my objective function
yh = model.Array(model.Intermediate,(10),equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]*f[i]*0.1) #x,f are variable arrays of size 10
y1 = model.Array(model.if3, (10), x1=1, x2=0, condition=sum(yh)-d) #d is a constant array of size 10
y2 = model.Array(model.if3, (10), x1=1, x2=0, condition=-1*(sum(yh)-lb)) #lb is a constant array of size 10
model.Equation(sum(x)==10)
model.options.IMODE = 3
model.options.SOLVER = 1
m2 = model.Array(model.Intermediate,(10,10),equation=None)
for i in range(10):
for j in range(10):
m2[i][j] = model.Intermediate(m[i][j]*x[i]*0.1*y1[j]*y2[j]) #m is a 10x10 constant array, i'm trying to multiply every element in a row
#with the corresponding x value, and every element in a column with the corresponding y value
r = model.Array(model.Intermediate,(10),equation=None)
for i in range(10):
r[i]= model.Intermediate(sum(m2[j][i] for j in range(10))) #im trying to get the sum of each column
model.Obj(-1*(0.95*r*c2-x*c1)) #c1,c2 are constant arrays; x is a variable array
model.solve()
Here is a complete script that demonstrates the two issues with your current program.
from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = model.Array(model.Intermediate,10,equation=None)
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Obj(yh)
model.solve()
The first is that you are creating an array of Intermediate types and then creating them again in your loop. This gives the error:
#error: Model Expression
*** Error in syntax of function string: Invalid element: none
Position: 1
none
?
because the first Intermediates that you create have blank equations. You can avoid this error by just defining a list of None values.
yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
The second error is because you are using an array in the objective statement (as you already noted in your answer). This gives the error:
Warning: there is insufficient data in CSV file 136.36.211.159_gk_model0.csv
#error: Model Expression
*** Error in syntax of function string: Missing operator
Position: 2
0,0,0,0,0,0,0,0,0,0
?
As you correctly noted, you can add a summation to add the terms into a single term. You can also have multiple model.Obj() functions or model.Minimize() as a more descriptive version of the same function.
from gekko import GEKKO
model = GEKKO()
x = model.Array(model.Var,10)
yh = [None]*10
for i in range(10):
yh[i] = model.Intermediate(x[i]**2)
model.Equation(sum(x)==10)
model.Minimize(model.sum(yh))
model.solve()
```model.Obj(-1*(0.95*sum(r*c2)-sum(x*c1)))```
solved the issue as the objective function returns one value now not an array
I'm new to Stack Overflow. I searched for tips on my problem but unfortunately i could not find the solution i was looking for. I think it is not a very hard problem but i don't see the syntax error and it's driving me crazy. so the code:
MP = []
MP_ext = 2019
MP_num = 3
for x in range(0:MP_num):
MP[x] = MP.append('MP%s_%s' %MP_ext %x)
print(MP)
alternatively i tried this:
for x in range(0:MP_num):
MP[x] = MP.append('MP' + str(MP_ext) + '_' + str(x))
print(MP)
What i try to get is the vector MP like this:
[
MP2019_1,
MP2019_2,
MP2019_3
]
Thanks in advance for any advices
Welcome to Stack Overflow! I hope this helps:
MP = []
MP_ext = 2019
MP_num = 3
# range takes a single argument: an integer
# (it has optional arguments, see: https://www.w3schools.com/python/ref_func_range.asp)
# it will return a range of values like this: range(3) = [0,1,2]
for x in range(MP_num):
# string formatting using f-strings (Python 3.6+)
# list.append is in-place - list is mutable, don't need to reassign it
MP.append(f'MP{MP_ext}_{x+1}')
print(MP)
I am currently learning python, I just have one little question over here.
I used for loop and getting a result below.
Here is my code:
def psuedo_random(multiplier, modulus, X_0, x_try):
for i in range(x_try):
place_holder = []
count = []
next_x = multiplier * X_0 % modulus
place_holder.append(next_x)
X_0 = next_x
for j in place_holder:
j = j/modulus
count.append(j)
print(count)
Result:
[0.22021484375]
[0.75439453125]
[0.54443359375]
[0.47705078125]
Can we somehow change it into something like this?
[0.22021484375, 0.75439453125, 0.54443359375, 0.47705078125]
After you initialized a list, you can use append function in the loop.
initialize a list where you want to list these numbers
mylist = []
use this function in your for loop
for i in .....:
mylist.append(i)
It's simple. Do not initialize your list inside the loop. Just place it outside.
I am trying to define a function for Fibonacci series but the code is not working. I can't resolve the issues and need help to fix the problem. Whenever I am trying to call this function, last value of the series comes always greater than n, which I don't want
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
if Series[-1]>n:
break
return Series
Your code is really good, just the indentation of the return is wrong. Just align it properly.
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
return Series
do you need something like this:
def fibo(n):
l = [0,1]
for i in range(2,n+1):
l += [l[i-1] + l[i-2]]
return l
If you want to get the Fibonacci sequence up to n:
def fib(n):
series = [0,1]
if n > 1:
c = 1
while c <= n:
series.append(c)
c = series[-2] + series[-1]
return series