• + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    import numpy as np  
    from sklearn.linear_model import LinearRegression
    
    data = []
    
    with open('trainingdata.txt', 'r') as file:
        for line in file:
            charging_time, battery_lasted = map(float, line.strip().split(','))
            data.append((charging_time, battery_lasted))
    
    
    data = np.array(data) 
    
    X = data[:, 0]
    y = data[:, 1]
    
    # determines the minimum charging time at which the battery life reaches its maximum possible value (8.00 hours).
    
    #  find the smallest charging time at which the battery life reaches 8.00.
    
    threshold = min(X[y== 8])
    
    
    filtered_X = X[X < threshold].reshape(-1, 1)
    filtered_y = y[X < threshold]
    
    lr = LinearRegression().fit(filtered_X, filtered_y)
    
    def predict_battery_life(charge_time):
        if charge_time >= threshold:
            return 8 
        return lr.predict(np.array([[charge_time]]))[0] 
    
    if __name__ == '__main__':
        timeCharged = float(input().strip())
        predicted_life = predict_battery_life(timeCharged)
        print(round(predicted_life, 2))