Track C: A Prediction Line with a Warning چالش 6 از جلسهی 78، سخت
مسیر پ. تابع fit(xs, ys) را بنویس که (a, b) خط کمترین مربعات را با فرمول مستقیم برگرداند. بعد predict(hours): اگر hours بیرون از کمترین و بیشترین ساعت داده است، <hours> h: outside the data (<min>-<max> h), not trusted، وگرنه <hours> h: about <mark> (یک رقم اعشار). خروجی:mark = 1.97 * hours + 9.952.5 h: about 14.94 h: about 17.810 h: outside the data (1-5 h), not trustedRemember: correlation is not cause.
STUDY = [2, 4, 1, 3, 5, 2, 3, 1]
MARK = [15, 18, 11, 17, 19, 13, 16, 12]
def fit(xs, ys):
return 0, 0
a, b = fit(STUDY, MARK)
print(f"mark = {a:.2f} * hours + {b:.2f}")
def predict(hours):
return f"{hours} h: about {a * hours + b:.1f}"
for h in [2.5, 4, 10]:
print(predict(h))
print("Remember: correlation is not cause.")
راهنما و جواب این چالش · برگشت به درس جلسهی 78: پروژهی آزاد با مربی (۲): ساختن
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال