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.
Even not strictly necessary for this challenge, i got back to theory and implentation of least square regression, as this method opens solutions for a lot of problems, including this one.
#!/bin/python3importmathimportosimportrandomimportreimportsysimportpandasaspdimportnumpyasnpfromscipyimportoptimizefromioimportStringIOif__name__=='__main__':timeCharged=float(input().strip())# Read the data using pandasdata=pd.read_csv('trainingdata.txt',header=None,names=["TimeCharged","TimeLasted"])# training data: Range when not fully charge rangeindex_values=data.query("TimeCharged <= 4.01").index.tolist()x=data.TimeCharged[index_values]y=data.TimeLasted[index_values]# create basis functions for least square regression defbasis_func_lin(x,b1):returnb1*pow(x,1)# ordefbasis_func_quad(x,b1,b2):returnb1*pow(x,1)+b2*pow(x,2)#choose funcfunc=basis_func_lin# do training (least square regression)beta_params,covariance=optimize.curve_fit(func,x,y)# Make predictioniftimeCharged>4.01:#Batteryfullychargedprint(8)else:predicted_time=func(timeCharged,*beta_params)print(predicted_time)#Outputthepredictedbatterylife
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 →
Even not strictly necessary for this challenge, i got back to theory and implentation of least square regression, as this method opens solutions for a lot of problems, including this one.
I super recommned this book: https://pythonnumericalmethods.studentorg.berkeley.edu/notebooks/chapter16.04-Least-Squares-Regression-in-Python.html