How Many Hours for an 80?

Machine Learning in CodeMedium+10 XP+10

How Many Hours for an 80?

A music teacher logged how many hours each student practised (hours) and their exam mark (marks). A new student asks: how many hours should I practise to be predicted a mark of target?

Write hours_for_mark(hours, marks, target).

  • Fit a LinearRegression model with hours as the only feature.
  • The fitted line is mark = coef_ * hours + intercept_. Find the number of hours where this line equals target.
  • Return that number of hours, rounded to 2 decimal places, as a plain float.

The fitted slope is never zero in the tests. The answer may be outside the range of hours in the data, or even negative. Return it anyway.

Examples

hours = [2, 4, 6, 8, 10]
marks = [52, 60, 67, 75, 84]
target = 80
returns 9.14
hours = [1, 2, 3]
marks = [40, 50, 60]
target = 75
returns 4.5

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run