diff --git a/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py b/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py index 3c62a74..c0ce2ec 100644 --- a/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py +++ b/Artificial Intelligence/Statistics and Machine Learning/001. Laptop Battery Life.py @@ -1,15 +1,29 @@ # Problem: https://www.hackerrank.com/challenges/battery/problem # Score: 10 - +# By @Rsc2414 import pandas as pd from sklearn.linear_model import LinearRegression +# Read the input time charged +time_charged = float(input()) -timeCharged = float(input()) +# Load the training data from the file data = pd.read_csv('trainingdata.txt', names=['charged', 'lasted']) -train = data[data['lasted'] < 8] + +# Use only data points where battery lasted less than 8 hours (as per problem note) +filtered_data = data[data['lasted'] < 8] + +# Prepare the training input (X) and output (y) +X = filtered_data['charged'].values.reshape(-1, 1) +y = filtered_data['lasted'].values.reshape(-1, 1) + +# Create and train the linear regression model model = LinearRegression() -model.fit(train['charged'].values.reshape(-1, 1), train['lasted'].values.reshape(-1, 1)) -ans = model.predict([[timeCharged]]) -print(min(ans[0][0], 8)) +model.fit(X, y) + +# Predict battery life for the given charging time +predicted = model.predict([[time_charged]]) + +# Print the predicted time, but cap it at 8 hours +print(min(predicted[0][0], 8))