How do I create a loop repeating the following but each time deducting 0.1 more from a=10? It should repeat a 100 times and then stop. Thanks!
for i in x:
yt = (a - 0.1)* i
MSE = np.square(np.subtract(y,yt)).mean()
You could use a while loop instead, like the following:
a = 10
while a > 0:
yt = (a-0.1)
MSE = np.square(np.subtract(y,yt)).mean()
a -= 0.1
That way, if a == 0 the loop stops and yt won't become 0. If that is what you are asking for. Due to accurancy problems, often a repeated -0.1 will result in rounding errors and could create unwanted results. Thus I recommand using something like this:
a = 100
while a > 0:
yt = (a/10-0.1)
MSE = np.square(np.subtract(y,yt)).mean()
a -= 1
Alternatively after the newest comment: Using temporary values which get compared iteratively every loop index:
import numpy as np
a = 10
y = 5.423 #example value
tmp_MSE = np.infty #the first calculated MSE is always smaller then infty
tmp_a = a #if no modified a results in a better MSE, a itself is closest
for i in range(100):
yt = a-0.1*(i+1)
MSE = np.square(np.subtract(y,yt)).mean()
if MSE < tmp_MSE: #new MSE comparison
tmp_MSE = MSE #tmp files are updated
tmp_a = yt
print("nearest value of a and MSE:",tmp_a,tmp_MSE)