We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you copy the data provided and plot it -- the solution is pretty clear.
`
!/bin/python3
import math
import os
import random
import re
import sys
from sklearn.linear_model import LinearRegression
if name == 'main':
timeCharged = float(input().strip())
battery, duration = [], []
with open('trainingdata.txt', 'r') as inf:
for line in inf.readlines():
x, y = line.replace('\n', '').split(',')
battery.append(float(x))
duration.append(float(y))
lr = LinearRegression()
# Split the data
X, Y = [], []
for x, y in zip(battery, duration):
if x < 4.0:
X.append([x])
Y.append(y)
lr.fit(X, Y)
# predict
print(lr.predict([[timeCharged]])[0] if timeCharged < 4 else 8.0)```
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Laptop Battery Life
You are viewing a single comment's thread. Return to all comments →
Polynomial Regression wasn't really needed..
If you copy the data provided and plot it -- the solution is pretty clear.
`
!/bin/python3
import math import os import random import re import sys from sklearn.linear_model import LinearRegression
if name == 'main': timeCharged = float(input().strip()) battery, duration = [], [] with open('trainingdata.txt', 'r') as inf: for line in inf.readlines(): x, y = line.replace('\n', '').split(',') battery.append(float(x)) duration.append(float(y))