You need to focus on the area between #%%%PLACEHOLDER_2#start and #%%%PLACEHOLDE
ID: 3756539 • Letter: Y
Question
You need to focus on the area between #%%%PLACEHOLDER_2#start and #%%%PLACEHOLDER_2#end. You need to define iterError (the difference between the estimated price and true price), the delta (using the deltaHistory), and adjust the estimation of unit prices based on the current error (a calculation that should involve the estimated price, the learning rate, and the iterError). Make sure the code works and that your result is close to the ground unit price given.
import numpy as np
import matplotlib.pyplot as plt
from dataNormalization import rescaleNormalization
# Fill in the codes between "%PLACEHOLDER#start" and "PLACEHOLDER#end"
# Ground-truth Cashier
groundUnitPrice = np.array([20, 25, 8]) # for fish, chip, and ketchup, respectively
# step 1: initialize your guess on the unit prices of fish, chip and ketchup.
estimatedUnitPrice = np.array([10,10,10]) # initial unit prices.
#MAX_POSSIBLE_UNIT_PRICE = 50
#estimatedUnitPrice = np.random.randint(MAX_POSSIBLE_UNIT_PRICE, size=3) # choose random initial guesses
#PLACEHOLDER_1#start: set your own stopping conditions and learning rate
#condition 1: maximal iterations, stop.
MAX_ITERATION = 100000
#condition 2: if the difference between your prediction and the cashier's price is smaller than a threshold, stop.
MIN_DELTA = 0.01
# learning rate
ALPHA = .001#1e-3
#PLACEHOLDER_1#end
# Y coordinates for plotting
deltaHistory = []
# step 2: iterative method
for i in range(0, MAX_ITERATION):
# order a meal (simulating training data)
randomMealPortions = np.random.randint(10, size=3)
# calculate the estimated price
expectedTotalPrice = np.sum(estimatedUnitPrice * randomMealPortions )
# calculate cashier/true price;
cashierPrice = np.sum(groundUnitPrice * randomMealPortions)
#%%%PLACEHOLDER_2#start
# calculate current error
iterError =
# append iterError to the history array
deltaHistory.append(abs(iterError))
delta =
#update unit prices
estimatedUnitPrice =
#%%%%PLACEHOLDER_2#end
#check stop conditions
if abs(delta) < MIN_DELTA:
break
print('iteration:{}, delta:{}'.format(i, abs(delta)))
# step 3: evaluation
error = np.mean(abs(estimatedUnitPrice - groundUnitPrice))
print('estimation error:{}'.format(error))
# visualize convergence curve: error v.s. iterations
plt.plot(range(0, len(deltaHistory)), deltaHistory)
plt.xlabel('iteration (cnt:{})'.format(len(deltaHistory)))
plt.ylabel('abs(delta)')
plt.title('Final:{} est err:{} actl :{}'.format([ '%.4f' % elem for elem in estimatedUnitPrice ], round(error, 4), round(delta, 4)))
plt.show()
Explanation / Answer
# Fill in the codes between "%PLACEHOLDER#start" and "PLACEHOLDER#end"
# Ground-truth Cashier
groundUnitPrice = np.array([20, 25, 8]) # for fish, chip, and ketchup, respectively
# step 1: initialize your guess on the unit prices of fish, chip and ketchup.
estimatedUnitPrice = np.array([10,10,10]) # initial unit prices.
#MAX_POSSIBLE_UNIT_PRICE = 50
#estimatedUnitPrice = np.random.randint(MAX_POSSIBLE_UNIT_PRICE, size=3) # choose random initial guesses
#PLACEHOLDER_1#start: set your own stopping conditions and learning rate
#condition 1: maximal iterations, stop.
MAX_ITERATION = 100000
#condition 2: if the difference between your prediction and the cashier's price is smaller than a threshold, stop.
MIN_DELTA = 0.01
# learning rate
ALPHA = .001#1e-3
#PLACEHOLDER_1#end
# Y coordinates for plotting
deltaHistory = []
# step 2: iterative method
for i in range(0, MAX_ITERATION):
# order a meal (simulating training data)
randomMealPortions = np.random.randint(10, size=3)
# calculate the estimated price
expectedTotalPrice = np.sum(estimatedUnitPrice * randomMealPortions )
# calculate cashier/true price;
cashierPrice = np.sum(groundUnitPrice * randomMealPortions)
#%%%PLACEHOLDER_2#start
# calculate current error
iterError =
# append iterError to the history array
deltaHistory.append(abs(iterError))
delta =
#update unit prices
estimatedUnitPrice =
#%%%%PLACEHOLDER_2#end
#check stop conditions
if abs(delta) < MIN_DELTA:
break
print('iteration:{}, delta:{}'.format(i, abs(delta)))
# step 3: evaluation
error = np.mean(abs(estimatedUnitPrice - groundUnitPrice))
print('estimation error:{}'.format(error))
# visualize convergence curve: error v.s. iterations
plt.plot(range(0, len(deltaHistory)), deltaHistory)
plt.xlabel('iteration (cnt:{})'.format(len(deltaHistory)))
plt.ylabel('abs(delta)')
plt.title('Final:{} est err:{} actl :{}'.format([ '%.4f' % elem for elem in estimatedUnitPrice ], round(error, 4), round(delta, 4)))
plt.show()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.